============ Problem Description ============
How does Android realize that the image moves as the finger moves, and when you click on a place, the picture moves to that place. You want to give specific sample code
============ Solution 1============
Activity content:
public class CustomView extends activity{@Overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate ( Savedinstancestate); Setcontentview (R.layout.main);}}
Drawview components:
public class Drawview extends view{public float currentx = 40;public float currenty = 50;/** * @param context */public Dra Wview (context context, AttributeSet set) {Super (context, set);} @Overridepublic void OnDraw (canvas canvas) {super.ondraw (canvas);//Create brush paint p = new Paint ();//Set the color of the brush P.setcolor ( color.red);//Draw a small circle (as a ball) canvas.drawcircle (CurrentX, CurrentY, p);} @Overridepublic boolean ontouchevent (Motionevent event) {//CurrentX of the current component, currenty two properties this.currentx = Event.getx (); This.currenty = Event.gety ();//notify reorganization pieces to redraw this.invalidate ();//Returns True to indicate that the processing method has handled the event return true;}}
XML layout file:
<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" vertical "android:layout_width=" fill_parent "android:layout_height=" Fill_parent " ><!--using the custom component--><org.crazyit.event.drawviewandroid:orientation= "vertical" android:layout_width= "fill _parent "android:layout_height=" Fill_parent "/></linearlayout>
Code quoted from "Crazy Android Handout" Li Gang
How Android implements an image that moves as the finger moves