##帧动画FrameAnimation
* 多张图片快速切换,形成动画效果
* 帧动画使用xml定义
package com.itheima.frameanimation; import android.os.Bundle; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.view.Menu; import android.widget.ImageView; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView iv = (ImageView) findViewById(R.id.iv); //把帧动画的资源文件指定为iv的背景 iv.setBackgroundResource(R.drawable.frameanimation); //获取iv的背景 AnimationDrawable ad = (AnimationDrawable) iv.getBackground(); ad.start(); } } /* drawable文件里的frameanimation.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/g1" android:duration="200" /> 每个图片停留200毫秒, <item android:drawable="@drawable/g2" android:duration="200" /> <item android:drawable="@drawable/g3" android:duration="200" /> <item android:drawable="@drawable/g4" android:duration="200" /> <item android:drawable="@drawable/g5" android:duration="200" /> <item android:drawable="@drawable/g6" android:duration="300" /> <item android:drawable="@drawable/g7" android:duration="300" /> <item android:drawable="@drawable/g8" android:duration="300" /> <item android:drawable="@drawable/g9" android:duration="200" /> <item android:drawable="@drawable/g10" android:duration="200" /> <item android:drawable="@drawable/g11" android:duration="200" /> </animation-list> */
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" 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=".MainActivity" > <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </RelativeLayout>
时间: 2024-10-11 17:13:14