Android custom View must inherit the View and override constructors, onDraw, (onMeasure) and other functions.
If a custom View requires custom attributes, you must create attrs. xml under values. Define your attributes.
Add xmlns: prefix = "http://schemas.android.com/apk/res/your custom view package path" to the xml layout file that uses the custom View ".
When using custom attributes, use the prefix: attribute name, for example, my: textColor = "# FFFFFFF ".
[Java]
Package com. example. cumsview
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.
* At least the constructor and onDraw methods must be reloaded.
* If custom views do not have their own unique attributes, they can be directly used in xml files.
* If you have unique attributes, You need to obtain the custom attribute name in the attrs. xml Attribute file in the constructor.
* Set the default value as needed, which is not defined in the xml file.
* If you use custom attributes, you need to add a new schemas to the application xml file,
* For example, here is xmlns: my = "http://schemas.android.com/apk/res/demo.view.my"
* "My" after xmlns is the prefix of the custom attribute, and "res" is the package where the custom View is located.
* @ Author Administrator
*
*/
Public class MyView extends View {
Paint mPaint; // Paint brush, which contains the style and color information of the painting ry, text, etc.
Public MyView (Context context ){
Super (context );
}
Public MyView (Context context, AttributeSet attrs ){
Super (context, attrs );
MPaint = new Paint ();
// TypedArray is an array used to store attributes obtained by context. obtainStyledAttributes.
// Call the recycle method after use.
// The attribute name is the name + "_" + attribute name in styleable
TypedArray array = context. obtainStyledAttributes (attrs, R. styleable. MyView );
Int textColor = array. getColor (R. styleable. MyView_textColor, 0XFF00FF00); // provides the default value, which is not specified
Float textSize = array. getDimension (R. styleable. MyView_textSize, 36 );
MPaint. setColor (textColor );
MPaint. setTextSize (textSize );
Array. recycle (); // It must be called. Otherwise, this setting will affect the next use.
}
Public void onDraw (Canvas canvas ){
Super. onDraw (canvas );
// Canvas contains many drawing interfaces. Using these interfaces, we can draw the image we want.
// MPaint = new Paint ();
// MPaint. setColor (Color. RED );
MPaint. setStyle (Style. FILL); // set Filling
Canvas. drawRect (10, 10,100,100, mPaint); // draw a rectangle
MPaint. setColor (Color. BLUE );
Canvas. drawText ("I am drawn", 10,120, mPaint );
}
}
Corresponding property file: attrs. xmlcopy
<? 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/com.example.cumsview"
Android: orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
>
<Com. example. cumsview. MyView
Android: layout_width = "fill_parent"
Android: layout_height = "wrap_content"
My: textColor = "# FFFFFFFF"
My: textSize = "22dp"
/>
</LinearLayout>
MainActivity. java
Package com. example. cumsview;
Import android. OS. Bundle;
Import android. app. Activity;
Import android. view. Menu;
Public class MainActivity extends Activity {
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
@ Override
Public boolean onCreateOptionsMenu (Menu menu ){
GetMenuInflater (). inflate (R. menu. activity_main, menu );
Return true;
}
}
The effect is as follows: