饼状圆形进度条

这是一个用Paint画图实现的进度条,仿iphone带进度的进度条,线程安全的View,可直接在线程中更新进度

先看一下效果图
​​​​​​​​在这里插入图片描述
不多说,下面上源码:

先定义一个RoundProgressBar,继承自view

public class RoundProgressBar extends View {
	/**
	 * 画笔对象的引用
	 */
	private Paint paint;
	
	/**
	 * 圆环的颜色
	 */
	private int roundColor;
	
	/**
	 * 圆环进度的颜色
	 */
	private int roundProgressColor;
	
	/**
	 * 中间进度百分比的字符串的颜色
	 */
	private int textColor;
	
	/**
	 * 中间进度百分比的字符串的字体
	 */
	private float textSize;
	
	/**
	 * 圆环的宽度
	 */
	private float roundWidth;
	
	/**
	 * 最大进度
	 */
	private int max;
	
	/**
	 * 当前进度
	 */
	private int progress;
	/**
	 * 是否显示中间的进度
	 */
	private boolean textIsDisplayable;
	
	/**
	 * 进度的风格,实心或者空心
	 */
	private int style;
	
	public static final int STROKE = 0;
	public static final int FILL = 1;
	
	public RoundProgressBar(Context context) {
		this(context, null);
	}

	public RoundProgressBar(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
//	自定义的类型attrs,在attrs中
	public RoundProgressBar(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		
		paint = new Paint();

		
		TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
				R.styleable.RoundProgressBar);
		
		//获取自定义属性和默认值
		roundColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundColor, Color.RED);
		roundProgressColor = mTypedArray.getColor(R.styleable.RoundProgressBar_roundProgressColor, Color.GREEN);
		textColor = mTypedArray.getColor(R.styleable.RoundProgressBar_textColor, Color.GREEN);
		textSize = mTypedArray.getDimension(R.styleable.RoundProgressBar_textSize, 15);
		roundWidth = mTypedArray.getDimension(R.styleable.RoundProgressBar_roundWidth, 5);
		max = mTypedArray.getInteger(R.styleable.RoundProgressBar_max, 100);
		textIsDisplayable = mTypedArray.getBoolean(R.styleable.RoundProgressBar_textIsDisplayable, true);
		style = mTypedArray.getInt(R.styleable.RoundProgressBar_style, 0);
		
		mTypedArray.recycle();
	}

还需要在res-value下写一个attrs文件,定义一些属性

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <!--
        max                 最大进度
        startprogress            初始时的进度
        startAngle             进度从圆环的哪个角度开始走
        centreColor         圆环内部的填充色
        ringColor             圆环的颜色
        ringProgressColor   圆环进度的颜色
        ringWidth             圆环宽度
        textColor             中心文字的颜色
        textSize             中心文字的大小
        textIsDisplayable     中心文字是否显示
        style = 0/1          进度的风格,实心或者空心
     -->

    <declare-styleable name="RoundProgressBar">
        <attr name="max" format="integer"></attr>
        <attr name="startprogress" format="integer"></attr>
        <attr name="startAngle" format="integer"></attr>
        <attr name="ringProgressColor" format="color"/>
        <attr name="ringWidth" format="dimension"></attr>
        <attr name="centreColor" format="color"/>
        <attr name="textColor" format="color"/>
        <attr name="textSize" format="dimension"/>
        <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>

上面是自定义的一些属性什么的,下面就是xml文件中的使用:

 <com.example.roundprogressbar.RoundProgressBar
        android:id="@+id/roundProgressBar4"
        android_custom:style="FILL"
        android:layout_width="80dip"
        android:layout_height="80dip"
        android_custom:roundWidth="1dip"
        android_custom:roundProgressColor="#C2C2C2" />

android_custom:style=“FILL” 这个设置画出的圆是空心还是实心

到此这个扇形的进度条就结束了。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值