android_studio的自定义View的圆形进度条

**需要 考试第三方**
**MyView写法如下**
package com.bawei.lss_yuan;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import java.util.Timer;
import java.util.TimerTask;

public class MyView extends View{

    private float cx;
    private float cy;
    private float radius;
    private Paint paint;
    private int sweepAngle;
    private int color;

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

    public MyView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //获取自定义属性
        TypedArray a=context.obtainStyledAttributes(attrs,R.styleable.MyView);
        //获取颜色,圆的颜色
        color=a.getColor(R.styleable.MyView_circle_color, Color.BLACK);
//圆的半径        radius=a.getDimension(R.styleable.MyView_circle_radius,100);
 //圆的距离
        cx=a.getDimension(R.styleable.MyView_circle_x,260);
        cy=a.getDimension(R.styleable.MyView_circle_y,260);
        a.recycle();
        paint=new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.STROKE);
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                if(sweepAngle>360){
                    return;
                }
                sweepAngle+=1;
                postInvalidate();
            }
        },1000,20);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setColor(color);
        paint.setStrokeWidth(5);
        canvas.drawCircle(cx,cy,radius,paint);
        //进度条走动颜色
        paint.setColor(Color.RED);
        RectF rectF=new RectF(cx - radius, cy - radius, cx + radius, cy + radius);
        canvas.drawArc(rectF, -90, sweepAngle, false, paint);
        int progress= (int) (sweepAngle/360f*100);
        paint.setStrokeWidth(0);
        //百分数颜色
        paint.setColor(Color.BLACK);
        paint.setTextSize(15);
        canvas.drawText(progress+"%",cx-5,cy,paint);
    }
}
**activity_main写法如下**
"1.0" encoding="utf-8"?>
"http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.bawei.lss_yuan.MyView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

**复制values,attrs.xml的写法如下**
"1.0" encoding="utf-8"?>

    "MyView">
        "circle_color" format="color">
        "circle_radius" format="dimension">
        "circle_x" format="dimension">
        "circle_y" format="dimension">

    

你可能感兴趣的:(android_studio的自定义View的圆形进度条)