android动画篇之帧动画(frameAnimation)



/**
 * Created by zhu on 2018/6/26.
 */

public class FrameView extends View {

    private Drawable birdItem;
    private Context mContext;
    private AnimationDrawable frameAnamation = null;


    public FrameView(Context context) {
        super(context);
        mContext = context;
        frameAnamation = new AnimationDrawable();
        for (int i = 0; i < 9; i++) {
            int id = getResources().getIdentifier("bird" + i, "drawable",
                    mContext.getPackageName());
            birdItem = getResources().getDrawable(id);
            frameAnamation.addFrame(birdItem, 300 / i);
        }

        frameAnamation.setOneShot(false);

        this.setBackgroundDrawable(frameAnamation);
    }

    public FrameView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public FrameView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getPointerCount() == 1) {
            frameAnamation.start();
        } else if (event.getPointerCount() == 2) {
            frameAnamation.stop();
        }

        return true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            frameAnamation.stop();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

猜你喜欢

转载自blog.csdn.net/u014644594/article/details/80811130