屬性動(dòng)畫在Android 3.0中引入,為開發(fā)者提供了更強(qiáng)的自定義動(dòng)畫的能力,屬性動(dòng)畫在游戲開發(fā)中比較常見。下面是演示一副圖像的寬度的例子:
代碼:屬性動(dòng)畫的實(shí)例
package com.miaozl.test2;
import android.animation.IntEvaluator;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class PropertyAnimActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout root= ((LinearLayout) this.findViewById(R.id.animlayout));
ImageAnimView imageAnim = new ImageAnimView(this);
imageAnim.setImageResource(R.drawable.photo3);
root.addView(imageAnim);
}
private class ImageAnimView extends ImageView {
public ImageAnimView(Context context) {
super(context);
// TODO Auto-generated constructor stub
ValueAnimator valueAnim = ObjectAnimator.ofInt(this, "width", 0, 200);
valueAnim.setDuration(3000); //持續(xù)時(shí)間
valueAnim.setEvaluator(new IntEvaluator());//設(shè)置演進(jìn)器
valueAnim.setRepeatCount(ValueAnimator.INFINITE);//設(shè)置重復(fù)數(shù)
valueAnim.setRepeatMode(ValueAnimator.REVERSE);//設(shè)置重復(fù)模式
valueAnim.start();
}
}
}
在Android中,目前定義了三種類型的演進(jìn)器:ArgbEvaluator、FloatEvaluator、IntEvaluator等。如果用于動(dòng)畫的屬性不是int、float、color類型的。開發(fā)者可以擴(kuò)展TypeEvaluator接口來計(jì)算目標(biāo)對(duì)象的屬性變化。