天天看點

自定義控件(動畫)第二節:Android的animation的四種類型的代碼編寫方式

上一節寫的實作方式是xml格式,這種單一的方式肯定不能夠滿足我們今後的開發工作,畢竟需求一直在變嘛(呵呵),是以還涉及到代碼編寫的實作方式,也不複雜,基本上就是跟xml對應的關系書寫方式。

(一)各标簽對應的類:

scale ——  ScaleAnimation

alpha —— AlphaAnimation

rotate ——  RotateAnimation

translate  ——  TranslateAnimation

set  ——  AnimationSet

(二)animation基類的标簽屬性對應的方法:

 android:duration                  setDuration(long)  動畫持續時間,以毫秒為機關 

android:fillAfter                    setFillAfter(boolean) 如果設定為true,控件動畫結束時,将保持動畫最後時的狀态

android:fillBefore                 setFillBefore(boolean) 如果設定為true,控件動畫結束時,還原到開始動畫前的狀态

android:fillEnabled              setFillEnabled(boolean) 與android:fillBefore 效果相同,都是在動畫結束時,将控件還原到初始化狀态

android:repeatCount           setRepeatCount(int)  重複次數

android:repeatMode            setRepeatMode(int)  重複類型,有reverse和restart兩個值,取值為RESTART或 REVERSE,必須與repeatCount一起使用才能看到效果。因為這裡的意義是重複的類型,即回放時的動作。

android:interpolator            setInterpolator(Interpolator) 設定插值器,其實就是指定的動作效果,比如彈跳效果等

(三)ScaleAnimation類的構造函數

  • ScaleAnimation(Context context, AttributeSet attrs)  
  • ScaleAnimation(float fromX, float toX, float fromY, float toY)
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, float pivotX, float pivotY)
  • ScaleAnimation(float fromX, float toX, float fromY, float toY, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

對應代碼示例:

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <scale xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromXScale="0.0"  
  4.     android:toXScale="1.5"  
  5.     android:fromYScale="0.0"  
  6.     android:toYScale="1.5"  
  7.     android:pivotX="50"  
  8.     android:pivotY="50"  
  9.     android:duration="600" />  

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

  1. scaleAnim = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f;  
  2. scaleAnim.setDuration(600);  

(四)AlphaAnimation類的構造函數

  • AlphaAnimation(Context context, AttributeSet attrs)  
  • AlphaAnimation(float fromAlpha, float toAlpha)
  • 對應代碼示例:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <alpha xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:fromAlpha="1.0"  
  4.     android:toAlpha="0.2"  
  5.     android:duration="4000"  
  6.     android:fillBefore="true">  
  7. </alpha>  

↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓

  1. alphaAnim = new AlphaAnimation(1.0f,0.2f);  
  2. alphaAnim.setDuration(4000);  
  3. alphaAnim.setFillBefore(true);  

(五)RotateAnimation類的構造函數

  • RotateAnimation(Context context, AttributeSet attrs)
  • RotateAnimation(float fromDegrees, float toDegrees)
  • RotateAnimation(float fromDegrees, float toDegrees, float pivotX, float pivotY)
  • RotateAnimation(float fromDegrees, float toDegrees, int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)

(六)TranslateAnimation類的構造函數

  • TranslateAnimation(Context context, AttributeSet attrs)  
  • TranslateAnimation(float fromXDelta, float toXDelta, float fromYDelta, float toYDelta)
  • TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue, int fromYType, float fromYValue, int toYType, float toYValue)

(七)AnimationSet類的構造函數

  • AnimationSet(Context context, AttributeSet attrs)  
  • AnimationSet(boolean shareInterpolator)  shareInterpolator取值true或false,取true時,指在AnimationSet中定義一個插值器(interpolater),它下面的所有動畫共同。如果設為false,則表示它下面的動畫自己定義各自的插值器。
  • public void addAnimation (Animation a) 增加動畫
  1. alphaAnim = new AlphaAnimation(1.0f,0.1f);  
  2. scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  3. rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);  
  4. setAnim=new AnimationSet(true);  
  5. setAnim.addAnimation(alphaAnim);  
  6. setAnim.addAnimation(scaleAnim);  
  7. setAnim.addAnimation(rotateAnim);  
  8. setAnim.setDuration(4000);  
  9. setAnim.setFillAfter(true);  

(八)Interpolater

直接上代碼通俗易懂

  1. ScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);  
  2. interpolateScaleAnim.setInterpolator(new BounceInterpolator());  
  3. interpolateScaleAnim.setDuration(4000);  

繼續閱讀