How Android implements an image that moves as the finger moves

Source: Internet
Author: User

============ 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.