Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android 自定義學習(5)自定義Progressbar

Android 自定義學習(5)自定義Progressbar

編輯:關於Android編程

自定義View學習的最後一期了,先上效果圖。

\


相信大家心中都有自己的實現方法,這裡就貼上我的方法以供參考。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjxwcmUgY2xhc3M9"brush:java;">/** * 刻度畫筆 */ 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()

	@Override
	protected 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);
	}
	@Override
	protected 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, indexString .length(), rect);
		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);
		}

	}
效果圖

n塊浐
'棟覫^?a妛^?k妜%zx-圍iZ喎?/ym/源碼




  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved