自定义view绘制圆形里面带个勾的动画

看了点自定义view的入门,关于view到底是如何动画画出来的,先上代码

public class DrawHookView extends View {
    public int xiankuan = 5;
    //绘制圆弧的进度值
    private int progress = 0;
    //线1的x轴
    private int line1_x = 0;
    //线1的y轴
    private int line1_y = 0;
    //线2的x轴
    private int line2_x = 0;
    //线2的y轴
    private int line2_y = 0;



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

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

    public DrawHookView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    //绘制


    //    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.e("-->", "循环来了" + progress);
        progress++;

        /**
         * 绘制圆弧
         */
        Paint paint = new Paint();
        //设置画笔颜色
        paint.setColor(getResources().getColor(R.color.arc_blue));
        //设置圆弧的宽度
        paint.setStrokeWidth(xiankuan);
        //设置圆弧为空心
        paint.setStyle(Paint.Style.STROKE);
        //消除锯齿
        paint.setAntiAlias(true);

        //获取圆心的x坐标
        int center = getWidth() / 2;
        //圆弧半径
        int radius = getWidth() / 2 - 5;




        Log.e("-->-->1:", (center - radius) + "");
        Log.e("-->-->2:", (center + radius) + "");
        //定义绘制的起始x,y  结束x,y,
        // 需要注意,线的宽度是算在外面的,线的宽度是算在外面的,从内往外扩充,所以减掉线的宽度(也就是圆形的的粗细)
        RectF rectF = new RectF(xiankuan, xiankuan, center - xiankuan, center - xiankuan);

        //根据进度画圆弧
        canvas.drawArc(rectF, 100, -360 * progress / 100, false, paint);
        Log.e("-->hudu", (-360 * progress / 100) + "");

//        startAngle: 圆弧起始角度,单位为度。
//        sweepAngle: 圆弧扫过的角度,顺时针方向,单位为度。 (注意:::这里的正负用来设置绘制的方法,弧线坐标到右边或者右边到左边)
//        useCenter: 如果为True时,在绘制圆弧时将圆心包括在内,通常用来绘制扇形。
//        paint: 绘制圆弧的画板属性,如颜色,是否填充等。

        /**
         * 绘制对勾
         */
        //先等圆弧画完,才话对勾
        if(progress >= 100) {
            if(line1_x < radius / 3) {
                line1_x++;
                line1_y++;
            }
            //画第一根线
            canvas.drawLine(center1, center, center1 + line1_x, center + line1_y, paint);

            if (line1_x == radius / 3) {
                line2_x = line1_x;
                line2_y = line1_y;
                line1_x++;
                line1_y++;
            }
            if (line1_x >= radius / 3 && line2_x <= radius) {
                line2_x++;
                line2_y--;
            }
            //画第二根线
            canvas.drawLine(center1 + line1_x - 1, center + line1_y, center1 + line2_x, center + line2_y, paint);
        }

        //每隔10毫秒界面刷新
        postInvalidateDelayed(100);
    }

}

color的资源文件自行加入就行了 ,或者随便改个颜色都行,

所谓动画,就是通过

 postInvalidateDelayed(100);

这个方法会间隔100毫秒,反复调用OnDraw(),

然后在onDRAW通过个别参数来实现动画,

例如这个demo,里面有一个进度值propess,

通过这个进度值,算出来圆形的角度,(例如第一次绘制,0~30度, 第二次 0~60度,如此快速刷新界面),

看着就好像动画一样了 。

那个圆的绘制看懂了,那个勾勾设计到算坐标点,有时间再去研究,记个笔记。


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值