实现高端的自定义View属性动画自定义圆改变颜色进行下平移


//首先创建一个CircleView类


import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.TypeEvaluator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;




public class CircleView extends View{/**
 * 定义画笔
 */
    private Paint paint;

    private Point currentPoint;

    public static final float RADIUS = 50f;


    private circleColor currentColor;

    public CircleView(Context context) {
        super(context);
    }

    public CircleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        paint = new Paint();
    }

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

    /**
     * 定义原点类
     */
    public class Point{
        private float x;
        private float y;
        public Point(float x,float y){
            this.x=x;
            this.y=y;
        }
        public float getX(){
            return x;
        }
        public float getY(){
            return y;
        }
    }

    public class circleColor{
        private int color;

        public circleColor(int color) {
            this.color = color;
        }

        public int getColor() {
            return color;
        }

        public void setColor(int color) {
            this.color = color;
        }
    }

    /**
     * 在自定义模式里计算从开始到结束 原点的差值
     */
    class PointEvalustor implements TypeEvaluator {
        /**
         *   值 = 开始值 + 分段 * (结束值 - 开始值)
         * @param fraction  部分
         * @param startValue  开始值
         * @param endValue  结束值
         * @return  返回原点类对象
         */
        @Override
        public Object evaluate(float fraction, Object startValue, Object endValue) {
            Point startPoint= (Point) startValue;
            Point endPoint= (Point) endValue;

            float x=startPoint.getX()+fraction*(endPoint.getX()-startPoint.getX());
            float y=startPoint.getY()+fraction*(endPoint.getY()-startPoint.getY());

            return new Point(x,y);
        }
    }

    class ColorEvalustor implements TypeEvaluator{

        int count;
        @RequiresApi(api = 26)
        @Override
        public Object evaluate(float v, Object o, Object t1) {
            circleColor starColor = (circleColor) o;
            circleColor endColor = (circleColor) t1;
            int alpha = (int)(Color.alpha(starColor.getColor())+v*(Color.alpha(endColor.getColor())-Color.alpha(starColor.getColor())));
            int red = (int)(Color.red(starColor.getColor())+v*(Color.red(endColor.getColor())-Color.red(starColor.getColor())));
            int green = (int)(Color.green(starColor.getColor())+v*(Color.green(endColor.getColor())-Color.green(starColor.getColor())));
            int blue = (int)(Color.blue(starColor.getColor())+v*(Color.blue(endColor.getColor())-Color.blue(starColor.getColor())));


            return new circleColor(Color.argb(alpha,red,green,blue));
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(currentPoint == null){
            currentColor = new circleColor(Color.BLUE);
            paint.setColor(currentColor.getColor());
            currentPoint = new Point(RADIUS,RADIUS);
            canvas.drawCircle(currentPoint.getX(),currentPoint.getY(),RADIUS,paint);
            //设置动画效果的方法
            startAnim();
        }else{
            paint.setColor(currentColor.getColor());
            canvas.drawCircle(currentPoint.getX(),currentPoint.getY(),RADIUS,paint);
        }
    }

    /**
     * 设置动画效果的方法
     */
    private void startAnim() {
        Point startPoint = new Point(RADIUS,RADIUS);
        Point endPoint = new Point(getWidth()-RADIUS,getHeight()-RADIUS);
        ValueAnimator valueAnimator = ValueAnimator.ofObject(new PointEvalustor(), startPoint, endPoint);
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                if(valueAnimator != null){
                    currentPoint = (Point) valueAnimator.getAnimatedValue();
                    invalidate();
                }
            }
        });

        ValueAnimator value2 = ValueAnimator.ofObject(new ColorEvalustor(), new circleColor(Color.RED),
                new circleColor(Color.YELLOW),new circleColor(Color.BLUE),new circleColor(Color.GREEN));
        value2.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator valueAnimator) {
                currentColor = (circleColor) valueAnimator.getAnimatedValue();
                invalidate();
            }
        });

        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(valueAnimator).with(value2);
        animatorSet.setDuration(5000);
        animatorSet.start();

        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                //动画结束后  跳转至下一页面
              //  getContext().startActivity(new Intent(getContext(), MainActivity.class));
            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }
        });

    }
}
 
//然后在你的main布局当中加入你所创建的类去实现
 
<demo01.com.xiaoyuandian.CircleView
    android:id="@+id/circleView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

//然而自己的mainActivity什么也不用写 直接运用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值