天天看點

android四種補間動畫

android的動畫分為兩大類:補間動畫,幀動畫。

補間動畫又分為四大類:移動補間動畫,縮放補間動畫,旋轉補間動畫,透明補間動畫。

android四種補間動畫

這四種補間動畫都是Animation的子類。

移動補間動畫:TranslateAnimation

eg:

Animation  animation = new TranslateAnimation(0,50,0,50);

參數1:x軸的起始位置

參數2:x軸的終止位置

參數3:  y軸的起始位置

參數4:y軸的終止位置

相對于原圖位置的原點(圖檔的右上角為0,0),如果不想用這個點作為參照點,可以使用其他構造

TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue)

參數1,參數3,參數5,參數7就是設定參照點的方式

可以通過Animation類的常量進行設定例如:Animation.RELATIVE_TO_SELF

縮放補間動畫:ScaleAnimation

eg:

Animation   animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 

參數1:x方向起始大小(1f表示原圖大小)

參數2:x方向終止大小(0.2f表示原圖的0.2倍)

參數3:y方向起始大小(1f表示原圖大小)

參數4:y方向終止大小(0.2f表示原圖的0.2倍)

參數5:縮放中心點x軸取值的參照方式

參數6:中心點x軸的取值(0.5f表示相對與原圖的0.5倍)

參數7:縮放中心點y軸取值參照方式

參數8:中心點y軸的取值(0.5f表示相對與原圖的0.5倍)

 旋轉補間動畫:RotateAnimation

eg:

 Animation animation  = new RotateAnimation(360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 

參數1:旋轉的起始角度

參數2:旋轉的終止角度

參數3:旋轉中心的x軸取值參照方式

參數4:中心點x軸的取值

參數5:旋轉中心的y軸取值參照方式

參數6:中心點y軸的取值

透明補間動畫: AlphaAnimation

eg:

Animation animation = new AlphaAnimation(1f,0.1f);

參數1: 起始透明度;

參數2: 目标透明度;

每種動畫都有很多種重載,可以根據需求進行選擇,如果想讓動畫有效果還得設定動畫的時間

//設定動畫持續時間

   animation.setDuration(2000);

以毫秒為機關

對于動畫還可以設定渲染器

eg:

   //渲染器  android系統提供了很多渲染器資源 通過android.R.anim.的方式使用

   animation.setInterpolator(Main.this,android.R.anim.anticipate_overshoot_interpolator);

如果想要多個動畫效果同時使用,可以通過AnimationSet 實作:

AnimationSet animationSet = new AnimationSet(false);

   animationSet.addAnimation(animation);

得到動畫對象之後就是使用了,每個view都有startAnimation(animation)方法

因為AnimationSet 繼承自Animation類是以該方法的參數既可以是動畫對象(Animation)也可以是動畫集(AnimationSet )對象