android自己定義漸層進度條

來源:互聯網
上載者:User

標籤: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自己定義漸層進度條

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.