Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自定義控件之自定義屬性(二):打造可配置的圓形百分比控件

編輯:關於 Android 程式設計

前言:上一篇介紹了自定義控件的基本要求與繪製原理,本篇則著重說明如何為自定義控件加上自定義屬性。本文將延續上一篇的圓形百分比範例繼續講解,原理部分可參考〈Android 自定義控件基本原理詳解(一)〉。

需求背景:為什麼要引入自定義屬性?當 Android 提供的原生屬性無法滿足實際需求時——比如我們想自定義圓形百分比的半徑大小、背景、顯示位置、進度顏色等等——這時就需要自定義屬性來幫忙了。

自定義屬性的步驟:

1.)在 res/values 目錄下新增一個 attrs.xml 檔。如果專案比較龐大,attrs.xml 可能會變得相當冗長,這時可以依功能模塊命名,方便查找,例如登錄模塊相關的 attrs_login.xml。

 <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="RoundImageView"> <attr name="borderRadius" /> <attr name="type" /> </declare-styleable> </resources> 

2.)如何聲明一組屬性
使用<declare-styleable name="PercentView"></declare-styleable>來定義一個屬性集合,name 就是屬性集合的名字,這個名字一定要起得見名知意。

 <declare-styleable name="PercentView"> <!--添加屬性--> </declare-styleable> 

接著定義屬性值,透過<attr name="textColor" format="color" />的方式定義屬性,屬性名同樣要見名知意,format 表示該屬性值的類型,常見類型有以下幾種:

 • reference:引用資源
 • string:字串
 • color:顏色
 • boolean:布爾值
 • dimension:尺寸值
 • float:浮點型
 • integer:整型
 • fraction:百分數
 • enum:列舉類型
 • flag:位或運算

基於上面的需求,我們可以定義百分比控件的屬性:

 <declare-styleable name="PercentView"> <attr name="percent_circle_gravity"><!--圓形繪制的位置--> <flag name="left" value="0" /> <flag name="top" value="1" /> <flag name="center" value="2" /> <flag name="right" value="3" /> <flag name="bottom" value="4" /> </attr> <attr name="percent_circle_radius" format="dimension" /><!--圓形半徑--> <attr name="percent_circle_progress" format="integer" /><!--當前進度值--> <attr name="percent_progress_color" format="color" /><!--進度顯示顏色--> <attr name="percent_background_color" format="color" /><!--圓形背景色--> </declare-styleable> 

3.)版面配置中如何使用

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:lee="https://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <com.whoislcj.views.PercentView android:layout_width="200dp" android:layout_height="200dp" android:layout_margin="10dp" android:background="@color/red" android:padding="10dp" lee:percent_background_color="@color/gray" lee:percent_circle_gravity="left" lee:percent_circle_progress="30" lee:percent_circle_radius="50dp" lee:percent_progress_color="@color/blue" /> </LinearLayout> 

這裡要為屬性集設置一個命名空間,我用的是 lee——實在想不到好名字時的權宜之計。建議在真正的專案中使用專案的縮寫,比如微信大概就會用 wx。

4.)自定義控件中如何獲取自定義屬性
每個屬性集合編譯後都會對應一個 styleable 物件,透過它可以取得 TypedArray,再以鍵值對的方式讀取屬性值,這點有點類似 SharedPreferences 的用法。

 TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PercentView); if (typedArray != null) { backgroundColor = typedArray.getColor(R.styleable.PercentView_percent_background_color, Color.GRAY); progressColor = typedArray.getColor(R.styleable.PercentView_percent_progress_color, Color.BLUE); radius = typedArray.getDimension(R.styleable.PercentView_percent_circle_radius, 0); progress = typedArray.getInt(R.styleable.PercentView_percent_circle_progress, 0); gravity = typedArray.getInt(R.styleable.PercentView_percent_circle_gravity, CENTER); typedArray.recycle(); }

