標籤:mode ida article err raw androi alt ide str
項目中須要用到一個弧形漸層的進度條,通過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();}@Overrideprotected 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);}@Overrideprotected 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
android自己定義漸層進度條