Implementation:
Implementation Method:
The inherited View Class re-draws a circle by itself, adds the custom inherited View class to the current activity, and adds an ontouchlitener event to the custom view control.
Custom components inherit from the View class:
Package COM. jiahui. draw; import android. content. context; import android. graphics. canvas; import android. graphics. color; import android. graphics. paint; import android. view. view; public class drawview extends view {public float currentx = 40; public float currenty = 50; // You must override a constructor public drawview (context) {super (context ); // todo auto-generated constructor stub} // rewrite the ondraw method by painting with canvas @ overrideprotected void ondraw (canvas) {super. ondraw (canvas); // create paint = new paint (); paint. setcolor (color. red); // draw a small circle canvas. drawcircle (currentx, currenty, 25, paint );}}
Add custom components to the current activity
Package COM. jiahui. draw; import android. app. activity; import android. OS. bundle; import android. view. motionevent; import android. view. view; import android. widget. linearlayout; public class customeviewactivity extends activity {public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); linearlayout root = (linearlayout) findviewbyid (R. id. root); // create an object that inherits from the view, final drawview = new drawview (this); // set the maximum width and height of the custom component drawview. setminimumwidth (300); drawview. setminimumheight (500); drawview. setontouchlistener (new view. ontouchlistener () {public Boolean ontouch (view V, motionevent event) {drawview. currentx = event. getx (); drawview. currenty = event. gety (); // redraw drawview. invalidate (); // return true indicates that the method has handled the event return true;}); root. addview (drawview );}}
Main. xml:
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:background="#99FFCC" android:id="@+id/root"></LinearLayout>
If reprint please declare the Article Source: http://blog.csdn.net/jiahui524
Source code download: http://download.csdn.net/detail/jiahui524/3786788