Android Custom View Components

Source: Internet
Author: User

The components in Android are similar to the panel in Swxing, which is just a blank area, and for the UI component, it is just the appearance that is drawn in it.

Developers can inherit the view, customize their own components, and then override the methods in them:

Constructor: Called when Java is created or when XML creates a component

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/6B/D4/wKiom1U3nG3QO6dcAAEALZ7cMLY825.jpg "style=" float: none; "title=" 1.png "alt=" Wkiom1u3ng3qo6dcaaealz7cmly825.jpg "/>

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M01/6B/D4/wKiom1U3nG2SdpmFAAOc-e-yQMc865.jpg "style=" float: none; "title=" 2.png "alt=" Wkiom1u3ng2sdpmfaaoc-e-yqmc865.jpg "/>

Here's an example of designing a component that draws a circle where the hand touches:


To create a new Java class, add the following code:

Package Com.example.tupianliulanqi;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.view.motionevent;import Android.view.View; public class MyView extends View {private Paint p = new paint ();p rivate float myx = 0;private float MyY = 0;public MyView ( Context context) {super (context);//TODO auto-generated constructor stub}//used to redraw the component public void OnDraw (canvas canvas) { Super.ondraw (canvas);//Set brush properties P.setcolor (color.red); Canvas.drawcircle (Myx, MyY, p);} When a touch event occurs, call public boolean ontouchevent (Motionevent event) {MYX = Event.getx (); MyY = Event.gety ();// Tells that the component needs to be redrawn this.invalidate (); return true;}}


Add in activity:

public class Mainactivity extends Activity {MyView MyView; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); LinearLayout layout = (linearlayout) This.findviewbyid (r.id.main); myview = new MyView (this); Layout.addview (MyView);}



This article is from the "Useless Uncle" blog, please be sure to keep this source http://aslonely.blog.51cto.com/6552465/1637333

Android Custom View Components

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.