简单一点的,如AlphaAnimation。直接定义,然后设置属性,然后startAnimation 复杂一点的,可以用AnimationSet。如下:
AnimationSet animationSet = new AnimationSet(false);
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.1f);
alphaAnimation.setDuration(3000);
RotateAnimation rotateAnimation = new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
rotateAnimation.setDuration(3000);
animationSet.addAnimation(alphaAnimation);
animationSet.addAnimation(rotateAnimation);
imageView.startAnimation(animationSet);
上面的构造函数的参数什么意思就不说明了,自己可以查API。当然也可以用到AnimationUtils
Animation animation = AnimationUtils.loadAnimation(MainActivity.this, R.anim.alpha);
imageView.startAnimation(animation);
其中alpha.xml如下
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<alpha
android:fromAlpha="1"
android:toAlpha="0.1"
android:duration="3000"
android:repeatCount="3"/>
<rotate
android:startOffset="3000"
android:fromDegrees="0"
android:toDegrees="360"
android:duration="3000"/>
</set>
最后讲一下FrameAnimation。先定义Drawable(frame.xml)
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="@drawable/red" android:duration="200" />
<item android:drawable="@drawable/yellow" android:duration="200" />
<item android:drawable="@drawable/blue" android:duration="200" />
</animation-list>
然后像这样使用:
imageView.setBackgroundResource(R.drawable.frame);
ad = (AnimationDrawable)imageView.getBackground();
ad.start();
//不要在onCreate中调用start,因为AnimationDrawable还没有完全跟Window相关联,如果想要界面显示时就开始动画的话,可以在onWindowFoucsChanged()中调用start()。