Android自定义圆形加载框

效果图如下:

下载链接:https://download.csdn.net/download/wuqingsen1/12320180

XCRoundProgressBar 中的代码为:


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;

/**
 * wuqingsen on 2020-02-25
 * Mailbox:1243411677@qq.com
 * annotation:自定义圆环加载
 */
public class XCRoundProgressBar extends View {

    private Paint paint;//画笔对象的引用
    private int roundColor;//圆环的颜色
    private int roundProgressColor;//圆环进度的颜色
    private int innerRoundColor;//圆环内部圆颜色
    private float roundWidth;//圆环的宽度
    private int textColor;//中间进度百分比字符串的颜色
    private float textSize;//中间进度百分比字符串的字体
    private int max;//最大进度
    private int progress;//当前进度
    private boolean isDisplayText;//是否显示中间百分比进度字符串
    private int style;//进度条的风格:空心圆环或者实心圆环
    private static final int STROKE = 0;//空心
    private static final int FILL = 1;//实心

    public XCRoundProgressBar(Context context) {
        this(context, null);
    }

    public XCRoundProgressBar(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public XCRoundProgressBar(Context context, AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub

        paint = new Paint();
        //从attrs.xml中获取自定义属性和默认值
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.XCRoundProgressBar);
        roundColor = typedArray.getColor(R.styleable.XCRoundProgressBar_roundColor, Color.WHITE);//外边框的颜色
        roundProgressColor = typedArray.getColor(R.styleable.XCRoundProgressBar_roundProgressColor, Color.WHITE);//加载进度颜色
        innerRoundColor = typedArray.getColor(R.styleable.XCRoundProgressBar_innerRoundColor, Color.parseColor("#00000000"));//内部加载框颜色
        roundWidth = typedArray.getDimension(R.styleable.XCRoundProgressBar_roundWidth, 5);//圆环宽度
        textColor = typedArray.getColor(R.styleable.XCRoundProgressBar_textColor, Color.parseColor("#FD7500"));//字体颜色
        textSize = typedArray.getDimension(R.styleable.XCRoundProgressBar_textSize, 18);//字体大小
        max = typedArray.getInteger(R.styleable.XCRoundProgressBar_max, 100);//最大
        style = typedArray.getInt(R.styleable.XCRoundProgressBar_style, FILL);//0空心,1实心
        isDisplayText = typedArray.getBoolean(R.styleable.XCRoundProgressBar_textIsDisplayable, true);
        typedArray.recycle();

    }

    @Override
    protected void onDraw(Canvas canvas) {
        // TODO Auto-generated method stub
        super.onDraw(canvas);

        //画最外层大圆环
        int centerX = getWidth() / 2;//获取中心点X坐标
        int certerY = getHeight() / 2;//获取中心点Y坐标
        int radius = (int) (centerX - roundWidth / 2);//圆环的半径
        paint.setColor(roundColor);
        paint.setStyle(Paint.Style.STROKE);//设置空心
        paint.setStrokeWidth(roundWidth);//设置圆环宽度
        paint.setAntiAlias(true);//消除锯齿
        canvas.drawCircle(centerX, certerY, radius, paint);//绘制圆环

        //绘制圆环内部圆
        paint.setColor(innerRoundColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
        canvas.drawCircle(centerX, certerY, radius - roundWidth / 2, paint);


        //画进度
        paint.setStrokeWidth(roundWidth);//设置圆环宽度
        paint.setColor(roundProgressColor);//设置进度颜色
        RectF oval = new RectF(centerX - radius, centerX - radius, centerX
                + radius, centerX + radius);  //用于定义的圆弧的形状和大小的界限
        switch (style) {
            case STROKE: {
                paint.setStyle(Paint.Style.STROKE);
                canvas.drawArc(oval, 0, 360 * progress / max, false, paint); // 根据进度画圆弧
                break;
            }
            case FILL: {
                paint.setStyle(Paint.Style.FILL);
                if (progress != 0)
                    canvas.drawArc(oval, -90, 360 * progress / max, true, paint); // 根据进度画圆弧
                break;
            }
        }
        //画中间进度百分比字符串
        paint.setStrokeWidth(0);
        paint.setColor(textColor);
        paint.setTextSize(textSize);
//        paint.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        int percent = (int) (((float) progress / (float) max) * 100);//计算百分比
        float textWidth = paint.measureText(percent + "%");//测量字体宽度,需要居中显示

        if (isDisplayText && style == STROKE && percent != 0) {
            canvas.drawText(percent + "%", centerX - textWidth / 2, centerX + textSize / 2, paint);
        }


    }

    public Paint getPaint() {
        return paint;
    }

    public void setPaint(Paint paint) {
        this.paint = paint;
    }

    public int getRoundColor() {
        return roundColor;
    }

    public void setRoundColor(int roundColor) {
        this.roundColor = roundColor;
    }

    public int getRoundProgressColor() {
        return roundProgressColor;
    }

    public void setRoundProgressColor(int roundProgressColor) {
        this.roundProgressColor = roundProgressColor;
    }

    public float getRoundWidth() {
        return roundWidth;
    }

    public void setRoundWidth(float roundWidth) {
        this.roundWidth = roundWidth;
    }

    public int getTextColor() {
        return textColor;
    }

    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    public float getTextSize() {
        return textSize;
    }

    public void setTextSize(float textSize) {
        this.textSize = textSize;
    }

    public synchronized int getMax() {
        return max;
    }

    public synchronized void setMax(int max) {
        if (max < 0) {
            throw new IllegalArgumentException("max must more than 0");
        }
        this.max = max;
    }

    public synchronized int getProgress() {
        return progress;
    }

    /**
     * 设置进度,此为线程安全控件,由于考虑多线的问题,需要同步
     * 刷新界面调用postInvalidate()能在非UI线程刷新
     *
     * @author caizhiming
     */
    public synchronized void setProgress(int progress) {
        if (progress < 0) {
            throw new IllegalArgumentException("progress must more than 0");
        }
        if (progress > max) {
            this.progress = progress;
        }
        if (progress <= max) {
            this.progress = progress;
            postInvalidate();
        }
    }

    public boolean isDisplayText() {
        return isDisplayText;
    }

    public void setDisplayText(boolean isDisplayText) {
        this.isDisplayText = isDisplayText;
    }

    public int getStyle() {
        return style;
    }

    public void setStyle(int style) {
        this.style = style;
    }
}

 

values 添加 attrs.xml ,代码为:

<resources>
    <!--圆弧进度条-->
    <declare-styleable name="CircularProgressView">
        <attr name="backWidth" format="dimension" />    <!--背景圆环宽度-->
        <attr name="progWidth" format="dimension" />    <!--进度圆环宽度-->
        <attr name="backColor" format="color" />        <!--背景圆环颜色-->
        <attr name="progColor" format="color" />        <!--进度圆环颜色-->
        <attr name="progStartColor" format="color" />   <!--进度圆环开始颜色-->
        <attr name="progFirstColor" format="color" />   <!--进度圆环结束颜色-->
        <attr name="progress" format="integer" />       <!--圆环进度-->
    </declare-styleable>
    <declare-styleable name="XCRoundProgressBar">
        <attr name="roundColor" format="color"/>
        <attr name="roundProgressColor" format="color"/>
        <attr name="roundWidth" format="dimension"></attr>
        <attr name="innerRoundColor" format="color" />
        <attr name="textColor" format="color" />
        <attr name="textSize" format="dimension" />
        <attr name="max" format="integer"></attr>
        <attr name="textIsDisplayable" format="boolean"></attr>
        <attr name="style">
            <enum name="STROKE" value="0"></enum>
            <enum name="FILL" value="1"></enum>
        </attr>
    </declare-styleable>

</resources>

 

MainActivity 代码为:

package com.wu.circularloadwu;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    Button btn_start;
    XCRoundProgressBar progressbar1, progressbar2, progressbar3;
    private Timer timer;
    int myProgress = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn_start = findViewById(R.id.btn_start);
        progressbar1 = findViewById(R.id.progressbar1);
        progressbar2 = findViewById(R.id.progressbar2);
        progressbar3 = findViewById(R.id.progressbar3);
        setProgressbar();

