控件调用一个样式文件,样式文件的background的背景使用一个动画xml。
代码获取控件的背景转成动画,然后start一下。
控件:
ImageView iv_rocket=(ImageView) view.findViewById(R.id.iv_rocket);
控件的样式文件:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/iv_rocket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@anim/rocket"/>
</RelativeLayout>
样式文件背景调用了动画:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false"> <!-- oneshot : 是否执行一次 true:执行一次 false:循环执行 -->
<item android:drawable="@drawable/desktop_rocket_launch_1" android:duration="200" />
<item android:drawable="@drawable/desktop_rocket_launch_2" android:duration="200" />
</animation-list>
控件获取背景动画并运行动画:代码这里直接将背景动画转换成了一个动画对象。
//火箭控件iv_rocket创建动画
AnimationDrawable animationDrawable=(AnimationDrawable) iv_rocket.getBackground();
animationDrawable.start();//开始动画