1.在values下面新建一个attrs.xml,现在里面定义我们的自定义属性,

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="RoundProgressBar"><attr name="roundColor" format="color"></attr><attr name="roundProgressColor" format="color"></attr><attr name="roundWidth" format="dimension"></attr><attr name="textColor" format="color"></attr><attr name="textSize" format="dimension"></attr><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>

2、创建一个customView 自定义view类

package com.example.customprogress;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.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;public class RoundProgressBar extends View
{private Paint mPaint; //画笔private int roundColor; //圆环的颜色private int roundProgressColor; //圆环进度的颜色private int textColor; //百分比字符串的颜色private float textSize; //百分比字体的大小private float roundWidth;//圆环的宽度private int max;//最大进度private int progerss;//当前进度private boolean textIsDisplayable; //是否显示private int style;//进度风格 public static final int STROKE = 0;public static final int FILL = 1;//第一步重写所以构造方法public RoundProgressBar(Context context){super(context,null);// TODO Auto-generated constructor stub
    }public RoundProgressBar(Context context, AttributeSet attrs){super(context, attrs);//创建画笔对象mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);// 第二步   初始化自定义属性
        initAttrs(context,attrs);}public RoundProgressBar(Context context, AttributeSet attrs,int defStyleAttr){super(context, attrs, defStyleAttr);}//第三步  重写onDraw方法
    @Overrideprotected void onDraw(Canvas canvas){        super.onDraw(canvas);//画圆环int center = getWidth()/2; //获取圆形的x坐标int radius =(int)(center-roundWidth/2); //圆环的半径mPaint.setColor(roundColor);//设置圆环的颜色mPaint.setStyle(Paint.Style.STROKE);//空心mPaint.setStrokeWidth(roundWidth);//宽度//画出圆环
        canvas.drawCircle(center, center, radius, mPaint);//画进度百分比mPaint.setStrokeWidth(0);mPaint.setColor(textColor);mPaint.setTextSize(textSize);mPaint.setTypeface(Typeface.DEFAULT_BOLD);//设置字体        //计算中间的进度百分比,先转换成float在进行除法运算,不然都为0  int percent = (int)(((float)progerss/ (float)max) * 100);float textWidth = mPaint.measureText(percent+"%");//获取字体宽度if(textIsDisplayable&&percent!=0&&style==STROKE){//画出中间进度值canvas.drawText(percent+"%",center-textWidth/2, center+textSize/2, mPaint);}//画圆弧
        mPaint.setStrokeWidth(roundWidth);mPaint.setColor(roundProgressColor);//圆弧进度颜色
RectF oval = new RectF(center-radius, center-radius,center+radius, center+radius);switch (style){case STROKE:mPaint.setStyle(Paint.Style.STROKE);canvas.drawArc(oval,0,360*progerss/max, false,mPaint);//根据进度画圆弧break;case FILL://填充的圆
            mPaint.setStyle(Paint.Style.FILL_AND_STROKE);if(progerss!=0){canvas.drawArc(oval, 0, 360*progerss/max,true, mPaint);}break;}}public synchronized int getMax(){return max;}public synchronized void setMax(int max){if(max<0){throw new IllegalArgumentException("max not less than 0");}this.max = max;}public synchronized int getProgress(){return progerss;}public synchronized void setProgress(int progress){if(progress<0){throw new IllegalArgumentException("progress not less than 0");}if(progress>max){this.progerss = max;}if(progress<max){this.progerss = progress;}postInvalidate();//刷新界面调用postInvalidate()能在非UI线程中刷新
    }public int getCricleColor() {  return roundColor;  }  public void setCricleColor(int cricleColor) {  this.roundColor = cricleColor;  }  public int getCricleProgressColor() {  return roundProgressColor;  }  public void setCricleProgressColor(int cricleProgressColor) {  this.roundProgressColor = cricleProgressColor;  }  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 float getRoundWidth() {  return roundWidth;  }  public void setRoundWidth(float roundWidth) {  this.roundWidth = roundWidth;  }  //初始化自定义属性private void initAttrs(Context context,AttributeSet attrs){//获取TypedArray 对象   得到自定义属性TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.RoundProgressBar);//初始化属性roundColor = typedArray.getColor(R.styleable.RoundProgressBar_roundColor,Color.RED );roundProgressColor = typedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor,Color.GREEN);textColor = typedArray.getColor(R.styleable.RoundProgressBar_textColor,Color.GREEN);textSize = typedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);roundWidth = typedArray.getDimension(R.styleable.RoundProgressBar_roundWidth,5);max = typedArray.getInteger(R.styleable.RoundProgressBar_max,100);textIsDisplayable = typedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable,true);style = typedArray.getInt(R.styleable.RoundProgressBar_style,0);//一定要注意  用完TypedArray 对象 要回收
        typedArray.recycle();}}

