public class Drawview extends view{public float currentx=40; public float currenty=50; Public Drawview (Context context) {super (context); }//rewrite OnDraw, protected void OnDraw (canvas canvas) {Paint paint=new paint () through canvas painting; Paint.setcolor (color.red); Canvas.drawcircle (Currentx,currenty,25,paint); }
<?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:o rientation= "vertical" android:background= "#99FFCC" android:id= "@ +id/root "></LINEARLAYOUT>
public class mainactivity extends activity{ @Override protected void oncreate (bundle savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); linearlayout root= (linearlayout) findviewbyid (r.id.root); final Drawview drawview=new drawview (this); windowmanager wm = this.getwindowmanager (); int width = wm.getdefaultdisplay (). getwidth (); int height = wm.getdefaultdisplay (). GetHeight (); toast.maketext (This, "Width is" +width+ "Height "+height,toast.length_long). Show (); Drawview.setminimumheight (+); drawview.setminimumheight (500); drawview.setontouchlistener (New View.OnTouchListener () { @Override public boolean ontouch (View v, MotionEvent event) { drawview.currentx=event.getx (); drawview.currenty=event.gety (); drawview.invalidate (); return true; } }); root.addview (Drawview); }}
This article is from the "Java White Battlefield" blog, be sure to keep this source http://8023java.blog.51cto.com/10117207/1726924
Simple application of view in Android---small ball that moves with your finger