        timer = new Timer(true);

        btn_start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                timer.schedule(timerTask, 0, 50); //延时1000ms后执行,1000ms执行一次
                btn_start.setClickable(false);
            }
        });
    }

    //设置样式
    private void setProgressbar() {
        //风格1
        progressbar1.setMax(100);//最大
        progressbar1.setStyle(1);//0空心,1实心
        progressbar1.setRoundColor(Color.WHITE);//圆环颜色
        progressbar1.setRoundWidth(5);//圆环宽度
        progressbar1.setRoundProgressColor(Color.WHITE);//圆环进度颜色
        progressbar1.setDisplayText(false);//是否显示中间文字
        //风格2
        progressbar2.setMax(100);//最大
        progressbar2.setStyle(0);//0空心,1实心
        progressbar2.setRoundColor(Color.WHITE);//圆环颜色
        progressbar2.setRoundWidth(5);//圆环宽度
        progressbar2.setRoundProgressColor(Color.RED);//圆环进度颜色
        progressbar2.setDisplayText(false);//是否显示中间文字
        progressbar2.setTextSize(18);//中间文字大小
        progressbar2.setTextColor(Color.RED);//中间文字颜色
        //风格3
        progressbar3.setMax(100);//最大
        progressbar3.setStyle(0);//0空心,1实心
        progressbar3.setRoundColor(Color.WHITE);//圆环颜色
        progressbar3.setRoundWidth(5);//圆环宽度
        progressbar3.setRoundProgressColor(Color.RED);//圆环进度颜色
        progressbar3.setDisplayText(true);//是否显示中间文字
        progressbar3.setTextSize(20);//中间文字大小
        progressbar3.setTextColor(Color.BLACK);//中间文字颜色
    }

    TimerTask timerTask = new TimerTask() {
        public void run() {
            Message message = new Message();
            message.what = 1;
            handler.sendMessage(message);
        }
    };

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            if (msg.what == 1) {
                //回到主线程执行结束操作
                myProgress++;
                if (myProgress <= 100) {
                    progressbar1.setProgress(myProgress);
                    progressbar2.setProgress(myProgress);
                    progressbar3.setProgress(myProgress);
                } else {
                    timer.cancel();
                }
            }
        }
    };
}

 

activity_main.xml 文件代码为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/btn_start"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="开始" />

    <com.wu.circularloadwu.XCRoundProgressBar
        android:id="@+id/progressbar1"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_margin="10dp"/>

    <com.wu.circularloadwu.XCRoundProgressBar
        android:id="@+id/progressbar2"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_margin="10dp"/>

    <com.wu.circularloadwu.XCRoundProgressBar
        android:id="@+id/progressbar3"
        android:layout_width="36dp"
        android:layout_height="36dp"
        android:layout_margin="10dp"/>

</LinearLayout>

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值