Android三种动画之(一)帧动画

帧动画介绍:就是将原有的照片一帧一帧的排列好按照一定的顺序播放而达到动画的效果

技术实现:实现的方式有两种,一种是在资源文件中添加图片,一种是直接在代码中添加图片

第一种:在资源文件中添加图片

1.在drawable里面新建文件frame_animation.xml 

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="false">

    <!--其中 android:oneshot="false" 是表示不循环一次,true代表的是只循环一次-->
    <!--其中 android:drawable="@mipmap/zhen1" 代表的是添加资源文件-->
    <!--其中 android:duration="100" 代表的是这帧照片停留的时间是多少时间,单位毫秒-->

    <item
        android:drawable="@mipmap/zhen1"
        android:duration="100" />
    <item
        android:drawable="@mipmap/zhen2"
        android:duration="100" />
    <item
        android:drawable="@mipmap/zhen3"
        android:duration="100" />
    <item
        android:drawable="@mipmap/zhen4"
        android:duration="200" />
    <item
        android:drawable="@mipmap/zhen5"
        android:duration="200" />
    <item
        android:drawable="@mipmap/zhen6"
        android:duration="200" />

</animation-list>

然后在布局里面的ImageView调用

        <ImageView
            android:id="@+id/iv_frame"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:src="@drawable/frame_animation" />

最后在代码里面使用这个动画:

        mIvFrame.setImageResource(R.drawable.frameanimation);
        animationDrawable = (AnimationDrawable) mIvFrame.getDrawable();
        animationDrawable.start();

另外还有其他的设置:

animationDrawable.setOneShot(true);//在代码里设置是否循环播放
android:oneshot="false"//在资源文件里面设置是否循环播放,false代表是
animationDrawable.isRunning();//是否正在播放
animationDrawable.start();//表示开始动画
animationDrawable.stop();//表示停止动画,分别在需要的点击事件里面调用

第二种:在代码中添加资源图片

animationDrawable = new AnimationDrawable();
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.zhen1),200);//添加图片
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.zhen2),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.zhen3),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.zhen4),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.zhen5),200);
animationDrawable.addFrame(getResources().getDrawable(R.mipmap.zhen6),200);
animationDrawable.setOneShot(false);//是否循环播放
mIvFrame.setBackground(animationDrawable);
animationDrawable.start();

如果要添加资源文件太多的话可以循环添加:

     animationDrawable = new AnimationDrawable();
     for (int i = 1; i <= 6; i++) {
         int id = getResources().getIdentifier("zhen" + i, "mipmap", getPackageName());
         Drawable drawable = getResources().getDrawable(id);
         animationDrawable.addFrame(drawable, 200);
     }
     animationDrawable.setOneShot(false);
     mIvFrame.setImageDrawable(animationDrawable);
     animationDrawable.start();

下载地址:https://download.csdn.net/download/lanrenxiaowen/10753851

猜你喜欢

转载自blog.csdn.net/lanrenxiaowen/article/details/83545251