Android onMeasure與onDraw及自定義屬性使用示例
1.自定義View簡介
自定義view可以被認(rèn)為是繼承自View,系統(tǒng)沒有的效果(ImageView,TextView,Button),extents View,extents ViewGrop
2.構(gòu)造方法
繼承View。View有四個(gè)構(gòu)造方法,下面講述四個(gè)構(gòu)造方法什么時(shí)候調(diào)用:
第一個(gè)構(gòu)造方法會(huì)在代碼中new的時(shí)候調(diào)用
TextView textView = new TextView(this);
public TextView(Context context) {
super(context);
}
第二個(gè)構(gòu)造方法在布局layout中使用(調(diào)用)
<com.zrc.view_java_demo_01.TextView android:layout_width="match_parent" android:layout_height="match_parent"/>
public TextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
第三個(gè)構(gòu)造方法在布局layout中使用(調(diào)用),但是會(huì)有style
調(diào)用 <com.zrc.view_java_demo_01.TextView style="@style/defualt"/>
<style name="defualt" > <item name="android:layout_width">wrap_content</item> <item name="android:layout_height">wrap_content</item> <item name="android:textColor">@color/colorAccent</item> </style>
public TextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
還有第四個(gè)構(gòu)造方法,在用到時(shí)候,再做說明,在這里就不再展開。
3.onMeasure()
獲取寬高的模式
int widthSize = MeasureSpec.getMode(widthMeasureSpec); //獲取前兩位 int heightSize = MeasureSpec.getMode(heightMeasureSpec);
獲取寬高的值,指定控件的寬高
int widthSize = MeasureSpec.getSize(widthMeasureSpec); //獲取后面30位 int heightSize = MeasureSpec.getSize(heightMeasureSpec);
MeasureSpec.AT_MOST:在布局中指定了wrap_content
MeasureSpec.EXACTLY: 在布局中特定的值 100dp match_parent fill_parent
MeasureSpec.UNSPECIFIED:盡可能的大,很少用到。listview,Scrollview 在測量子布局時(shí)候會(huì)用UNSPECIFIED
Scrollview+ListView會(huì)出現(xiàn)顯示不全的現(xiàn)象?
widthMeasureSpec widthMeasureSpec : 會(huì)包含兩個(gè)信息是一個(gè)32位的值,第一個(gè)信息是模式:2位 值:30位
4.onDraw()
/**
* 用于繪制
* */
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//畫文本
canvas.drawText();
//畫弧
canvas.drawArc();
//畫圓
canvas.drawCircle();
}
5.onTouch()
/**
* 處理用戶交互的,手指觸摸等等(事件分發(fā)事件攔截)
* */
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
//手指按下
Log.e("TAG","手指按下");
break;
case MotionEvent.ACTION_MOVE:
//手指移動(dòng)
Log.e("TAG","手指移動(dòng)");
break;
case MotionEvent.ACTION_UP:
//手指抬起
Log.e("TAG","手指抬起");
break;
}
return super.onTouchEvent(event);
}
6.自定義屬性
自定義屬性就是用來配置的,android:text = "Darren"是系統(tǒng)自定義屬性
6.1在res下的values下面新建attrs.xml
<!-- name 屬性名稱
format 格式: string 文字 color:顏色
dimension 寬高 字體大小 integer 數(shù)字
reference 資源(drawable)
-->
<attr name="text" format="string"/>
<attr name="textColor" format="color"/>
<attr name="textSize" format="dimension"/>
<attr name="maxLength" format="integer"/>
<attr name="background" format="reference|color"/>
<!-- 枚舉 -->
<attr name="inputType">
<enum name="number" value="1"/>
<enum name="text" value="2"/>
<enum name="password" value="3"/>
</attr>
</declare-styleable>
6.2在布局中使用
聲明命名空間,然后在自己的自定義View中使用
xmlns:app="http://schemas.android.com/apk/res-auto"
<com.zrc.view_java_demo_01.TextView
app:text="Darren"
app:textColor="@color/colorAccent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
6.3在自定義View中獲取屬性
// 獲取自定義屬性 TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.TextView); mText = array.getString(R.styleable.TextView_text); mTextColor = array.getColor(R.styleable.TextView_textColor,mTextColor); mTextSize = array.getDimensionPixelSize(R.styleable.TextView_textSize,mTextSize); // 回收 array.recycle();
到此這篇關(guān)于Android onMeasure與onDraw及自定義屬性使用示例的文章就介紹到這了,更多相關(guān)Android onMeasure內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Android xmlns 的作用及其自定義實(shí)例詳解
這篇文章主要介紹了 Android xmlns 的作用及其自定義實(shí)例詳解的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android開發(fā)之關(guān)于項(xiàng)目
本文是此系列文章的第二篇,給大家介紹的是項(xiàng)目相關(guān)的內(nèi)容,非常的細(xì)致全面,有需要的小伙伴可以參考下2016-02-02
Android App開發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法
這篇文章主要介紹了Android App開發(fā)中將View或Drawable轉(zhuǎn)為Bitmap的方法,其中View轉(zhuǎn)換時(shí)作者特別提到了getDrawingCache=null問題的解決方法,需要的朋友可以參考下2016-03-03
Android AIDL和遠(yuǎn)程Service調(diào)用示例代碼
本文主要介紹Android AIDL和遠(yuǎn)程Service,這里詳細(xì)介紹了相關(guān)知識,并附實(shí)例代碼和實(shí)現(xiàn)效果圖,有興趣的朋友參考下2016-08-08
Android 使用Fragment模仿微信界面的實(shí)例代碼
自從Android 3.0中引入fragments 的概念,根據(jù)詞海的翻譯可以譯為:碎片、片段。其目的是為了解決不同屏幕分辯率的動(dòng)態(tài)和靈活UI設(shè)計(jì)。下面通過本文給大家分享Android 使用Fragment模仿微信界面的實(shí)例代碼,需要的的朋友參考下吧2017-07-07
Android網(wǎng)頁H5 Input選擇相機(jī)和系統(tǒng)相冊
這篇文章主要為大家詳細(xì)介紹了Android網(wǎng)頁H5 Input選擇相機(jī)和系統(tǒng)相冊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10
Android中EditText屏蔽第三方輸入法表情的方法示例
最近在工作終于遇到一個(gè)問題,因?yàn)榈谌捷斎敕ū砬榈膯栴}導(dǎo)致Android中TextView的內(nèi)容顯示異常,只能想辦法解決了,下面這篇文章主要記錄了在處理Android中EditText屏蔽第三方輸入法表情的方法,需要的朋友可以參考借鑒,下面來一起看看吧。2017-01-01
Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果示例
這篇文章主要介紹了Android開發(fā)實(shí)現(xiàn)ListView點(diǎn)擊展開收起效果,結(jié)合實(shí)例形式分析了Android ListView控件的布局及事件響應(yīng)相關(guān)操作技巧,需要的朋友可以參考下2019-03-03

