首页 > 其他分享 >Android项目实战(六十七):自定义圆形进度条

Android项目实战(六十七):自定义圆形进度条

时间:2023-11-29 16:34:57浏览次数:43  
标签:自定义 进度条 int value mPaint import Android android 圆环

圆形进度条

支持设置:

1、圆环背景颜色

2、圆管背景宽度

3、进度圆环颜色

4、进度圆环宽度

5、圆环进度

6、开始角度

7、动画执行时间

 

自定义类:

package com.example.mainactivty;

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.util.TypedValue;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Transformation;

import androidx.annotation.Nullable;

public class ProgressBarView extends View {

    private Paint mPaint; // 画笔
    private CircleBarAnim anim; // 动画
    private float progressSweepAngle;//进度条圆弧扫过的角度

    // 以下是自定义参数
    private int mAnnulusWidth; // 圆环宽度
    private int mProgressWidth; // 进度条宽度
    private int mAnnulusColor; // 圆环颜色
    private int mLoadColor; // 加载进度圆弧扫过的颜色
    private int mProgress = 0; // 当前进度
    private int maxProgress = 100; // 最大进度,默认100
    public int startAngle = -90; // 开始圆点角度

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

    public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // 获取自定义属性
        TypedArray value = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AnnulusCustomizeView, defStyleAttr, 0);
        int indexCount = value.getIndexCount();
        for (int i = 0; i < indexCount; i++) {
            int parm = value.getIndex(i);
            switch (parm) {
                case R.styleable.AnnulusCustomizeView_startAngle:
                    startAngle = value.getInt(parm, 90);
                    break;
                case R.styleable.AnnulusCustomizeView_annulusWidth:
                    mAnnulusWidth = value.getDimensionPixelSize(parm,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                    10,
                                    getResources().getDisplayMetrics()));
                    break;
                case R.styleable.AnnulusCustomizeView_progressWidth:
                    mProgressWidth = value.getDimensionPixelSize(parm,
                            (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                                    10,
                                    getResources().getDisplayMetrics()));
                    break;
                case R.styleable.AnnulusCustomizeView_annulusColor:
                    mAnnulusColor = value.getColor(parm, Color.BLACK);
                    break;
                case R.styleable.AnnulusCustomizeView_loadColor:
                    mLoadColor = value.getColor(parm, Color.BLACK);
                    break;
                case R.styleable.AnnulusCustomizeView_progress:
                    mProgress = value.getInt(parm, 10);
                    break;
            }
        }
        value.recycle();
        // 动画
        anim=new CircleBarAnim();
        mPaint = new Paint();
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // TODO:绘制圆环
        // 获取圆形坐标
        int centre = getWidth() / 2;
        // 获取半径
        int radius = centre - mAnnulusWidth / 2-10;
        // 取消锯齿
        mPaint.setAntiAlias(true);
        // 设置画笔宽度
        mPaint.setStrokeWidth(mAnnulusWidth);
        // 设置空心
        mPaint.setStyle(Paint.Style.STROKE);
        // 设置画笔颜色
        mPaint.setColor(mAnnulusColor);
        canvas.drawCircle(centre, centre, radius, mPaint);

        // TODO:画圆弧,进度
        // 获取进度条中心点
        // 进度条半径
        int progressRadius = centre - mAnnulusWidth /2-10;
        // 设置进度颜色
        mPaint.setColor(mLoadColor);
        mPaint.setStrokeWidth(mProgressWidth);
        mPaint.setStyle(Paint.Style.STROKE);
        // 用于定义的圆弧的形状和大小的界限
        RectF ovalStroke = new RectF(centre - progressRadius, centre - progressRadius,
                centre + progressRadius, centre + progressRadius);
        canvas.drawArc(ovalStroke, startAngle, progressSweepAngle, false, mPaint);
    }

    /**
     * 设置进度
     * @param progress 进度值
     * @param time 动画时间范围
     */
    public synchronized void setProgress(final int progress,int time) {
        anim.setDuration(time);
        this.startAnimation(anim);
        this.mProgress = progress;
    }

    // 动画
    public class CircleBarAnim extends Animation {
        public CircleBarAnim() {

        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            super.applyTransformation(interpolatedTime, t);
            progressSweepAngle=interpolatedTime*(mProgress * 360 / maxProgress);//这里计算进度条的比例

            postInvalidate();
        }
    }

    public synchronized int getmProgress() {
        return mProgress;
    }
}
ProgressBarView

属性值:

放在main->res->values->attr.xml文件

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">

    <attr name="annulusColor" format="color"/>//圆环颜色
    <attr name="loadColor" format="color"/>//环形进度条加载颜色
    <attr name="annulusWidth" format="dimension"/>//圆环宽度
    <attr name="progressWidth" format="dimension"/>//圆环宽度
    <attr name="startAngle" format="integer"/>//开始角度
    <attr name="progress" format="integer"/>//圆环进度
    <declare-styleable name="AnnulusCustomizeView">
        <attr name="annulusColor"/>
        <attr name="loadColor"/>
        <attr name="annulusWidth"/>
        <attr name="progressWidth"/>
        <attr name="startAngle"/>
        <attr name="progress"/>
    </declare-styleable>
