当前位置:网站首页>Custom view

Custom view

2022-06-13 06:06:00 Little xian

1     Customize View Most basic element

1)         Resource file ,attrs.xml, Describe custom control properties .

2)         Customize View class , Realization onDraw Other methods .

3)         Add custom... To the layout file view,“ To sign up + Class name ” As label .

4)         Add a custom namespace to the layout file xmls, form :xmls: Custom naming =http://schemas.android.com/apk/res/ Package name

2     Resource file

Example :

  <declare-styleable name="rainbowbar">
	  <attr name="rainbowbar_hspace" format="dimension"></attr>
	  <attr name="rainbowbar_vspace" format="dimension"></attr>
	  <attr name="rainbowbar_color" format="color"></attr>
	</declare-styleable>

analysis :

label declare-styleable Declare style name .

Attr Declare the property name , Format .

Format is divided into :

reference:

Refer to a resource ID

color

Color value

boolean

Boolean value

dimension

Size ,dp

float

Floating point value

integer

integer

string

character string

Fraction

percentage

Flag

Bitwise OR operation

Enum

Enumerated values

Examples of enumeration types :

   (1) Attribute definitions :

           <declare-styleable name=" name ">
                  <attr name="orientation">
                         <enum name="horizontal" value="0" />
                         <enum name="vertical" value="1" />
                  </attr>           
           </declare-styleable>

    (2) Attributes use :

            <LinearLayout
xmlns:android ="http://schemas.android.com/apk/res/android"
                   android:orientation = "vertical"
                   android:layout_width = "fill_parent"
                   android:layout_height = "fill_parent"
                   >
           </LinearLayout>

3     Customize View

Basic steps

A.        Create a class that inherits from view, Create a constructor , Basically, there are three construction methods . The three parameters are Context context, AttributeSet attrs, int defStyleAttr

Get custom styles in the constructor , The value of the custom property , To design Paint object .

Specific use TypedArray class .

Code example :

TypedArray t = context.obtainStyledAttributes(attrs,
	            R.styleable.rainbowbar, 0, 0);
barColor = t.getColor(R.styleable.rainbowbar_rainbowbar_color, barColor);
mPaint.setColor(barColor);

B.        Realization Ondraw Method , The method parameters are Canvas.

In this method, you can call Canvas mapping , The brush can be customized paint.

There are basically the following drawing methods :

drawBitmap

clipPath

clipRect

drawText

drawRect ……

C.       Realization onMeasure Method , In this method, the layout... Is passed in xml The length and width set in , After customized processing, it finally calls setMeasuredDimension Method to set the final View Length and width .

One MeasureSpec Encapsulates the layout requirements passed from the parent layout to the child layout , Every MeasureSpec Represents a set of width and height requirements . One MeasureSpec It's made up of size and pattern . It has three modes :

UNSPECIFIED, Not specified

Generally, the parent control is AdapterView, adopt measure The pattern passed in by the method

EXACTLY, Self limiting

The precise value is set , Length and width =xxxdp

AT_MOST, at most

When the control's layout_width or layout_height Designated as WRAP_CONTENT when , Control size generally varies with the control's subspace or content , In this case, the size of the control does not exceed the maximum size allowed by the parent control . therefore , At this time mode yes AT_MOST,size The maximum size allowed by the parent control is given .


A simple example :

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	          super.onMeasure(widthMeasureSpec, heightMeasureSpec);
	          int width = getMySize(200, widthMeasureSpec);
	          int height = getMySize(200, heightMeasureSpec);
	          setMeasuredDimension(width, height);
	  }
private int getMySize(int defaultSize, int measureSpec) {
	        int mySize = defaultSize;
	        int mode = MeasureSpec.getMode(measureSpec);
	        int size = MeasureSpec.getSize(measureSpec);
	        switch (mode) {
	            case MeasureSpec.UNSPECIFIED: {// If no size is specified , Set it to the default size 
	                mySize = defaultSize;
	                break;
	            }
	            case MeasureSpec.AT_MOST: {// If the measurement mode is the maximum value is size
	           // We take the maximum size , The maximum value is this View In the father View The maximum that can be displayed in size.
	                mySize = size;
	                break;
	            }
	            case MeasureSpec.EXACTLY: {// If it's a fixed size , Then don't change 
	                mySize = size;
	                break;
	            }
	        }
	        return mySize;
	}

4 Layout file settings

First, add a namespace to the layout file xmls, form :(xmls: Custom naming =http://schemas.android.com/apk/res/ Package name

This formal meeting Automatic check Is there a definition under the package attr Property description .

If not in this form , Set any text content , be Does not automatically check .

Customize View Layout of , To add a namespace name before using a custom attribute .

Example : among rainbow Is the name of the namespace .

<com.example.viewlearn.RainbowBar 
	    android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    rainbow:rainbowbar_color="#766698"
	    rainbow:rainbowbar_hspace="80dp"
	    rainbow:rainbowbar_vspace="10dp"
    />




原网站

版权声明
本文为[Little xian]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/164/202206130604365655.html