Android 自定義控件開發入門 (三)
上兩次我們從如何自定義控件講起,列舉了View的一些Api,說明了一些在自定義的時候,可以進行重寫的方法,然後通過一個例子的兩種寫法向大家展示了最基本的自定義控件和我們要充分了解並積極重寫View方法的精神,這次我們將繼續進行學習!
現在請大家回想一下我們使用安卓原生控件時的感受,一個好的控件是可以在xml中進行各種屬性的操作的,而自定義控件往往有一些特殊的需求,今天我要講的就是安卓給自定義控件添加自定義的屬性。
下麵再給大家具體介紹一下如果自定義的View需要有自定義的屬性我們該如何處理:
我們還是用這個例子,繼續擴展,比如我想在xml中設置球體的半徑,我該怎麼辦呢?
讓我們先來具體了解一下自定義屬性的一些簡單基礎,然後通過實例大家就會很容易掌握了!
首先看一下屬性文件中format可選項
如果自定義控件需要自定義該控件的屬性。在開始前,我們需要檢查在values目錄下是否有attrs.xml,如果沒有則創建。下麵我們先來了解一下format屬性類型都有哪些。
format可選項
"reference" //引用
"color" //顏色
"boolean" //布爾值
"dimension" //尺寸值
"float" //浮點值
"integer" //整型值
"string" //字符串
"fraction" //百分數比如200%
這裏我們的例子就多用幾個自定義屬性,幫助大家理解,我們就定義半徑,顏色,以及小球剛開始停止的位置就好了,
我們先創建attrs.xml:
代碼如下:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="MoveBallversion3"> <attr name="BallColor" format="color" /> <attr name="BallRadius" format="float" /> <attr name="BallStartX" format="float" /> <attr name="BallStartY" format="float" /> </declare-styleable> </resources>
MoveBallversion3是用來在java代碼中找到這些數據的名字,下麵每一項前部是屬性名,後部是類型。
之後我們在創建自定義控件的時候就可以使用自定義屬性了,但是設置的時候要注意:
1)其中com.example.customcomponentsdemo.component.DrawView3這句是需要顯示的控件所代表的類。
2)com.example.customcomponentsdemo是類的包名,
就是manifast裏麵package地址
3)DrawView3是類名。這個類肯定是繼承自View的自定義類,可以是在工程中直接源碼添加xxxx.java的,也可以是在libs目錄下自己新添加的jar包裏麵的。如果是jar包裏麵的一個類,則路徑就是jar包裏麵,
Xml如下
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:andro xmlns:myball="https://schemas.android.com/apk/res/com.example.customcomponentsdemo" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="這是MoveBall的Demo 版本3" android:textColor="@color/white" > </TextView> <com.example.customcomponentsdemo.component.DrawView3 android:layout_width="match_parent" android:layout_height="match_parent" android:layout_margin="10dp" myball:BallColor = "@color/white" myball:BallRadius = "20" myball:BallStartX = "50" myball:BallStartY = "200" > </com.example.customcomponentsdemo.component.DrawView3> </LinearLayout>
之後在重寫的時候獲取就可以了。
不過有時候會出現這種錯誤。
有時候使用控件的自定義屬性,使用時填寫命名空間。
按照老樣子:https://schemas.android.com/apk/控件地址
在編譯的時候報錯error: No resource identifier found for attribute 'xxxxt' in package
解決:代碼看了n遍也沒找出問題,最後再網上看了一個老外的回答。ADT升級以後,自定義控件的命名空間的路徑訪問給優化了改成
https://schemas.android.com/apk/res-auto 這樣填充之後ADT會自動去相應的包路徑下尋找。
我們在繼續向下看:
我們在DrawView3.java 代碼編寫如下,其中下麵的構造方法是重點,我們獲取定義的屬性,獲取方法中後麵通常設定默認值如:(floattextSize = a.getDimension(R.styleable.MyView_textSize, 36 ); ), 防止我們在xml 文件中沒有定義.從而使用默認值!
MoveBallversion3就是定義在<declare-styleablename="MoveBallversion3 "></declare-styleable> 裏的 名字,獲取裏麵屬性用 名字_ 屬性 連接起來就可以.TypedArray 通常最後調用 .recycle() 方法,為了保持以後使用該屬性一致性!
好,看一下具體代碼:
package com.example.customcomponentsdemo.component; import com.example.customcomponentsdemo.R; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class DrawView3 extends View { private float CircleX; private float CircleY; private float CircleR; private int BallColor; public DrawView3(Context context, AttributeSet attrs) { super(context, attrs); // 獲取自定義屬性 TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.MoveBallversion3); // 獲取開始的X位置屬性值,默認大小為:10 CircleX = a.getFloat(R.styleable.MoveBallversion3_BallStartX, 10); // 獲取開始的位置屬性值,默認大小為:10 CircleY = a.getFloat(R.styleable.MoveBallversion3_BallStartY, 10); // 獲取小球的半徑屬性值,默認大小為:10 CircleR = a.getFloat(R.styleable.MoveBallversion3_BallRadius, 10); // 獲取顏色屬性值,默認顏色為:0x990000FF BallColor = a.getColor(R.styleable.MoveBallversion3_BallColor, 0x990000FF); a.recycle(); } @Override public void onDraw(Canvas canves) { Paint paint = new Paint(); paint.setColor(BallColor); canves.drawCircle(CircleX, CircleY, CircleR, paint); } @Override public boolean onTouchEvent(MotionEvent motionevent) { CircleX = motionevent.getX(); CircleY = motionevent.getY(); this.invalidate(); return true; } }
具體的注釋也寫了,大家好好看看代碼就好了,接下來是activity的代碼,也就沒什麼好說的了:
package com.example.customcomponentsdemo.Activity; import com.example.customcomponentsdemo.R; import android.app.Activity; import android.content.Context; import android.os.Bundle; public class MoveBallActivity3 extends Activity{ @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_moveball3); } }
這樣我們的第三個教程也就到這裏結束了,這次給大家介紹一下如果自定義的View需要有自定義的屬性我們該如何處理,這一講也是這個係列完結篇,自定義View之路還有很遠,我也沒有舉一些很難的例子,我認為基礎知識設計流程就是這樣了,學習了這些之後自定義控件的製作就剩下的大家在次基礎之上發揮了!之後如果有比較好的例子我還會繼續補充的!
源碼的地址是:https://download.csdn.net/detail/sunmc1204953974/7738915
希望大家能學到東西!
另外我也是學生,如果有寫的不好或者有錯誤的地方還請大家多多指教,謝謝!
最後更新:2017-04-03 05:39:50