项目中须要用到一个弧形渐变的进度条,通过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