</resources>
attr.xml

xml使用:

 <com.example.mainactivty.ProgressBarView
        android:id="@+id/progressBar"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:padding="20dp"
        app:annulusColor="#F1EEE7"
        android:paddingVertical="6dp"
        android:paddingHorizontal="6dp"
        app:annulusWidth="4dp"
        app:loadColor="#BE9B79"
        app:progress="0"
        app:progressWidth="6dp"
        app:startAngle="-90"
        >
    </com.example.mainactivty.ProgressBarView>
View Code

 

progressBarView.setProgress(70,2000);

 

源码参考:

https://gitee.com/xqxacm/circleProgress

 

标签:自定义,进度条,int,value,mPaint,import,Android,android,圆环
From: https://www.cnblogs.com/xqxacm/p/17865197.html

相关文章

  • 2023-11-23-idea技巧-自定义后缀补全
    Idea技巧-PostfixCompletion在idea中可以使用.xxx进行后缀补全比如.sout如何自定义后缀补全?比如.log在idea中打开设置File|Settings|Editor|General|PostfixCompletion这里定义了上面提到的sout同理点击上图+号新建一个模板,并参考sout写入对应的规则效果......
  • Android开放配件 (AOA) 协议
    一、背景 自Android3.1之后的版本,Google引入了USBAccessories的概念,并提供了相关的开发库。Android3.1之后的版本不仅可以让Android设备作为USBHost的角色支持USB鼠标、键盘、游戏手柄等,还可以以USBDevice的角色与一些具有USBHost功能,但却扮演着配件角色的设备相连,Google......
  • 一些有用的自定义函数(抄录)
    提取字符串中的数字'提取字符串中的数字FunctionGetDigits(strTextAsString)AsStringDimstrCharAsString,strMsgAsStringDimiAsLongstrMsg=""Fori=1ToLen(strText)strChar=Mid(strText,i,1)IfstrCharLike"#"The......
  • SpringBoot JPA实践之EntityManage查询返回自定义DTO entityManager.createNativeQuer
    SpringBootJPA实践之EntityManage查询返回自定义DTOentityManager.createNativeQuery(sql)  在很多时候我更喜欢随意组合查询出来返回一个DTO对象的实现,JPA提供的多数查询均以返回Entity居多,它提供的EntityManager对象可以实现将SQL语句查询的结果转换为自定义DTO对象(这与......
  • el select 选项多列,换行,表格,数据量大,全部显示,自定义el-select,el-select插入表格
    1、效果图:2、实现:自定义下拉框内容,采用radio或checkbox作为选项绑定值<template><el-selectv-model="selectValue"v-bind="$attrs"clearable><!--隐藏的option组件,展示下面的插槽--><el-optionv-show="false"/><!--......
  • Android之 看“马达”如何贯通Android系统 (从硬件设计 --> 驱动 --> HAL --> JNI -->
    Android之看“马达”如何贯通Android系统(从硬件设计-->驱动-->HAL-->JNI-->Framework-->Application)-如果天空不死-博客园https://www.cnblogs.com/skywang12345/p/3404808.html  在Android2.3(Gingerbread)系统的时候,我写过一篇关于“Android震动马达......
  • Android Compose 的分页(Paging3)
    Overview官方链接:https://developer.android.com/topic/libraries/architecture/paging/v3-overview需要注意的是,Paging库的组件在应用程序的三层中运行,Paging在三层的架构如下图:存储库层ViewModel层用户界面层在三层的数据传递如下图:方法/接口官方链接:https://dev......
  • uhttp luci cgi-bin 自定义输出内容
    uhttplucicgi-bin自定义输出内容来源  https://www.cnblogs.com/osnosn/p/17131543.html 参考【ExampleofwebinterfaceusinguHTTPdandLua】【Lua5.1ReferenceManual】openwrt,op18,op19,op21,op22都是用的lua-5.1.5。修改openwrtuhttpd使用的ssl......
  • 自定义应用层通信协议结构消息的编码方式
    应用层通信协议设计 一、应用层通信协议概述TCP/UDP是基于字节流的传输层通信协议,对于其的编程是基于IO流编程,所谓“流”,就是没有界限的一长串二进制数据。TCP/UDP作为传输层协议,并不了解上层业务数据的具体含义,它会根据TCP缓冲区的实际情况进行数据包的划分。所以在业务上......
  • springboot 自定义响应体大小测试接口
    @ResponseBody@RequestMapping("/def/response/body/service")publicStringBuilderdefResponseBodyService(@RequestParam(name="count")Integercount,HttpServletRequestHttpRequest)throwsInterruptedException{  StringbaseStr="0......