自定义水波纹view

丶灬走出姿态 提交于 2019-12-15 06:35:00

今天自定义个水波纹扩散 或者收缩 效果的view;先上效果

/**
 * 自定义view实现 水波纹 效果
 */
public class MoireView extends View {
    private Paint paint;
    //最大半径
    private int radius;
    //绘制半径
    private int drawRadius;
    //屏幕宽高
    private int w, h;
    //背景颜色
    private int backColor = Color.TRANSPARENT;
    //水波纹颜色
    private int moireColor = Color.WHITE;
    //动画执行时间
    private int duration = 2000;
    //是否是扩散水波纹
    private boolean isDiffusion = true;


    public MoireView(Context context) {
        super(context);
        init();
    }


    public MoireView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }


    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(moireColor);

        MyAnimation myAnimation = new MyAnimation();
        myAnimation.setDuration(duration);
        startAnimation(myAnimation);
    }

    /**
     * 动画
     */
    public class MyAnimation extends Animation {
        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            if (isDiffusion){
                //扩散水波纹效果
                drawRadius = (int) (radius * interpolatedTime);
            }else {
                //收缩水波纹效果
                drawRadius=radius-(int) (radius * interpolatedTime);

            }

            postInvalidate();
        }


    }


    /**
     * 测量
     *
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        w = MeasureSpec.getSize(widthMeasureSpec);
        h = MeasureSpec.getSize(heightMeasureSpec);
        radius = (int) Math.sqrt(w * w + h * h) / 2;

    }


    /**
     * 绘制
     *
     * @param canvas
     */
    @Override
    protected void onDraw(Canvas canvas) {
        //设置背景颜色
        canvas.drawColor(backColor);
        //绘制圆
        canvas.drawCircle(w / 2, h / 2, drawRadius, paint);


    }


}

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!