在API Demo的View->Animation下可以找到四個Animation的Demo,第一個3D Translate比較復雜,最后再講,先講第2個Interpolator。該Activity對應的是view包內的Animation3.java,和layout的animation_3.xml。
界面的布局不加解釋了,就一個Spinner和一個TextView。不是本文內容。
主要解釋下幾個重點語句。
初始化Animation,從類的名字可以看出是一個變換View的位置的動畫,參數起點橫坐標,終點橫坐標,起點縱坐標,終點縱坐標。
下面是動畫的參數設置,我加上了注釋
[java] view plaincopy a.setDuration(1000);//設置動畫所用的時間 a.setStartOffset(300);//設置動畫啟動的延時 //設置重復模式,RESTART為結束后重新開始,REVERSE為按原來的軌跡逆向返回 a.setRepeatMode(Animation.RESTART); //設置重復次數,INFINITE為無限 a.setRepeatCount(Animation.INFINITE); //根據用戶在Spinner的選擇設置target的進入的方式 switch (position) { case 0: //加速進入 a.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_interpolator)); break; case 1: //減速進入 a.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.decelerate_interpolator)); break; case 2: //加速進入.與第一個的區別為當repeatMode為reverse時,仍為加速返回原點 a.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.accelerate_decelerate_interpolator)); break; case 3: //先往后退一點再加速前進 a.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.anticipate_interpolator)); break; case 4: //減速前進,沖過終點前再后退 a.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.overshoot_interpolator)); break; case 5: //case 3,4的結合體 a.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.anticipate_overshoot_interpolator)); break; case 6: //停止前來回振幾下 a.setInterpolator(AnimationUtils.loadInterpolator(this, android.R.anim.bounce_interpolator)); break; } //讓target開始執行這個動畫 target.startAnimation(a); }
這里使用的是Android已預設的一些動作,我們也可以自定義XML來實現更好看的動畫效果的,這個下一篇再講。
除了TranslationAnimation,還有AlphaAnimation、RotateAnimation、ScaleAnimation,使用這幾個基體動作的組合,可以形成一系列復雜的動畫效果。具體用法請查看SDK。
整個都比較簡單,就一個函數的調用,還不懂的看一下API的注釋和SDK文檔,沒什么難理解的。
From:http://blog.csdn.net/lxw1980/article/details/6162985
AnimationSet 可以包含多個Animation,但都是在同一個時間執行的,是並行,不是串行執行的。
如果AnimationSet中有一些設定,如duration,fillBefore等,它包含的子動作也設定了的話,
子動作中的設定將會給覆蓋掉。
If AnimationSet sets any properties that its children also set (for example, duration or fillBefore), the values of AnimationSet override the child values. AnimationSet as = new AnimationSet( true ); as.addAnimation( ta ); TranslateAnimation t1 = new TranslateAnimation( 2, 200, 8, 300); t1.setDuration( 2000 ); as.addAnimation( t1 ); RotateAnimation r1 = new RotateAnimation( (float) 1, (float) 0.1 ); r1.setDuration( 2000 ); //as.addAnimation( r1 ); imgView.setAnimation(as); //as.setDuration( 6000 ); //as.setStartTime( 1000 ); as.start();
ScaleAnimation應用詳解
android中提供了4中動畫:
AlphaAnimation 透明度動畫效果
ScaleAnimation 縮放動畫效果
TranslateAnimation 位移動畫效果
RotateAnimation 旋轉動畫效果
本節講解ScaleAnimation 動畫,
ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 參數說明: 復制代碼 代碼如下: float fromX 動畫起始時 X坐標上的伸縮尺寸 float toX 動畫結束時 X坐標上的伸縮尺寸 float fromY 動畫起始時Y坐標上的伸縮尺寸 float toY 動畫結束時Y坐標上的伸縮尺寸 int pivotXType 動畫在X軸相對於物件位置類型 float pivotXValue 動畫相對於物件的X坐標的開始位置 int pivotYType 動畫在Y軸相對於物件位置類型 float pivotYValue 動畫相對於物件的Y坐標的開始位置
public class MainActivity extends Activity { ImageView image; Button start; Button cancel; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); image = (ImageView) findViewById(R.id.main_img); start = (Button) findViewById(R.id.main_start); cancel = (Button) findViewById(R.id.main_cancel); /** 設置縮放動畫 */ final ScaleAnimation animation =new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); animation.setDuration(2000);//設置動畫持續時間 /** 常用方法 */ //animation.setRepeatCount(int repeatCount);//設置重復次數 //animation.setFillAfter(boolean);//動畫執行完后是否停留在執行完的狀態 //animation.setStartOffset(long startOffset);//執行前的等待時間 start.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { image.setAnimation(animation); /** 開始動畫 */ animation.startNow(); } }); cancel.setOnClickListener(new OnClickListener() { public void onClick(View v) { /** 結束動畫 */ animation.cancel(); } }); } }