Android onMeasure與onDraw及自定義屬性使用示例
1.自定義View簡介
自定義view可以被認為是繼承自View,系統(tǒng)沒有的效果(ImageView,TextView,Button),extents View,extents ViewGrop
2.構(gòu)造方法
繼承View。View有四個構(gòu)造方法,下面講述四個構(gòu)造方法什么時候調(diào)用:
第一個構(gòu)造方法會在代碼中new的時候調(diào)用
TextView textView = new TextView(this);
public TextView(Context context) { super(context); }
第二個構(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òu)造方法在布局layout中使用(調(diào)用),但是會有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òu)造方法,在用到時候,再做說明,在這里就不再展開。
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 在測量子布局時候會用UNSPECIFIED
Scrollview+ListView會出現(xiàn)顯示不全的現(xiàn)象?
widthMeasureSpec widthMeasureSpec : 會包含兩個信息是一個32位的值,第一個信息是模式: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: //手指移動 Log.e("TAG","手指移動"); 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();
到此這篇關于Android onMeasure與onDraw及自定義屬性使用示例的文章就介紹到這了,更多相關Android onMeasure內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
Android實現(xiàn)滑動刪除操作(PopupWindow)
這篇文章主要介紹了Android ListView結(jié)合PopupWindow實現(xiàn)滑動刪除的相關資料,需要的朋友可以參考下2016-07-07Android基礎總結(jié)篇之三:Activity的task相關介紹
這篇文章主要介紹了Android基礎總結(jié)篇之三:Activity的task相關介紹,具有一定的參考價值,有需要的可以了解一下。2016-11-11AndroidStudio 使用過程中出現(xiàn)的異常(Gradle sync failed)處理辦法
本文主要介紹AndroidStudio 使用過程中出現(xiàn)的異常的解決辦法,這里幫大家舉例說明,如何處理出現(xiàn)這種問題,有需要的小伙伴可以參考下2016-09-09Android 實現(xiàn)手機接通電話后振動提示的功能
本文主要介紹Android 實現(xiàn)手機接通電話后振動提示的功能,這里整理了詳細的相關資料,并附有示例代碼,有需要的朋友可以參考下2016-08-08Android ScrollView 下嵌套 ListView 或 GridView出現(xiàn)問題解決辦法
這篇文章主要介紹了ScrollView 下嵌套 ListView 或 GridView 會發(fā)列表現(xiàn)數(shù)據(jù)只能顯示一行。因為他們都是滾動結(jié)構(gòu),兩個滾動條放到一起就會引起沖突,這里提供解決辦法相關資料,需要的朋友可以參考下2017-07-07Android drawable微技巧,你不知道的drawable細節(jié)
今天小編就為大家分享一篇關于Android drawable微技巧,你不知道的drawable細節(jié),小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2018-10-10