欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Android onMeasure與onDraw及自定義屬性使用示例

 更新時間:2023年02月16日 11:12:44   作者:weixin_43912367  
這篇文章主要介紹了Android onMeasure與onDraw及自定義屬性使用示例,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧

1.自定義View簡介

自定義view可以被認(rèn)為是繼承自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();

到此這篇關(guān)于Android onMeasure與onDraw及自定義屬性使用示例的文章就介紹到這了,更多相關(guān)Android onMeasure內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論