Customizing the View Component

Source: Internet
Author: User

  

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.