Android 自訂學習(5)自訂Progressbar

來源:互聯網
上載者:User

標籤:自訂view   android   

自訂View學習的最後一期了,先上。



相信大家心中都有自己的實現方法,這裡就貼上我的方法以供參考。


/** * 刻度畫筆 */private Paint linePaint;/** * 進度畫筆 */private Paint textPaint;/** * 進度 */private int index = 0;/** * 刻度 */private int ruling = 0;
private void init() {linePaint = new Paint();linePaint.setColor(Color.BLACK);linePaint.setStrokeWidth(5);linePaint.setAntiAlias(true);linePaint.setStyle(Style.FILL);textPaint = new Paint(linePaint);textPaint.setTextSize(70);textPaint.setColor(Color.WHITE);handler.sendEmptyMessage(0);handler.sendEmptyMessageDelayed(1, 2000);}
上述相信大家都可以明白。。接下來就是最關鍵的onDraw()

@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//移動畫布canvas.translate(getWidth() / 2, getHeight() / 2);//填充背景canvas.drawColor(getResources().getColor(R.color.gray));//刻度總數int count = 30;//進度字佔大小Rect rect = new Rect();String indexsString = String.valueOf(index);if (index < 10) {indexsString = "0" + indexsString;}textPaint.getTextBounds(indexsString, 0, indexsString.length(), rect);//進度字型canvas.drawText(indexsString, -rect.width() / 2, rect.height() / 2,textPaint);//旋轉畫布,畫刻度,並根據ruling值來改變顏色for (int i = 0; i < count; i++) {if (i <= ruling) {linePaint.setColor(getResources().getColor(R.color.pink));} else {linePaint.setColor(Color.BLACK);}canvas.drawLine(0, 100, 0, 120, linePaint);canvas.rotate(360 / count, 0, 0);}}
注釋的應該很詳細不懂的也可以留言,接下來看看效果



完成了這個之後相信第二個也是手到擒來!


/** * 圓環 */private Paint rectPaint;/** * 刻度 */private Paint linePaint;/** * 填充 */private Paint fillPaint;/** * 進度字 */private Paint textPaint;private int index = 0;private int index2 = 0;public ProgressView2(Context context) {this(context, null);// TODO Auto-generated constructor stub}public ProgressView2(Context context, AttributeSet attrs) {this(context, attrs, 0);// TODO Auto-generated constructor stub}public ProgressView2(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);init();}

private void init() {rectPaint = new Paint();rectPaint.setAntiAlias(true);rectPaint.setStrokeWidth(20);rectPaint.setStyle(Style.STROKE);rectPaint.setColor(getResources().getColor(R.color.dark));linePaint = new Paint(rectPaint);linePaint.setStrokeWidth(20);linePaint.setColor(getResources().getColor(R.color.pink));fillPaint = new Paint(linePaint);fillPaint.setStrokeWidth(1);fillPaint.setColor(Color.BLACK);fillPaint.setAlpha(60);textPaint = new Paint();textPaint.setStrokeWidth(5);textPaint.setAntiAlias(true);textPaint.setTextSize(70);textPaint.setColor(Color.WHITE);handler.sendEmptyMessage(0);handler.sendEmptyMessage(1);}
@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//刻度數int count = 60;//背景色canvas.drawColor(getResources().getColor(R.color.gray));//移動畫布canvas.translate(getWidth() / 2, getHeight() / 2);//圓環大小RectF rectF = new RectF(-100, -100, 100, 100);String indexString = String.valueOf(index);if (index < 10) {indexString = "0" + indexString;}Rect rect = new Rect();textPaint.getTextBounds(indexString, 0, <span style="font-family: Arial, Helvetica, sans-serif;">indexString </span><span style="font-family: Arial, Helvetica, sans-serif;">.length(), rect);</span>canvas.drawText(indexString, -rect.width() / 2, rect.height() / 2,textPaint);//圓環canvas.drawArc(rectF, 0, 360, false, rectPaint);//填充色canvas.drawArc(rectF, -90, index, false, linePaint);for (int i = 0; i < index2; i++) {canvas.drawLine(0, -90, 0, -110, fillPaint);canvas.rotate(360 / count, 0, 0);}}


PS:上傳的代碼中有一個bug,再onDraw中衡量字型大小時不應該用“00”.length(),應該用indexString.length()

粗心大意要不得啊

項目源碼




Android 自訂學習(5)自訂Progressbar

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.