1.创建
animator包
2.创建animator.xml文件
<animator xmlns:android="http://schemas.android.com/apk/res/android"
android:valueFrom="0"
android:valueTo="100"
android:valueType="intType"
android:duration="9000">
</animator>
3.在MainActivity中:
private ImageView iv;
private Button b;
private Animator animator;
private ValueAnimator anInt;
private int[] a = {R.mipmap.a,R.mipmap.b,R.mipmap.c,R.mipmap.d
,R.mipmap.e,R.mipmap.f,R.mipmap.g,R.mipmap.h};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
iv = (ImageView) findViewById(R.id.iv);
b = (Button) findViewById(R.id.b);
anInt = ValueAnimator.ofInt(7,0);
//anInt.setTarget(b);
anInt.setDuration(5000);
anInt.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (int) animation.getAnimatedValue();
iv.setImageResource(a[animatedValue]);
}
});
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.b:
//animator.start();
anInt.start();
break;
}
}
网友评论