欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Android 帧动画

程序员文章站 2022-03-25 23:41:26
...

实现

https://www.shanshanstory.com/
在deawable 新建文件 frame_anim.xml

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:drawable="@mipmap/d_0"
        android:duration="100" />
    <item
        android:drawable="@mipmap/d_1"
        android:duration="100" />
    <item
        android:drawable="@mipmap/d_2"
        android:duration="100" />
    <item
        android:drawable="@mipmap/d_3"
        android:duration="100" />
    <item
        android:drawable="@mipmap/d_4"
        android:duration="100" />
    <item
        android:drawable="@mipmap/d_5"
        android:duration="100" />
    <item
        android:drawable="@mipmap/d_6"
        android:duration="100" />
    <item
        android:drawable="@mipmap/d_7"
        android:duration="100" />
</animation-list>

使用

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_frame_anim);
        mImageView = (ImageView) findViewById(R.id.iv);
        mImageView.setImageResource(R.drawable.frame_anim);
        AnimationDrawable animationDrawable = (AnimationDrawable) mImageView.getDrawable();
        animationDrawable.setOneShot(false);  //循环
        animationDrawable.start();
    }

Java 代码实现

 private void initAnimationDrawable() {
        AnimationDrawable mAnimationDrawable = new AnimationDrawable();
        for (int i = 0; i < 8; i++) {
            int id = getResources().getIdentifier("d_" + i, "mipmap", getPackageName());
            Drawable drawable = getResources().getDrawable(id);
            mAnimationDrawable.addFrame(drawable, 100);
        }
        mImageView.setImageDrawable(mAnimationDrawable);
        mAnimationDrawable.setOneShot(false);
        mAnimationDrawable.start();
    }