android自定义渐变进度条

项目中需要用到一个弧形渐变的进度条,通过android自带是不能实现的,我是没有找到实现的方法,有大神知道的可以指点,效果图是下面这样的

这是通过继承VIew来绘制出来的,网上也有类似的,但是代码那是相当的累赘,而且创建了很多无用的对象,给内存管理带来负担    
我在这把自定义的View代码贴出来了,用到的话可以加以参考
public class SpringProgressView extends View {

	/**
	 * 分段颜色
	 */

	private static final int[] SECTION_COLORS = {Color.RED, Color.parseColor("#ffa000"), Color.YELLOW};

	/**
	 * 进度条最大值
	 */
	private float maxCount;
	/**
	 * 进度条当前值
	 */
	private float currentCount;
	/**
	 * 画笔
	 */
	private Paint mPaint;
	private int mWidth, mHeight;

	private RectF rectBg = new RectF();
	private RectF rectProgressBg = new RectF();
	private LinearGradient shader;

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

	public SpringProgressView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	public SpringProgressView(Context context) {
		super(context);
		initView(context);
	}

	private void initView(Context context) {
		mPaint = new Paint();
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		super.onLayout(changed, left, top, right, bottom);
		mHeight = bottom - top;
		mWidth = right - left;
		rectBg.set(0, 0,mWidth, mHeight);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		float section = currentCount / maxCount;
		if (shader == null) {
			shader = new LinearGradient(0, 0, mWidth, mHeight, SECTION_COLORS, null, Shader.TileMode.CLAMP);
		}
		mPaint.setShader(shader);
		mPaint.setAntiAlias(true);
		mPaint.setStyle(Paint.Style.STROKE);

		//绘制进度条外侧边框
		int round = mHeight*2/3;
		canvas.drawRoundRect(rectBg, round, round, mPaint);

		//绘制进度条
		mPaint.setStyle(Paint.Style.FILL);
		int pl=(int)(mWidth*(1-section));
		rectProgressBg.set(pl, 0, mWidth, mHeight);
		canvas.drawRoundRect(rectProgressBg, round, round, mPaint);
	}

	/*
     * 设置最大的进度值
     */
	public void setMaxCount(float maxCount) {
		this.maxCount = maxCount;
	}

	/**
	 * 设置当前的进度值
	 */
	public void setCurrentCount(float currentCount) {
		this.currentCount = currentCount > maxCount ? maxCount : currentCount;
		invalidate();
	}

	public float getMaxCount() {
		return maxCount;
	}

	public float getCurrentCount() {
		return currentCount;
	}
}

                                                               
 
 
 
 
 
     以上就是自定义的view部分,直接在布局文件中引用就可以了,在Activity中设置该进度条的最大值和和当前进度值,就可以完美实现了 

    我也把代码地址粘下来,可以下载http://download.csdn.net/detail/u013122144/9495668

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值