Drawable Animation(Frame Animation):帧动画,就像GIF图片,通过一系列Drawable依次显示来模拟动画的效果。
效果图:
看代码
main.xml
两个Button和一个ImageView
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.tiany.android_10_1.MainActivity" >
<ImageView
android:id="@+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="54dp"/>
<Button
android:id="@+id/btnStart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/img1"
android:layout_marginBottom="95dp"
android:layout_marginRight="55dp"
android:text="@string/btnStart" />
<Button
android:id="@+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnStart"
android:layout_toRightOf="@+id/btnStart"
android:layout_marginLeft="10dp"
android:text="@string/btnStop" />
</RelativeLayout>
java代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnStart = (Button) findViewById(R.id.btnStart);
Button btnStop = (Button) findViewById(R.id.btnStop);
ImageView img1 = (ImageView) findViewById(R.id.img1);
img1.setBackgroundResource(R.anim.frame_ani);
final AnimationDrawable ad = (AnimationDrawable)img1.getBackground();
btnStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ad.start();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ad.stop();
}
});
}
frame_ani.xml
设置图片、停留时间等配置
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/f10025"
android:duration="500"
android:visible="true">
</item>
<item
android:drawable="@drawable/f10026"
android:duration="500"
android:visible="true">
</item>
<item
android:drawable="@drawable/f10027"
android:duration="500"
android:visible="true">
</item>
<item
android:drawable="@drawable/f10028"
android:duration="500"
android:visible="true">
</item>
<item
android:drawable="@drawable/f10029"
android:duration="500"
android:visible="true">
</item>
<item
android:drawable="@drawable/f10033"
android:duration="500"
android:visible="true">
</item>
</animation-list>