SoFunction
Updated on 2025-04-09

Android onMeasure, onDraw and custom attribute usage examples

1. Introduction to Custom View

Custom view can be considered to be inherited from View, which has no effect (ImageView, TextView, Button), extents View, extents ViewGrop

2. Construction method

Inherit View. View has four constructor methods. The following describes when the four constructor methods are called:

The first constructor will be called when new in the code

TextView textView = new TextView(this);

public TextView(Context context) {
        super(context);
    }

The second constructor is used in layout layout (call)

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

The third constructor is used (called) in the layout layout, but there will be style

Call<.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);
    }

There is also a fourth construction method, which will be explained when it is used, and will not be expanded here.

()

Get width and height mode

int widthSize = (widthMeasureSpec); //Get the first twoint heightSize = (heightMeasureSpec);

Get the width and height value, specify the width and height of the control

 int widthSize = (widthMeasureSpec); //Get the last 30 digits int heightSize = (heightMeasureSpec);

MeasureSpec.AT_MOST: wrap_content is specified in the layout

: Specific value in layout 100dp match_parent fill_parent

: As large as possible, rarely used. listview, Scrollview will use UNSPECIFIED when measuring sub-layout

Will Scrollview+ListView show incomplete display?

widthMeasureSpec widthMeasureSpec: It will contain two information that is a 32-bit value. The first information is the mode: 2-bit value: 30-bit

()

 /**
      * For drawing
      * */
    @Override
    protected void onDraw(Canvas canvas) {
        (canvas);
        //Draw text        ();
        //Draw an arc        ();
        //Draw a circle        ();
    }

()

    /**
      * Handle user interaction, finger touch, etc. (event distribution event interception)
      * */
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (()){
            case MotionEvent.ACTION_DOWN:
                //Press your finger                ("TAG","Press your finger");
                break;
            case MotionEvent.ACTION_MOVE:
                //Finger movement                ("TAG","Finger Move");
                break;
            case MotionEvent.ACTION_UP:
                //Release your fingers                ("TAG","Finger lift");
                break;
        }
        return (event);
    }

6. Custom properties

Custom attributes are used to configure, android:text = "Darren" is a system custom attribute

6.1 Create new values ​​under res

&lt;!-- name Attribute name
         format Format: string Word  color:color
                       dimension Width and height Font size integer number
                       reference resource(drawable)
          --&gt;
        &lt;attr name="text" format="string"/&gt;
        &lt;attr name="textColor" format="color"/&gt;
        &lt;attr name="textSize" format="dimension"/&gt;
        &lt;attr name="maxLength" format="integer"/&gt;
        &lt;attr name="background" format="reference|color"/&gt;
        &lt;!-- enumerate --&gt;
        &lt;attr name="inputType"&gt;
            &lt;enum name="number" value="1"/&gt;
            &lt;enum name="text" value="2"/&gt;
            &lt;enum name="password" value="3"/&gt;
        &lt;/attr&gt;
    &lt;/declare-styleable&gt;

6.2 Use in layout

Declare the namespace and use it in your own custom view

xmlns:app="/apk/res-auto"
<.view_java_demo_01.TextView
        app:text="Darren"
        app:textColor="@color/colorAccent"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

6.3 Get attributes in custom view

 // Get custom properties TypedArray array = (attrs, );
 mText = (.TextView_text);
 mTextColor = (.TextView_textColor,mTextColor);
  mTextSize = (.TextView_textSize,mTextSize);
 // Recycle ();

This is the end of this article about Android onMeasure, onDraw and custom attribute usage examples. For more related Android onMeasure content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!