This study is mainly in the package in addition to automatically generated Mainactivity.java, and then create a new class, Drawview, mainactivity inside call Drawview.
In Drawview, it is primarily a rewrite of the OnDraw method,----The method is responsible for drawing a small ball at the specified location of the component.
Package Org.crazyit.helloworld;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; A constructor method must be overridden public Drawview (context context) { super (context); TODO auto-generated constructor stub } //override OnDraw method canvas painting @Override public void OnDraw (canvas Canvas) { super.ondraw (canvas); Create brush Paint paint=new paint (); Paint.setcolor (color.red); Draw a small round canvas.drawcircle (CurrentX, currenty, +, paint); }}
Quote Drawview in Mainactivity.
Package Org.crazyit.helloworld;import android.app.Activity; Import Android.os.Bundle; Import Android.util.log;import android.view.MotionEvent; Import Android.view.View; Import Android.widget.LinearLayout; public class Mainactivity extends Activity {public void onCreate (Bundle savedinstancestate) {super.oncreate (s Avedinstancestate); Setcontentview (R.layout.activity_main); LinearLayout root= (linearlayout) Findviewbyid (r.id.root); Create an object that you inherit from view final Drawview drawview=new Drawview (this); Sets the maximum width and height of the custom component Drawview.setminimumwidth (300); Drawview.setminimumheight (500); Bind Tounch Event Drawview.setontouchlistener for Drawview component (new View.ontouchlistener () {public Boolean OnT Ouch (View V, motionevent event) {LOG.E ("OnTouch", "OnTouch"); Drawview.currentx=event.getx (); Drawview.currenty=event.gety (); Notifies the Drawview component to redraw DRawview.invalidate (); Returns true to indicate that the method has handled the event return true; } }); Root.addview (Drawview); }}
In the Main.xml:
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" fill_parent " android:layout_height=" Fill_parent " android:orientation= "vertical" android:background= "#00FFCC"//define background color android:id= "@+id/root" > </ Linearlayout>
Final Result:
Android footstep---ball with finger movement