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