A custom component is a component that gets personalized color, rendering, and behavior by inheriting the view class and overriding the view class's methods.
A circle:
Public classSelfviewextendsView {floatCurrentx=40; floatCurrenty=50;
The coordinate is of type float. Paint Paint=NewPaint ();
Brushes, PublicSelfview (Context context) {Super(context); } PublicSelfview (Context context,attributeset set) {Super(Context,set); }
These are the two constructors of the Selfview class
Overrides the OnDraw () method of the view class whose parameters are the canvas type @Override Public voidOnDraw (canvas canvas) {Super. OnDraw (canvas);
Specifies the brush color paint.setcolor (color.blue);
Drawing graphic canvas.drawcircle (Currentx,currenty,25, paint); }
@Override Public Booleanontouchevent (motionevent motionevent) {
Gets the new coordinate position under touch CurrentX=Motionevent.getx (); CurrentY=motionevent.gety ();
Notifies the current component to redraw itself, why not pass This.ondraw (), or OnDraw ()? invalidate (); return true; }}
Add a custom Selfview component as you would add a system component:
Selfview selfview=New Selfview (this); Root.addview (Selfview);
It can also be in XML:
<com.xxx.xxx.xxx. Selfview android:layout_width= "Wrap_content" android:layout_height= "Wrap_content"/>
Customizing the View Component