上一節寫的實作方式是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)
對應代碼示例:
- <?xml version="1.0" encoding="utf-8"?>
- <scale xmlns:android="http://schemas.android.com/apk/res/android"
- android:fromXScale="0.0"
- android:toXScale="1.5"
- android:fromYScale="0.0"
- android:toYScale="1.5"
- android:pivotX="50"
- android:pivotY="50"
- android:duration="600" />
↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
- scaleAnim = new ScaleAnimation(0.0f,1.5f,0.0f,1.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f;
- scaleAnim.setDuration(600);
(四)AlphaAnimation類的構造函數
- AlphaAnimation(Context context, AttributeSet attrs)
- AlphaAnimation(float fromAlpha, float toAlpha)
- 對應代碼示例:
- <?xml version="1.0" encoding="utf-8"?>
- <alpha xmlns:android="http://schemas.android.com/apk/res/android"
- android:fromAlpha="1.0"
- android:toAlpha="0.2"
- android:duration="4000"
- android:fillBefore="true">
- </alpha>
↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓ ↓ ↓↓↓↓↓↓↓↓↓↓↓
- alphaAnim = new AlphaAnimation(1.0f,0.2f);
- alphaAnim.setDuration(4000);
- 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) 增加動畫
- alphaAnim = new AlphaAnimation(1.0f,0.1f);
- scaleAnim = new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
- rotateAnim = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
- setAnim=new AnimationSet(true);
- setAnim.addAnimation(alphaAnim);
- setAnim.addAnimation(scaleAnim);
- setAnim.addAnimation(rotateAnim);
- setAnim.setDuration(4000);
- setAnim.setFillAfter(true);
(八)Interpolater
直接上代碼通俗易懂
- ScaleAnimation interpolateScaleAnim=new ScaleAnimation(0.0f,1.4f,0.0f,1.4f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
- interpolateScaleAnim.setInterpolator(new BounceInterpolator());
- interpolateScaleAnim.setDuration(4000);