First, customize a view subclass:
Package Com.example.androidtest0.myview;import Android.content.context;import Android.graphics.canvas;import Android.graphics.color;import Android.graphics.paint;import Android.util.attributeset;import Android.view.motionevent;import Android.view.view;public class Drawview extends View {public float CurrentX = 40;public F Loat currenty = 50;//definition, and create brush paint p = new paint ();p ublic Drawview (context context) {super (context);} Public Drawview (context context, AttributeSet Attrs) {Super (context, attrs);} @Overrideprotected void OnDraw (canvas canvas) {super.ondraw (canvas);//Set the brush's color P.setcolor (color.red);// Draw a small ball canvas.drawcircle (CurrentX, CurrentY, p);} /** * Override event handling method for the component's Touch event */@Overridepublic Boolean ontouchevent (Motionevent event) {//modify CurrentX, currenty two properties CurrentX = Event.getx (); currenty = Event.gety ();//Notifies the current component to redraw itself invalidate (); return true;} }
Main interface xml:
Custom_layout.xml
<pre name= "code" class= "java" ><?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= " Http://schemas.android.com/apk/res/android " android:layout_width=" match_parent " android:layout_height= "Match_parent" android:id= "@+id/root" android:orientation= "vertical" ></LinearLayout>
Main activity:
Package Com.example.androidtest0;import Com.example.androidtest0.myview.drawview;import android.app.Activity; Import Android.os.bundle;import Android.widget.linearlayout;public class CustomView extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.custom_layout);//Get the LinearLayout container in the layout file linearlayout root = (linearlayout) Findviewbyid (r.id.root);// Create Drawview component Final Drawview Drawview = new Drawview (this);//Set the minimum width of the custom component, Height drawview.setminimumwidth (10); Drawview.setminimumheight (Root.addview); (Drawview);}
Effect:
Besides:
It can also be done in XML: It is also the first subclass of a view, as in the above. Then the main interface XML is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:id=" @+id /root " android:orientation=" vertical "> <com.example.androidtest0.myview.drawview android: Layout_width= "Match_parent" android:layout_height= "match_parent" /></linearlayout>
The main activity file is as follows:
Package Com.example.androidtest0;import Com.example.androidtest0.myview.drawview;import android.app.Activity; Import Android.os.bundle;import Android.widget.linearlayout;public class CustomView extends Activity {@ overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( r.layout.custom_layout);}}
Android custom components automatically draw circles with your fingers