編輯:關於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); } }效果圖
源碼
android4.4 webview chromium是單進程的,圖中所有組件都運行在Browser進程中。 按從上而下的順序介紹這張圖中與顯示網頁相關的chromiu
效果圖一、繪制圓環圓環故名思意,第一個首先繪制是圓環1:圓環繪制函數圓環APIpublic void drawArc (RectF oval, float startAn
大家好,由於最近會有對智能硬件相關的開發需求,所以最近這些天分享的博文也就大致掛鉤智能硬件了,像上一篇的藍牙分享,相信很多讀者已經看過了,那麼今天我為大家帶來Androi
什麼是內存洩露?Android虛擬機的垃圾回收采用的是根搜索算法。GC會從根節點(GC Roots)開始對heap進行遍歷。到最後,部分沒有直接或者間接引用到GC Roo