MasterofProject

Custom View simple example

Classification: Andrews advanced 349 people read Comment(0) Collection Report

In the development process, although the Android system provides a lot of control to our use, but still can not meet the needs of our human beings, we feel that we are indeed greedy, ha ha! This time, we may have to use a custom control, as well as custom attributes, it should be how to operate it?

Generally follow the following steps to the operation:

OneView or other control, onDraw, onTouch, onMeasure, and so on.

2, custom attributes, you need to create a attrs.xml in the values, which define the attributes you need, the details of the property type can refer to the articleHttp://www.jb51.net/article/32172.htm.

3, the use of the XML layout file to customize the View to add xmlns: prefix = "http://schemas.android.com/apk/res/ custom View package path", as I used the prefix is xmlns:app.When using the custom attribute, also uses the prefix: attribute name.

Here follow the steps to a small example:

- definition View, override onDraw () -

Class DefaultView extends View public {
Brush paint with / * *, set the color and style drawing drawing patterns.
Paint mPaint private;


* / / * * construction method
DefaultView public (context Context) {
Super (context);
}


DefaultView public (context AttributeSet, attrs Context) {
Super (context, attrs);
MPaint = Paint new ();
An array of properties that are acquired by /**TypedArray context.obtainStyledAttributes
* use the recycle method when you are finished.
* /
Array TypedArray = context.obtainStyledAttributes (attrs, R.styleable.DefaultView);
/ * * get the color value, or use the default value.
TextColor int = array.getColor (R.styleable.DefaultView_textColor, 0XFF00FF00);
Get / * * text size, or use the default value.
TextSize float = array.getDimension (R.styleable.DefaultView_textSize, 36);
MPaint.setColor (textColor);
MPaint.setTextSize (textSize);
/ * * if you don't call recycle () method, this setting will use for the next impact.
Array.recycle ();
}


(onDraw) / * * rewriting method, to draw the pattern implementation here * /
Void onDraw public (canvas Canvas) {
Super.onDraw (canvas);
Brush color / * * * /
MPaint.setColor (Color.RED);
Brush style / * * * /
MPaint.setStyle (Style.FILL);
Brush width * / / * *
MPaint.setStrokeWidth (2);
Draw a rectangle / * * * /
Canvas.drawRect (10, 10, 100, 100, mPaint);


MPaint.setColor (Color.BLUE);
Set the brush against a saw tooth * / / * *
MPaint.setAntiAlias (true);
Draw text / * * * /
Canvas.drawText ("painting is text", 20, 120, mPaint);


MPaint.setColor (Color.GREEN);
Draw a circle / * * * /
Canvas.drawCircle (100F, 100F, 50F, mPaint);
}
}

- create a attrs.xml file in the res/values folder, ------------------------ custom attribute of our View

<? Version= encoding= "1" XML "UTF-8" >
<resources>

Name= <declare-styleable "DefaultView" >
<attr name= "textColor" format= "color" / >
<attr name= "textSize" format= "dimension" / >
</declare-styleable>
</resources>

- use -- in the layout file in our

Xmlns:android= http://schemas.android.com/apk/res/android "<LinearLayout"
Http://schemas.android.com/tools "xmlns:tools="
Xmlns:app= "http://schemas.android.com/apk/res/com.ldm.map" <! -- to add this sentence -- >
Match_parent "android:layout_width="
Android:layout_height= "match_parent" >

<com.ldm.map.DefaultView
Match_parent "android:layout_width="
Wrap_content "android:layout_height="
App:textColor= "#f0f0f0" <! -- prefix is app:-->
App:textSize= "16sp" / >

</LinearLayout>

Copyright statement: This article is the original article of the main Bo, without the permission of the owner.

Top
0
Step
0
Guess what you're looking for.
View comments
* above the user's comments only represent their personal views, do not represent the views or position of the CSDN website
    Personal data
    • Visit35221 times
    • Integral:1704
    • Grade
    • Rank:13303rd
    • Original141
    • Reprint:36
    • Translation:1
    • Comment:9
    Latest comments