帧动画是有很多张图片连续显示的效果。
首先在res目录下新建个drawable文件夹,在drawable文件夹下新建一个animation-list类型的xml文件,在该xml文件中定义要播放的图片,每个图片播放的时长,播放的顺序等。如:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:drawable="@drawable/girl_1"
android:duration="200"/>
<item
android:drawable="@drawable/girl_2"
android:duration="200"/>
<item
android:drawable="@drawable/girl_3"
android:duration="200"/>
</animation-list>
在animation-list节点下还可以设置android:oneshot="true"为true表示动画只执行一次,默认为false。
private ImageView iv;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView) findViewById(R.id.iv);
// 把xml文件的动画资源设置为iv的背景
iv.setBackgroundResource(R.drawable.girl);
// 获取设置的动画资源,执行可能需要一定的时间
animationDrawable = (AnimationDrawable) iv.getBackground();
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
animationDrawable.start();
return true;
}
return super.onTouchEvent(event);
}
补间动画
程序自动补充两个帧之间的动画效果。
补间动画的种类:旋转、缩放、位移、透明度(0是完全透明)。还可以将这些动画组合在一起播放。用AnimationSet.
是否用共同的播放速度
AnimationSet as = new AnimationSet(false);
// 旋转动画
public void rotate(View v) {
RotateAnimation ra = new RotateAnimation(0, 180,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
ra.setDuration(2000);
ra.setRepeatCount(2);
ra.setRepeatMode(Animation.REVERSE);
ra.setFillAfter(true);
iv.startAnimation(ra);
}
// 缩放动画
public void scale(View v) {
ScaleAnimation sa = new ScaleAnimation(1.0f, 2.0f, 1.0f, 2.0f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
sa.setDuration(2000);
sa.setRepeatCount(2);
sa.setRepeatMode(Animation.REVERSE);
sa.setFillAfter(true);
iv.startAnimation(sa);
}
// 位移动画
public void translate(View v) {
TranslateAnimation ta = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 1);
ta.setDuration(2000);
ta.setRepeatCount(2);
ta.setRepeatMode(Animation.REVERSE);
ta.setFillAfter(true);
iv.startAnimation(ta);
}
// 透明度动画
public void alpha(View v) {
AlphaAnimation aa = new AlphaAnimation(100, 0);
// 设置一次播放的持续时间
aa.setDuration(2000);
// 设置重复播放次数,一共播放3次.若为-1,则一直播放。
aa.setRepeatCount(2);
// 设置重复播放的模式,reverse是倒序播放
aa.setRepeatMode(Animation.REVERSE);
// 设置动画完成之后的效果,若为true保留动画做完之后的效果,若为false,动画做完后回到最初的效果
aa.setFillAfter(true);
iv.startAnimation(aa);
}
xml文件自定义补间动画
首先在res目录下新建名为anim的文件夹,然后再在anim文件夹下创建xml文件如alpha.xml:
<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="3000"
android:fillAfter="true"
android:fromAlpha="100"
android:repeatCount="3"
android:repeatMode="reverse"
android:toAlpha="0" >
</alpha>
调用:
// 透明度动画
public void alpha(View v) {
Animation au = AnimationUtils.loadAnimation(this, R.anim.alpha);
iv.startAnimation(au);
}
其他的动画定义格式、调用方式都一样的。其中pivotX和pivotY若都设为0.5是相对于图片本身,若设为0.5p是相对于它父亲。