標籤:android開發 動畫
android中Tween動畫實現原理:通過對View的內容進行圖形變換 (平移、縮放、旋轉、透明度)的改變來實現動畫效果。動畫效果的定義可用XML來做也可以採用編碼來做,今天簡單講下用代碼來實現Tween動畫中的四種動畫方式。四種動畫分別對就四個動畫類:
漸層透明度動畫效果 |
AlphaAnimation |
漸層尺寸縮放動畫效果 |
ScaleAnimation |
畫面位置移動動畫效果 |
TranslateAnimation |
畫面旋轉動畫效果 |
RotateAnimation |
1:平移操作
/**TranslateAnimation(float fromXDelta, float toXDelta,
float fromYDelta, float toYDelta)
參數fromXDelta為動畫起始時 X座標上的移動位置
參數toXDelta為動畫結束時 X座標上的移動位置
參數fromYDelta為動畫起始時Y座標上的移動位置
參數toYDelta為動畫結束時Y座標上的移動位置*/
AnimationtranslateAnimation=new TranslateAnimation(0, 100, 0, 0);
/**動畫期間(單位:毫秒)*、
translateAnimation.setDuration(3000);
/**動畫插入器*/
translateAnimation.setInterpolator(this, android.R.anim.cycle_interpolator);
/**設定動畫結束後保持當前的位置*/
translateAnimation.setFillAfter(true);
aminationView.startAnimation(translateAnimation);/**aminationView指要實現動畫的View*/
2縮放操作
/**ScaleAnimation(float fromX, float toX, float fromY, float toY,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue);
第一個參數fromX為動畫起始時 X座標上的伸縮尺寸 0.0表示收縮到沒有
第二個參數toX為動畫結束時 X座標上的伸縮尺寸 1.0表示正常無伸縮
第三個參數fromY為動畫起始時Y座標上的伸縮尺寸 值小於1.0表示收縮
第四個參數toY為動畫結束時Y座標上的伸縮尺寸 值大於1.0表示放大*/
/**定義縮放動畫*/
AnimationscaleAnimation=new ScaleAnimation(0.5f, 1.0f,1.0f, 1.0f);
scaleAnimation.setDuration(2000);
scaleAnimation.setFillAfter(true);
scaleAnimation.setRepeatCount(3);
aminationView.startAnimation(scaleAnimation);/**aminationView指要實現動畫的View*/
3旋轉操作
/**RotateAnimation(float fromDegrees, float toDegrees,
int pivotXType, float pivotXValue, int pivotYType, float pivotYValue)
第一個參數fromDegrees為動畫起始時的旋轉角度 此角度是當前為0及360,設定其他值則先跳至該角度的位置再由from - to的值: 負則正向轉,正則反向轉
第二個參數toDegrees為動畫旋轉到的角度
第三個參數pivotXType為動畫在X軸相對於物件位置類型
第四個參數pivotXValue為動畫相對於物件的X座標的開始位置 此值是以本身原始位置為原點,即如設為20%p,則向右移動父控 件的20%位移,為負數則向左移
第五個參數pivotXType為動畫在Y軸相對於物件位置類型
第六個參數pivotYValue為動畫相對於物件的Y座標的開始位置 此值是以本身原始位置為原點,即如設為20%p,則向下移動父控 件的20%位移,為負數則向上移*/
AnimationrotateAnimation=new RotateAnimation(0, 45);
rotateAnimation.setDuration(3000);
rotateAnimation.setFillAfter(true);
aminationView.startAnimation(rotateAnimation);/**aminationView指要實現動畫的View*/
4透明度變化操作
/**AlphaAnimation(float fromAlpha, float toAlpha)
第一個參數fromAlpha為 動畫開始時候透明度 0.0表示完全透明
第二個參數toAlpha為 動畫結束時候透明度 1.0表示完全不透*/
AnimationalphaAnimation=new AlphaAnimation(1, (float) 0.1);
alphaAnimation.setDuration(3000);//設定動畫期間為3秒
alphaAnimation.setFillAfter(true);//設定動畫結束後保持當前的位置(即不返回到動畫開始前的位置)
aminationView.startAnimation(alphaAnimation);/**aminationView指要實現動畫的View*/
android動畫之:補間動畫(Tween動畫)