How to customize View
Benefits: special effects to satisfy individual needs
Process:
1) create a class that inherits the View or its subclass.
2) Add Constructor
One parameter: Create an object in the code
Two parameters: Used in the layout File
3) override the onDraw () method to a rectangular area, Canvas Paint
Set paint attributes
// Create a paint brush
Paint paint = new Paint ();
Paint. setColor (Color. RED );
Paint. setAntiAlias (true );
Paint. setTextSize (30 );
4) Draw with canvas
Example: canvas. rotate (90 );
Canvas. drawText ("VerticalTextView", 20, 0, paint );
How to use custom controls
Drag a custom control to the layout file.
To customize the properties of a control, follow these steps:
1) Create attrs. xml in the values directory
Copy From ApiDemos)
2) define attributes
For example:
<Declare-styleable name = "VerticalTextView">
<Attr name = "content" format = "string"/>
3) read and set properties configured in the layout in the custom control code.
Refer to LabelView in ApiDemos
Use custom attributes:
1) Add the namespace of the custom control to the layout file.
Example: xmlns: yuchen = "http://schemas.android.com/apk/res/org.yuchen.templete"
2) Configure attributes, for example, yuchen: content = "sdf"
Dynamic:
1) modify the re-drawn content
2) trigger the system to re-call the onDraw () method
Call the invalidate () method
For example:
// Dynamic custom control
PostDelayed (new Runnable ()
{
@ Override
Public void run ()
{
Text = new Date (). toLocaleString ();
Invalidate ();
PostDelayed (this, 1000 );
}
},1000 );
The running effect is as follows: dynamic generation)
650) this. width = 650; "style =" width: 250px; height: export PX "title =" qq 30902215202.jpg "border =" 0 "hspace =" 0 "src =" http://www.bkjia.com/uploads/allimg/131228/10333G294-0.jpg "width =" 250 "height =" 417 "/>
The Code is as follows:
Public class Mytextview extends View {String content = "abc"; private int color; private float dimension; private Paint paint; public Mytextview (Context context) {// constructor of a parameter, it is applicable to the construction method of the new custom view class super (context);} public Mytextview (Context context, AttributeSet attrs) {// two parameters in the Code. It is applicable, use the drag custom control method in the layout xml // super (context, attrs); // read the attributes used in the control attrs TypedArray a = context. obtainStyledAttributes (attrs, R. styleable. mytextview); CharSequence s =. getString (R. styleable. mytextview_text); // read the text color =. getColor (R. styleable. mytextview_textColor, Color. BLACK); dimension =. getDimension (R. styleable. mytextview_textSize, 20); // if (s! = Null) {// content = s. toString (); //} postDelayed (new Runnable () {// obtain the text of the custom control dynamically @ Override public void run () {content = new Date (). toLocaleString (); invalidate (); // call the ondraw () method and re-paint postDelayed (this, 1000); }}, 1000 );} @ Override protected void onDraw (Canvas canvas) {// canvas Canvas super. onDraw (canvas); Log. e ("onDraw", "onDraw"); paint = new Paint (); paint. setColor (color); paint. setTextSize (dimen Sion); paint. setAntiAlias (true); // removes the Sawtooth canvas. rotate (90); // rotate the canvas 90 degrees. drawText (content, 60,-60, paint); // draw text} public class Mytextview extends View {String content = "abc"; private int color; private float dimension; private Paint paint; public Mytextview (Context context) {// constructor of a parameter. It is applicable to the new custom view class super (context) in the Code );} public Mytextview (Context context, AttributeSet attrs) {// The constructor of the two parameters. Used to drag custom controls in layout xml // super (context, attrs); // read the attrs TypedArray a = context. obtainStyledAttributes (attrs, R. styleable. mytextview); CharSequence s =. getString (R. styleable. mytextview_text); // read the text color =. getColor (R. styleable. mytextview_textColor, Color. BLACK); dimension =. getDimension (R. styleable. mytextview_textSize, 20); // if (s! = Null) {// content = s. toString (); //} postDelayed (new Runnable () {// obtain the text of the custom control dynamically @ Override public void run () {content = new Date (). toLocaleString (); invalidate (); // call the ondraw () method and re-paint postDelayed (this, 1000); }}, 1000 );} @ Override protected void onDraw (Canvas canvas) {super. onDraw (canvas); paint = new Paint (); paint. setColor (color); paint. setTextSize (dimension); paint. setAntiAlias (true); // removes the Sawtooth canvas. rotate (90); // rotate the canvas 90 degrees. drawText (content, 60,-60, paint); // draw text }}
This article is from the "wangcuijing" blog, please be sure to keep this source http://wangcuijing.blog.51cto.com/7233352/1287582