自定义View-签到(二)

1.当我们点击签到,也会有些动画效果,就比如下面这种,从开始位置沿着一个path运动到目标位置,然后签到成功。

自定义View-签到(二)_第1张图片自定义View-签到(二)_第2张图片自定义View-签到(二)_第3张图片

 

 

2.实现代码

public void btn_sign(final View view) {

        view.setClickable(false);
        generateCoin.setVisibility(View.VISIBLE);

        //获取控件当前位置
        final int[] startLoc = new int[2];
        generateCoin.getLocationInWindow(startLoc);

        //获取目标位置
        int[] targetLoc = new int[2];
        targetCoin.getLocationInWindow(targetLoc);

        path.reset();
        path.moveTo(startLoc[0],startLoc[1]);
        path.cubicTo(2*(startLoc[0]+targetLoc[0])/3,2*(startLoc[1]+targetLoc[1])/3,(startLoc[0]+targetLoc[0])/3,(startLoc[1]+targetLoc[1])/3,targetLoc[0],targetLoc[1]);

        pathMeasure = new PathMeasure(path, false);

        ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, pathMeasure.getLength());

        valueAnimator.setDuration(1000);
        valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());

        valueAnimator.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                view.setClickable(true);
                generateCoin.setVisibility(View.INVISIBLE);
                generateCoin.setX(startLoc[0]);
                generateCoin.setY(startLoc[1]);

                startAddCoin();
            }
        });
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();

                //按照path,移动view到目标位置
                pathMeasure.getPosTan(value, mCurrentPosition, null);
                generateCoin.setX(mCurrentPosition[0]);
                generateCoin.setY(mCurrentPosition[1]);
            }
        });

        valueAnimator.start();

    }

    private void startAddCoin() {
        ObjectAnimator animator1 = ObjectAnimator.ofFloat(targetCoin, "rotationY", 0, 720);

        animator1.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                super.onAnimationEnd(animation);

                coinCount.setText(String.valueOf(Integer.parseInt((String) coinCount.getText())+12));
            }
        });
        animator1.setDuration(800).start();
    }

 

你可能感兴趣的:(Android自定义View)