Android学习笔记进阶十一图片动画播放(AnimationDrawable)

               

大家平时见到的最多的可能就是Frame动画了,Android中当然也少不了它。它的使用更加简单,只需要创建一个

AnimationDrawabledF对象来表示Frame动画,然后通过addFrame 方法把每一帧要显示的内容添加进去,并设置播放间隔时间,本例子中间隔时间为5S,

最后通过start 方法就可。

以播放这个动画了,同时还可以通过 setOneShot方法设置是否重复播放。

package xiaosi.bu;import android.app.Activity;import android.graphics.drawable.AnimationDrawable;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;public class TupianActivity extends Activity {    /** Called when the activity is first created. */ private Button start = nullprivate Button stop = nullprivate ImageView image = nullprivate AnimationDrawable animationDrawable = null;    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                start = (Button)findViewById(R.id.start);        start.setOnClickListener(new StartListener());        stop = (Button)findViewById(R.id.stop);        stop.setOnClickListener(new StopListener());                image = (ImageView)findViewById(R.id.imageview);                animationDrawable = new AnimationDrawable();        for(int i =0;i<8;i++){         //第一个 就是我们的资源名称(图片名)           //第二个 就是我们存放图片的文件夹drawable           //第三个 包名也可以用Context的getPackageName返回应用程序的包名           int id = getResources().getIdentifier( "a"+i, "drawable", "xiaosi.bu");         System.out.println("ID:" + id);            animationDrawable.addFrame(getResources().getDrawable(id), 2000);        }      //设置手否重复播放,false为重复        animationDrawable.setOneShot(false);        image.setImageDrawable(animationDrawable);    }    private class StartListener implements OnClickListener{  public void onClick(View v)  {   animationDrawable.start();  }    }        private class StopListener implements OnClickListener{  public void onClick(View v)  {   animationDrawable.stop();    }    }}


main.xml

<?xml version="1.0" encoding="utf-8"?>    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"        android:orientation="vertical"         android:layout_width="fill_parent"       android:layout_height="fill_parent">       <LinearLayout        android:orientation="horizontal"         android:layout_width="fill_parent"        android:layout_height="wrap_content">       <Button android:id="@+id/start"           android:text="Start"            android:layout_width="wrap_content"           android:layout_height="wrap_content"/>       <Button android:id="@+id/stop"           android:text="End"           android:layout_width="wrap_content"           android:layout_height="wrap_content"/>       </LinearLayout>       <ImageView android:id="@+id/imageview"             android:layout_width="fill_parent"            android:layout_height="fill_parent"            android:scaleType="fitXY"            android:background="#ffffff" /> </LinearLayout>


 

源代码:点击打开链接

           

再分享一下我老师大神的人工智能教程吧。零基础!通俗易懂!风趣幽默!还带黄段子!希望你也加入到我们人工智能的队伍中来!https://blog.csdn.net/jiangjunshow

猜你喜欢

转载自blog.csdn.net/sdfsdfytre/article/details/86588857