很多的時候,系統自帶的View滿足不了我們功能的需求,那么我們就需要自己來自定義一個能滿足我們需求的View,自定義View我們需要先繼承View,添加類的構造方法,重寫父類View的一些方法,例如onDraw,為了我們自定義的View在一個項目中能夠重用,有時候我們需要自定義其屬性,舉個很簡單的例子,我在項目中的多個界面使用我自定義的View,每個界面該自定義View的顏色都不相同,這時候如果沒有自定義屬性,那我們是不是需要構建不同顏色的View出來呢,這樣子我們的代碼就會顯得很沉厄,所以這時候我們就需要自定義其屬性來滿足我們不同的需求,自定義屬性呢,我們需要在values下建立attrs.xml文件,在其中定義我們需要定義的屬性,然后在自定義View中也要做相對應的修改,下面還是用一個實際例來看看自定義View和自定義屬性的使用。
首先看看圓環進度條的效果吧:
從上面的效果圖可以看出,我們可以自定義圓環的顏色,圓環進度的顏色,內圓的顏色,是否顯示進度的百分比,進度百分比的顏色,以及進度是實心還是空心等等,這樣一來是不是可以完全自定義,很方便呢?下面咱們就來看看如何實現。
1.創建自定義屬性文件:在values下面創建attrs.xml屬性文件:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="XCRoundProgressBar"> <attr name="roundColor" format="color"/> <attr name="roundProgressColor" format="color"/> <attr name="roundWidth" format="dimension"></attr> <attr name="innerRoundColor" format="color" /> <attr name="textColor" format="color" /> <attr name="textSize" format="dimension" /> <attr name="max" format="integer"></attr> <attr name="textIsDisplayable" format="boolean"></attr> <attr name="style"> <enum name="STROKE" value="0"></enum> <enum name="FILL" value="1"></enum> </attr> </declare-styleable> </resources>
2. 實現自定義View類:
package com.xc.xcskin.view; import com.xc.xcskin.R; import com.xc.xcskin.R.styleable; import android.R.integer; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.RectF; import android.graphics.Typeface; import android.util.AttributeSet; import android.view.View; /** * 帶進度百分比顯示的進度條,線程安全的View,可直接在線程中更新進度 * @author caizhiming * */ public class XCRoundProgressBar extends View{ private Paint paint;//畫筆對象的引用 private int roundColor;//圓環的顏色 private int roundProgressColor;//圓環進度的顏色 private int innerRoundColor;//圓環內部圓顏色 private float roundWidth;//圓環的寬度 private int textColor;//中間進度百分比字符串的顏色 private float textSize ;//中間進度百分比字符串的字體 private int max;//最大進度 private int progress;//當前進度 private boolean isDisplayText;//是否顯示中間百分比進度字符串 private int style;//進度條的風格:空心圓環或者實心圓環 private static final int STROKE = 0;//空心 private static final int FILL = 1;//實心 public XCRoundProgressBar(Context context){ this(context, null); } public XCRoundProgressBar(Context context,AttributeSet attrs){ this(context,attrs,0); } public XCRoundProgressBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); // TODO Auto-generated constructor stub paint = new Paint(); //從attrs.xml中獲取自定義屬性和默認值 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.XCRoundProgressBar); roundColor = typedArray.getColor(R.styleable.XCRoundProgressBar_roundColor, Color.GREEN); roundProgressColor = typedArray.getColor(R.styleable.XCRoundProgressBar_roundProgressColor, Color.RED); innerRoundColor = typedArray.getColor(R.styleable.XCRoundProgressBar_innerRoundColor, Color.GRAY); roundWidth = typedArray.getDimension(R.styleable.XCRoundProgressBar_roundWidth, 5); textColor =typedArray.getColor(R.styleable.XCRoundProgressBar_textColor, Color.RED); textSize = typedArray.getDimension(R.styleable.XCRoundProgressBar_textSize, 15); max = typedArray.getInteger(R.styleable.XCRoundProgressBar_max, 100); style = typedArray.getInt(R.styleable.XCRoundProgressBar_style, STROKE); isDisplayText =typedArray.getBoolean(R.styleable.XCRoundProgressBar_textIsDisplayable, true); typedArray.recycle(); } @Override protected void onDraw(Canvas canvas) { // TODO Auto-generated method stub super.onDraw(canvas); //畫最外層大圓環 int centerX = getWidth()/2;//獲取中心點X坐標 int certerY = getHeight()/2;//獲取中心點Y坐標 int radius =(int)(centerX - roundWidth/2);//圓環的半徑 paint.setColor(roundColor); paint.setStyle(Paint.Style.STROKE);//設置空心 paint.setStrokeWidth(roundWidth);//設置圓環寬度 paint.setAntiAlias(true);//消除鋸齒 canvas.drawCircle(centerX,certerY, radius, paint);//繪制圓環 //繪制圓環內部圓 paint.setColor(innerRoundColor); paint.setStyle(Paint.Style.FILL); paint.setAntiAlias(true); canvas.drawCircle(centerX, certerY, radius-roundWidth/2, paint); //畫進度 paint.setStrokeWidth(roundWidth);//設置圓環寬度 paint.setColor(roundProgressColor);//設置進度顏色 RectF oval = new RectF(centerX - radius, centerX - radius, centerX + radius, centerX + radius); //用於定義的圓弧的形狀和大小的界限 switch (style) { case STROKE: { paint.setStyle(Paint.Style.STROKE); canvas.drawArc(oval, 0, 360 * progress / max, false, paint); // 根據進度畫圓弧 break; } case FILL: { paint.setStyle(Paint.Style.FILL); if (progress != 0) canvas.drawArc(oval, 0, 360 * progress / max, true, paint); // 根據進度畫圓弧 break; } } //畫中間進度百分比字符串 paint.setStrokeWidth(0); paint.setColor(textColor); paint.setTextSize(textSize); paint.setTypeface(Typeface.DEFAULT_BOLD); int percent = (int)(((float)progress / (float)max) * 100);//計算百分比 float textWidth = paint.measureText(percent +"%");//測量字體寬度,需要居中顯示 if(isDisplayText && style == STROKE && percent != 0){ canvas.drawText(percent+"%", centerX-textWidth/2, centerX + textSize/2, paint); } } public Paint getPaint() { return paint; } public void setPaint(Paint paint) { this.paint = paint; } public int getRoundColor() { return roundColor; } public void setRoundColor(int roundColor) { this.roundColor = roundColor; } public int getRoundProgressColor() { return roundProgressColor; } public void setRoundProgressColor(int roundProgressColor) { this.roundProgressColor = roundProgressColor; } public float getRoundWidth() { return roundWidth; } public void setRoundWidth(float roundWidth) { this.roundWidth = roundWidth; } public int getTextColor() { return textColor; } public void setTextColor(int textColor) { this.textColor = textColor; } public float getTextSize() { return textSize; } public void setTextSize(float textSize) { this.textSize = textSize; } public synchronized int getMax() { return max; } public synchronized void setMax(int max) { if(max < 0){ throw new IllegalArgumentException("max must more than 0"); } this.max = max; } public synchronized int getProgress() { return progress; } /** * 設置進度,此為線程安全控件,由於考慮多線的問題,需要同步 * 刷新界面調用postInvalidate()能在非UI線程刷新 * @author caizhiming */ public synchronized void setProgress(int progress) { if(progress < 0){ throw new IllegalArgumentException("progress must more than 0"); } if(progress > max){ this.progress = progress; } if(progress <= max){ this.progress = progress; postInvalidate(); } } public boolean isDisplayText() { return isDisplayText; } public void setDisplayText(boolean isDisplayText) { this.isDisplayText = isDisplayText; } public int getStyle() { return style; } public void setStyle(int style) { this.style = style; } }