自定义view 动态圆形有百分比

public class CustomProgrssView extends View{

    //定义画笔
    private Paint paint;
    private int progress = 0;
    private boolean runing = true ;


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

    public CustomProgrssView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);

        //创建一个画笔
        paint =new Paint();
        //抗锯齿
        paint.setAntiAlias(true);
        //设置画笔颜色
        paint.setColor(Color.RED);
        //定义圆是空心的
        paint.setStyle(Paint.Style.STROKE);



        new Thread(new Runnable() {
            @Override
            public void run() {

                while (runing){


                    if(progress >= 360){
                        runing = false;
                        return;
                    }
                    System.out.println("progress = " + progress);
                    progress += 10 ;

                    //子线程刷新 系统调用onDraw() 方法
                    postInvalidate();

                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }
        }).start();



    }


    float sweep ;



    public CustomProgrssView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }
    //绘画控件
    public void onDraw(Canvas canvas){
        super.onDraw(canvas);


        int x=getWidth()/2;
        int y=getHeight()/2;

        int radius=200;

        //设置画笔的粗细
        paint.setStrokeWidth(30);
        //定义一个区域
        RectF rectF = new RectF(x-radius,y-radius,x+radius,y+radius);

        canvas.drawArc(rectF,-90,progress,false,paint);

        int text = (int) ((float)progress / 360  * 100 );

        float textWidth =  paint.measureText(text+"%");
        Rect rextText = new Rect();

        paint.getTextBounds(text+"%",0,(text+"%").length(),rextText);


        paint.setTextSize(30);
        paint.setStrokeWidth(1);
        //画文字
        canvas.drawText(text+"%",x-textWidth/2,y+rextText.height()/2,paint);
    }

}
 
写一个view类
 
 
 
布局中引用
<View.CustomProgrssView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<View.CustomProgrssView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值