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();