仿FlipBoard动态logo动画

link 跟着扔物线大神学习自定义动画,受益匪浅

public class FlipBoardLogoView extends View {

    //首次折叠的一半
    private float rightDegreeY;
    //对应首次折叠的另一半
    private float leftDegreeY;
    //Z轴方向折叠,实现翻转的效果
    private float degreeZ;

    private Paint paint;
    private Bitmap bitmap;
    private Camera camera;

    private ObjectAnimator animator1;
    private ObjectAnimator animator2;
    private ObjectAnimator animator3;

    public FlipBoardLogoView(Context context) {
        this(context, null);
    }

    public FlipBoardLogoView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FlipBoardLogoView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.google_map);
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        camera = new Camera();
        initAnimation();
    }

    private void initAnimation() {
        animator1 = ObjectAnimator.ofFloat(this, "rightDegreeY", 0, -45);
        animator1.setDuration(1000);
        animator1.setStartDelay(500);
        animator2 = ObjectAnimator.ofFloat(this, "degreeZ", 0, 270);
        animator2.setDuration(800);
        animator2.setStartDelay(500);
        animator3 = ObjectAnimator.ofFloat(this, "leftDegreeY", 0, 45);
        animator3.setDuration(1000);
        animator3.setStartDelay(500);
        AnimatorSet set = new AnimatorSet();
        set.playSequentially(animator1, animator2,animator3);
        set.start();

        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);
                //动画结束后
            }
        });
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int mWidth = bitmap.getWidth();
        int mHeight = bitmap.getHeight();
        int mCenterX = getWidth() / 2;
        int mCenterY = getHeight() / 2;
        int x = mCenterX - mWidth / 2;
        int y = mCenterY - mHeight / 2;

        canvas.save();
        camera.save();
        canvas.translate(mCenterX, mCenterY);
        canvas.rotate(-degreeZ);
        camera.rotateY(rightDegreeY);
        camera.applyToCanvas(canvas);
        canvas.clipRect(0, -mCenterY, mCenterX, mCenterY);
        canvas.rotate(degreeZ);
        canvas.translate(-mCenterX, -mCenterY);
        camera.restore();
        canvas.drawBitmap(bitmap, x, y, paint);
        canvas.restore();

        canvas.save();
        camera.save();
        canvas.translate(mCenterX, mCenterY);
        canvas.rotate(-degreeZ);
        camera.rotateY(leftDegreeY);
        camera.applyToCanvas(canvas);
        canvas.clipRect(-mCenterX, -mCenterY, 0, 0 + y);
        canvas.rotate(degreeZ);
        canvas.translate(-mCenterX, -mCenterY);
        camera.restore();
        canvas.drawBitmap(bitmap, x, y, paint);
        canvas.restore();

    }

    @SuppressWarnings("unused")
    public void setRightDegreeY(float degreeY) {
        this.rightDegreeY = degreeY;
        invalidate();
    }

    @SuppressWarnings("unused")
    public void setDegreeZ(float degreeZ) {
        this.degreeZ = degreeZ;
        invalidate();
    }

    @SuppressWarnings("unused")
    public void setLeftDegreeY(float degreeY) {
        this.leftDegreeY = degreeY;
        invalidate();
    }
}
复制代码

PS:学习并参考的扔物线大神的资料 juejin.im/post/598363…

转载于:https://juejin.im/post/5a27b7745188253edc7fcbf0

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值