美文网首页
Android 逐帧(Frame)动画

Android 逐帧(Frame)动画

作者: gaookey | 来源:发表于2022-02-14 14:36 被阅读0次
image.jpg
实例:在指定点爆炸

MainActivity

public class MainActivity extends AppCompatActivity {
    public static final int BLAST_WIDTH = 240;
    public static final int BLAST_HEIGHT = 240;
    private MyView myView;
    private AnimationDrawable anim;
    private MediaPlayer bomb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 使用FrameLayout布局管理器,它允许组件自己控制位置
        FrameLayout frame = new FrameLayout(this);
        setContentView(frame);
        // 设置使用背景
        frame.setBackgroundResource(R.drawable.back);
        // 加载音效
        bomb = MediaPlayer.create(this, R.raw.bomb);
        myView = new MyView(this);
        // 设置myView用于显示blast动画
        myView.setBackgroundResource(R.drawable.blast);
        // 设置myView默认为隐藏
        myView.setVisibility(View.INVISIBLE);
        // 获取动画对象
        AnimationDrawable anim = (AnimationDrawable) myView.getBackground();
        frame.addView(myView);
        frame.setOnTouchListener((source, event) -> {
            // 只处理按下事件(避免每次产生两个动画效果)
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                // 先停止动画播放
                anim.stop();
                float x = event.getX();
                float y = event.getY();
                // 控制myView的显示位置
                myView.setLocation((int) y - BLAST_HEIGHT, (int) x - BLAST_WIDTH / 2);
                myView.setVisibility(View.VISIBLE);
                // 启动动画
                anim.start();
                // 播放音效
                bomb.start();
            }
            return false;
        });
    }

    // 定义一个自定义View,该自定义View用于播放“爆炸”效果
    class MyView extends ImageView {
        MyView(Context context) {
            super(context);
        }

        // 定义一个方法,该方法用于控制MyView的显示位置
        public void setLocation(int top, int left) {
            this.setFrame(left, top, left + BLAST_WIDTH, top + BLAST_HEIGHT);
        }

        // 重写该方法,控制如果动画播放到最后一帧时,隐藏该View
        @Override
        public void onDraw(Canvas canvas) // ①
        {
            try {
                Field field = AnimationDrawable.class.getDeclaredField("mCurFrame");
                field.setAccessible(true);
                // 获取anim动画的当前帧
                int curFrame = field.getInt(anim);
                // 如果已经到了最后一帧
                if (curFrame == anim.getNumberOfFrames() - 1) {
                    // 让该View隐藏
                    setVisibility(View.INVISIBLE);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            super.onDraw(canvas);
        }
    }
}

drawable/blast.xml

<?xml version="1.0" encoding="utf-8"?><!-- 定义动画只播放一次,不循环 -->
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item
        android:drawable="@drawable/bom_f01"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f02"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f03"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f04"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f05"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f06"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f07"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f08"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f09"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f10"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f11"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f12"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f13"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f14"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f15"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f16"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f16"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f17"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f18"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f19"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f20"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f21"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f22"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f23"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f24"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f25"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f26"
        android:duration="80" />
    <item
        android:drawable="@drawable/bom_f27"
        android:duration="80" />
</animation-list>
image.gif

摘抄至《疯狂Android讲义(第4版)》

相关文章

网友评论

      本文标题:Android 逐帧(Frame)动画

      本文链接:https://www.haomeiwen.com/subject/blqllrtx.html