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

本文介绍了Android中Frame动画的使用,通过创建AnimationDrawable对象,添加帧内容并设置播放间隔,演示如何实现一个简单的动画效果。此外,还提到了通过setOneShot方法控制动画是否重复播放的功能。
摘要由CSDN通过智能技术生成
               

大家平时见到的最多的可能就是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>


 

 

源代码:点击打开链接

 

 

 

 

           
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值