5.)完整範例

 public class PercentView extends View { private final static String TAG = PercentView.class.getSimpleName(); private Paint mPaint; private int backgroundColor = Color.GRAY; private int progressColor = Color.BLUE; private float radius; private int progress; private float centerX = 0; private float centerY = 0; public static final int LEFT = 0; public static final int TOP = 1; public static final int CENTER = 2; public static final int RIGHT = 3; public static final int BOTTOM = 4; private int gravity = CENTER; private RectF rectF; //用於定義的圓弧的形狀和大小的界限 public PercentView(Context context) { super(context); init(); } public PercentView(Context context, AttributeSet attrs) { super(context, attrs); initParams(context, attrs); init(); } public PercentView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); initParams(context, attrs); init(); } private void init() { mPaint = new Paint(); mPaint.setAntiAlias(true); rectF = new RectF(); } private void initParams(Context context, AttributeSet attrs) { mPaint = new Paint(); mPaint.setAntiAlias(true); rectF = new RectF(); TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.PercentView); if (typedArray != null) { backgroundColor = typedArray.getColor(R.styleable.PercentView_percent_background_color, Color.GRAY); progressColor = typedArray.getColor(R.styleable.PercentView_percent_progress_color, Color.BLUE); radius = typedArray.getDimension(R.styleable.PercentView_percent_circle_radius, 0); progress = typedArray.getInt(R.styleable.PercentView_percent_circle_progress, 0); gravity = typedArray.getInt(R.styleable.PercentView_percent_circle_gravity, CENTER); typedArray.recycle(); } } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); int widthMode = MeasureSpec.getMode(widthMeasureSpec); int widthSize = MeasureSpec.getSize(widthMeasureSpec); int heightMode = MeasureSpec.getMode(heightMeasureSpec); int heightSize = MeasureSpec.getSize(heightMeasureSpec); Log.e(TAG, "onMeasure--widthMode-->" + widthMode); switch (widthMode) { case MeasureSpec.EXACTLY:// break; case MeasureSpec.AT_MOST: break; case MeasureSpec.UNSPECIFIED: break; } Log.e(TAG, "onMeasure--widthSize-->" + widthSize); Log.e(TAG, "onMeasure--heightMode-->" + heightMode); Log.e(TAG, "onMeasure--heightSize-->" + heightSize); int with = getWidth(); int height = getHeight(); Log.e(TAG, "onDraw---->" + with + "*" + height); centerX = with / 2; centerY = with / 2; switch (gravity) { case LEFT: centerX = radius + getPaddingLeft(); break; case TOP: centerY = radius + getPaddingTop(); break; case CENTER: break; case RIGHT: centerX = with - radius - getPaddingRight(); break; case BOTTOM: centerY = height - radius - getPaddingBottom(); break; } float left = centerX - radius; float top = centerY - radius; float right = centerX + radius; float bottom = centerY + radius; rectF.set(left, top, right, bottom); } @Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { super.onLayout(changed, left, top, right, bottom); Log.e(TAG, "onLayout"); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); mPaint.setColor(backgroundColor); // FILL填充, STROKE描邊,FILL_AND_STROKE填充和描邊 mPaint.setStyle(Paint.Style.FILL_AND_STROKE); canvas.drawCircle(centerX, centerY, radius, mPaint); mPaint.setColor(progressColor); double percent = progress * 1.0 / 100; int angle = (int) (percent * 360); canvas.drawArc(rectF, 270, angle, true, mPaint); //根據進度畫圓弧 } }

運行結果:依不同配置呈現出兩種效果

小結:透過自定義屬性,我們可以讓自定義控件像原生控件一樣支援配置。不過在實際專案中,這種自定義控件的使用頻率並不算最高,真正高頻的其實是透過自定義組合控件來達到配置复用、減少維護與開發成本。下一篇將重點介紹如何自定義組合控件,敬請期待。

以上就是本文的全部內容,希望對大家的學習有所幫助,也歡迎大家多多支持本站。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved