您的位置:首页 > 移动开发 > Android开发

Android动画之Drawable Animation

2014-10-17 13:58 561 查看
Drawable Animation 的 XML文件放在 /res/drawable目录下,它只将一些drawable元素放在一个容器当中,指定每一个drawable元素的属性,然后一帧一帧地播放出来。

创建如下的动画文件/res/drawable/rocket_thrust.xml

<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
<item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
<item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
</animation-list>每一个item就是动画的一帧,并且可以指定这一帧动画的duration, 因为它在/res/drawable目录下,所以,可以把它设定到一些组件的背景。
AnimationDrawable rocketAnimation;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
rocketImage.setBackgroundResource(R.drawable.rocket_thrust);//将动画设定到rocketImage的背景
rocketAnimation = (AnimationDrawable) rocketImage.getBackground();//强转为AnimationDrawable类型
}

public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {//点击时,启动动画
rocketAnimation.start();
return true;
}
return super.onTouchEvent(event);
}

通过drawable animation可以方便地实现类型于gif动画图的效果
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息