帧动画
程序员文章站
2022-03-01 20:53:51
...
MainActivity.java
public class MainActivity extends Activity {
private ImageView iv_show;
private AnimationDrawable animationDrawable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv_show = (ImageView) findViewById(R.id.iv_show);
iv_show.setBackgroundResource(R.drawable.girl_show);
//得到动画资源对象
animationDrawable = (AnimationDrawable) iv_show.getBackground();
}
public boolean onTouchEvent(MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
animationDrawable.start();
return true;
}
return super.onTouchEvent(event);
}
}
activity_main.xml
<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"
tools:context=".MainActivity" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/iv_show" />
</RelativeLayout>
girl_show.xml
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true"
>
<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"/>
<item
android:drawable="@drawable/girl_4"
android:duration="200"/>
<item
android:drawable="@drawable/girl_5"
android:duration="200"/>
<item
android:drawable="@drawable/girl_6"
android:duration="200"/>
<item
android:drawable="@drawable/girl_7"
android:duration="300"/>
<item
android:drawable="@drawable/girl_6"
android:duration="200"/>
<item
android:drawable="@drawable/girl_7"
android:duration="300"/>
<item
android:drawable="@drawable/girl_6"
android:duration="200"/>
<item
android:drawable="@drawable/girl_7"
android:duration="300"/>
<item
android:drawable="@drawable/girl_8"
android:duration="200"/>
<item
android:drawable="@drawable/girl_9"
android:duration="200"/>
<item
android:drawable="@drawable/girl_10"
android:duration="200"/>
<item
android:drawable="@drawable/girl_11"
android:duration="200"/>
</animation-list>