欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

文章标题 圆形进度条(内部显示百分数)

程序员文章站 2022-03-04 11:59:26
...

CustomProgrssView控件
package shidongliang.bwei.com.huatudemo.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

/**
 * 此类的作用:
 *
 * @author: forever
 * @date: 2017/11/1 15:05
 */
public class CustomProgrssView extends View {
    //定义个画笔

    private int progress=0;
    private boolean runing=true;
    private Paint paint;


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

    public CustomProgrssView(Context context, 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("progrss="+progress);
                    progress+=10;
                    //子线程刷新 系统调用onDraw方法
                    postInvalidate();
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();

    }

    public CustomProgrssView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //获得view的宽和高
        int x=getWidth()/2;
        int y=getHeight()/2;
        int radius=200;
        //设置画笔的粗细
        paint.setStrokeWidth(5);
        //定义区域
        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);
    }
}
布局-------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="shidongliang.bwei.com.huatudemo.MainActivity">

<shidongliang.bwei.com.huatudemo.view.CustomProgrssView
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
</LinearLayout>
相关标签: 控件