帧动画
1、帧动画文件实现
1)在res目录下新建drawable目录,把动画图片放进去,并实现一个xml文件,例如:girl_list.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> 3 <item android:drawable="@drawable/a1" android:duration="100" /> 4 <item android:drawable="@drawable/a2" android:duration="100" /> 5 <item android:drawable="@drawable/a3" android:duration="100" /> 6 <item android:drawable="@drawable/a4" android:duration="100" /> 7 <item android:drawable="@drawable/a5" android:duration="100" /> 8 </animation-list>
2、视图
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical" 6 tools:context=".MainActivity" > 7 8 <ImageView 9 android:id="@+id/iv" 10 android:layout_width="wrap_content" 11 android:layout_height="0dip" 12 android:layout_weight="100" 13 android:background="@drawable/a1" 14 /> 15 <Button 16 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:onClick="click" 20 android:text="播放" 21 /> 22 23 </LinearLayout>
3、java代码
1 package com.example.myframe; 2 3 import android.os.Bundle; 4 import android.app.Activity; 5 import android.graphics.drawable.AnimationDrawable; 6 import android.view.Menu; 7 import android.view.View; 8 import android.widget.ImageView; 9 10 public class MainActivity extends Activity { 11 private AnimationDrawable rocketAnimation; 12 private ImageView iv; 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 iv = (ImageView) findViewById(R.id.iv); 18 //设置背景资源 19 iv.setBackgroundResource(R.drawable.girl_list); 20 rocketAnimation = (AnimationDrawable) iv.getBackground();//这个是异步方法,所以最好不要马上start方法 21 22 } 23 24 public void click(View view){ 25 rocketAnimation.start(); 26 } 27 28 }

浙公网安备 33010602011771号