Android Custom View implementation is simple
Inherit view, override functions such as constructors, OnDraw, (onmeasure), and so on.
If a custom view needs to have a custom attribute, you need to create a attrs.xml under values. In which you define your properties.
In an XML layout file that uses a custom view, you need to include xmlns: prefix = "http://schemas.android.com/apk/res/the package path where your custom view is located."
When using custom attributes, use the prefix: property name, such as my:textcolor= "#FFFFFFF".
Instance:
-
package demo.view.my; import android.content.context; import Android.content.res.typedarray; import android.graphics.canvas; import android.graphics.color; import android.graphics.paint; import android.graphics.Paint.Style; import android.util.AttributeSet; import android.view.view; /** * This is a custom textview. . * requires at least overloaded construction methods and OnDraw methods * for custom view without its own unique properties, you can use it directly in the XML file * If you have a unique property, you need to get the name of the custom property in the property file Attrs.xml in the constructor * and set the default value as needed, which is not defined in the XML file. * If you use a custom attribute, you need to add a new schemas, * to the application XML file, such as this is xmlns:my= "HTTP/ schemas.android.com/apk/res/demo.view.my " * which xmlns after the" my "is a custom attribute prefix, after res is our custom view package * @author administrator * */ public class myview extends view { A paint mpaint; //brush that contains style and color information for drawing geometry, text, and so on public myview ( Context context) { super (context); } public myview (Context context, attributeset attrs) { super (context, attrs); mpaint = new paint (); // Typedarray is a store used by Context.obtainstyleDattributes an array of properties obtained //after use is complete, be sure to call the Recycle method The name of the //property is the name in Styleable + "_" + Property name TypedArray array = Context.obtainstyledattributes (Attrs, r.styleable.myview); int textcolor = array.getcolor (r.styleable.myview_textcolor, 0XFF00FF00); //provides default values to place unspecified float Textsize = array.getdimension (r.styleable.myview_textsize, 36); mpaint.setcolor (TextColor); mpaint.settextsize (textSize); &nbsP; array.recycle (); //must be called, otherwise this setting will affect the next use } public void OnDraw (Canvas canvas) { super.ondraw ( Canvas), //canvas contains a lot of drawing interfaces, using these interfaces, We can draw the graphics we want //mPaint = new Paint (); //mpaint.setcolor (Color.RED); mpaint.setstyle (Style.FILL); //Set fill canvas.drawrect (10, 10, 100, 100, mpaint); //Draw Rectangle mpaint.setcolor (Color.Blue); Canvas.drawtext ("I was painted", 10, 120, mpaint); } }
Corresponding properties file:
-
<?xml version= "1.0" encoding= "Utf-8"?> <resources> <declare-styleable name= "MyView" > < attr name= "TextColor" format= "color"/> <attr name= "textSize" format= "Dimension"/> </declare-styl Eable> </resources>
To use in a layout file:
<?xml version= "1.0" encoding= "Utf-8"? > <linearlayout xmlns:android= "http// Schemas.android.com/apk/res/android " xmlns:my= "http://schemas.android.com/apk/res/demo.view.my" android:orientation= "Vertical" android: Layout_width= "Fill_parent" android:layout_height= "Fill_parent" > < Demo.view.my.myview android:layout_width= "Fill_ Parent " android:layout_height=" Wrap_content " my:textcolor= "#FFFFFFFF" my:textsize= "22DP" / > </LinearLayout>
This article is from the "focus_000" blog, so be sure to keep this source http://120806872.blog.51cto.com/8289253/1437394