安卓自定義View

1.自定義View簡(jiǎn)介

自定義View可以認(rèn)為繼承自View,系統(tǒng)沒(méi)有的效果(ImageView,TextView,Button),extends View, extends ViewGroup

自定義一個(gè)系統(tǒng)的TextView,自定義View入門

2.onMeasure()

// 獲取寬高的模式

int widthMode = MeasureSpec.getMode(widthMeasureSpec); // 獲取前兩位

int heightMode = 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 在測(cè)量子布局的時(shí)候會(huì)用UNSPECIFIED

ScrollView + ListView 會(huì)顯示不全問(wèn)題,

widthMeasureSpecwidthMeasureSpec : 會(huì)包含兩個(gè)信息是一個(gè)32位的值,第一個(gè)信息是模式:2位? 值:30位?

3.onDraw()?

? ? /**

? ? * 用于繪制

? ? * @param canvas

? ? */

? ? @Override

? ? protected void onDraw(Canvas canvas) {

? ? ? ? super.onDraw(canvas);

? ? ? ? // 畫文本

? ? ? ? canvas.drawText();

? ? ? ? // 畫弧

? ? ? ? canvas.drawArc();

? ? ? ? // 畫圓

? ? ? ? canvas.drawCircle();

? ? }

4.onTouch() ?分析源碼

/**

? ? * 處理跟用戶交互的,手指觸摸等等

? ? * @param event 事件分發(fā)事件攔截

? ? * @return

? ? */

? ? @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);

? ? }

5.自定義屬性

自定義屬性就是用來(lái)配置的,android:text="Darren"

是系統(tǒng)的一個(gè)自定義屬性

5.1 在res下的values下面新建attrs.xml

<resources>

? ? <!--name 自定義View的名字 TextView-->

? ? <declare-styleable name="TextView">

? ? ? ? <!-- 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>

</resources>

5.1 在布局中使用

聲明命名空間,然后在自己的自定義View中使用

xmlns:app="http://schemas.android.com/apk/res-auto"

<com.darren.view_day01.TextView

? ? ? ? app:text="Darren"

? ? ? ? app:textColor="@color/colorAccent"

? ? ? ? android:layout_width="wrap_content"

? ? ? ? android:layout_height="wrap_content" />

5.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);

? ? ? ? // 15 15px 15sp

? ? ? ? mTextSize = array.getDimensionPixelSize(R.styleable.TextView_textSize,mTextSize);

? ? ? ? // 回收

? ? ? ? array.recycle();

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容