3、在布局文件中使用自定义View

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:android_custom="http://schemas.android.com/apk/res/com.example.customprogress"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"tools:context=".MainActivity" >  <com.example.customprogress.RoundProgressBar android:id="@+id/custom_progress"android:layout_width="80dp"android:layout_height="80dp"   android_custom:roundColor="#4f5f6f"android_custom:textColor="#9a32cd"android_custom:roundProgressColor="#f00"android_custom:textIsDisplayable="true"android_custom:roundWidth="10dp"android_custom:textSize="18sp"android_custom:style="STROKE"      /></LinearLayout>

转载于:https://www.cnblogs.com/pbq-dream/p/5399961.html

自定义View 进度条相关推荐

  1. Android 自定义view进度条

    创建自定义view继承view package com.example.jindu.ui.weight;import android.content.Context; import android.g ...

  2. android新闻项目、饮食助手、下拉刷新、自定义View进度条、ReactNative阅读器等源码...

    Android精选源码 Android仿照36Kr官方新闻项目课程源码 一个优雅美观的下拉刷新布局,众多样式可选 安卓版本的VegaScroll滚动布局 android物流详情的弹框 健身饮食记录助手 ...

  3. Android 自定义View,自定义属性--自定义圆形进度条(整理)

    很多的时候,系统自带的View满足不了我们的功能需求,那么我们就需要自定义View来满足我们的需求 自定义View时要先继承View,添加类的构造方法,重写父类View的一些方法,例如onDraw,为 ...

  4. Java渐变进度条_Android ProgressBar自定义图片进度,自定义渐变色进度条

    java.lang.Object ↳android.view.View ↳android.widget.ProgressBar 直接子类 AbsSeekBar 间接子类 RatingBar, Seek ...

  5. Android自定义圆形进度条

    Android自定义圆形进度条 github地址:https://github.com/opq1289/CircleProgressView 效果图: 无动画: 有动画: 整圆: 切割圆: 具体步骤: ...

  6. android绘制环形进度_Android动态自定义圆形进度条

    这篇文章主要介绍了Android动态自定义圆形进度条,需要的朋友可以参考下 效果图: A.绘制圆环,圆弧,文本 //1.画圆环 //原点坐标 float circleX = width / 2; fl ...

  7. android自定义带进度条的圆形图片

    前言:在项目听新闻的改版中需要实现环绕圆形新闻图片的进度条功能,作为技术预备工作我就去看了一些网上的相关的原理,做了一个自定义带进度条的圆形图片的demo,并将这个实现写成文章发布出来,谁需要了可以进 ...

  8. Android自定义半圆进度条 半圆渐变色进度条带指示 半圆开口大小可自由修改

    Android自定义半圆进度条 半圆渐变色进度条带指示 半圆开口大小可自由修改 首先我们来看下效果图 不同的开口大小只需要修改一个参数即可 半圆1: 半圆2: 半圆3: 如果是你想要的效果,就直接滑动 ...

  9. 自定义圆形进度条 自定义倒计时进度条

    自定义圆形进度条 自定义倒计时进度条 版权声明:转载必须注明本文转自严振杰的博客: http://blog.csdn.net/yanzhenjie1003 此控件源码已开源到Github:https: ...

最新文章

  1. Java在linux新建png_教你如何使用libpng显示PNG图片
  2. linux下编译动态和静态链接库
  3. 如何理解文件存取单位 -- 块(block)
  4. java脏字过滤_脏字过滤
  5. Python学习【第1篇】:Python简介以及入门
  6. 20135310陈巧然家庭作业汇总[3.56 3.67 6.23 6.39.6.40 6.41]
  7. RecycleView的Item Animator动画
  8. python 多线程下载_Python 多线程下载器
  9. visio连接线设置
  10. 大学毕业生,关于转正定级和干部身份你懂吗?
  11. 三原色图(最小生成树 kruskal)
  12. React中使用worker线程
  13. 腾讯手机充值小程序/话费充值/指定充值号码
  14. 一些植物查询的网站链接
  15. AMiner背后的技术细节与挑战
  16. 3d设计计算机配置,专业设计师选什么电脑配置?2018年专业3D建模渲染电脑配置推荐(2)...
  17. Chrome 55 浏览器推出 Android 离线下载功能
  18. 新买的电脑,设置电脑
  19. 微信公众平台开发之微商城
  20. c语言blackjack设计思路,Veriog——简易的BlackJack(21点)程序

热门文章

  1. Spring AOP / AspectJ AOP 的区别?
  2. 后端技术:IDEA构建maven项目生成的文件详解
  3. 电脑技巧:键盘失灵怎么办?
  4. Syncd - 开源自动化部署工具
  5. c++编写托管dll_教程:如何编写简单的网站并免费托管
  6. 远程控制工具_不要让您的工具控制您
  7. 如何创建和谐的色彩系统
  8. 效率神器!UI 稿智能转换成前端代码
  9. 资本冬天已至,开发者却可以着眼未来
  10. 2018年开发者生态体系状态调查报告(第一部分)