Android tracking Ball-gesture Move picture-custom control (with source code)

Source: Internet
Author: User
Tags gety

Because I don't make animated pictures, I put a few and a few representative pictures of them first.

My previous train of thought was to identify by gesture movement through the dynamic setting of XY coordinates, but I later tried it and found that the performance was very poor. So snatch made the following ball control, in fact, the implementation is very simple. As long as you are familiar with the use of custom controls and gesture recognition. Basically, it's OK.

Now let's look at the source code for this control Touchmoveview.java

 PackageCom.fay.touchmove;ImportAndroid.annotation.SuppressLint;ImportAndroid.content.Context;ImportAndroid.graphics.Bitmap;Importandroid.graphics.BitmapFactory;ImportAndroid.graphics.Canvas;ImportAndroid.util.AttributeSet;ImportAndroid.util.Log;Importandroid.view.MotionEvent;ImportAndroid.view.View;/*** Moving by gesture *@authorFay *@since2014/5/27 * {@link[email protected]}*/ Public classTouchmoveviewextendsView {PrivateString TAG = "Touchmoveview"; /*** The default bitmap for the Touchmoveview*/    PrivateBitmap Defaultbitmap =Bitmapfactory.decoderesource (Getresources (), r.drawable.ic_launcher); /*** The width of the default bitmap*/    Private intwidth =defaultbitmap.getwidth (); /*** The height of the default bitmap*/    Private intHeight =defaultbitmap.getheight (); /*** The x-location of the bitmap*/    Private floatxlocation = 0; /*** The y-location of the bitmap*/    Private floatylocation = 0;  PublicTouchmoveview (context context, AttributeSet attrs,intdefstyleattr) {        Super(context, attrs, defstyleattr); }     PublicTouchmoveview (Context context, AttributeSet attrs) {Super(context, attrs); }     PublicTouchmoveview (Context context) {Super(context); } @Overrideprotected voidOnDraw (canvas canvas) {Super. OnDraw (canvas); Canvas.drawbitmap (Defaultbitmap, Xlocation, Ylocation,NULL); } @SuppressLint ("Newapi") @Override Public Booleanontouchevent (Motionevent event) {Switch(Event.getaction ()) { CaseMotionevent.action_down:floatx =Event.getx (); floaty =event.gety (); if(xlocation <= x && x <= xlocation + width && ylocation <= y && y <= ylocation +height) {                //Continue}Else {                return false;//End the event            }             Break;  CaseMotionEvent.ACTION_MOVE:xLocation=Event.getx (); Ylocation=event.gety ();  Break;  Casemotionevent.action_up: Break;        } invalidate (); return true; }     PublicBitmap Getdefaultbitmap () {returnDefaultbitmap; }         Public voidSetdefaultbitmap (Bitmap defaultbitmap) { This. Defaultbitmap =Defaultbitmap; //Update the width and the height of the default bitmapwidth =defaultbitmap.getwidth (); Height=defaultbitmap.getheight (); }     Public floatgetxlocation () {returnxlocation; }    /*** Set the initialized x-location *@paramint xlocation*/     Public voidSetxlocation (floatxlocation) {         This. xlocation =xlocation; }     Public floatgetylocation () {returnylocation; }        /*** Set the initialized y-location *@paramint Ylocation*/     Public voidSetylocation (floatylocation) {         This. ylocation =ylocation; }    }

And look at this mainactivity.java.

 PackageCom.fay.touchmove;ImportAndroid.annotation.SuppressLint;Importandroid.app.Activity;Importandroid.graphics.BitmapFactory;Importandroid.os.Bundle; @SuppressLint ("Newapi") Public classMainactivityextendsActivity {PrivateString TAG = "Mainactivity"; PrivateTouchmoveview Mtouchmoveview =NULL; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Mtouchmoveview=(Touchmoveview) Findviewbyid (R.id.imageview); //Mtouchmoveview.setdefaultbitmap (Bitmapfactory.decoderesource (Getresources (), r.drawable.lock_selected)); //mtouchmoveview.setxlocation (100); //mtouchmoveview.setylocation (+);    }}

We should be able to see that in this activity it is easy to set the moving picture, as well as the initialized x, y coordinates. Moving through continuous drawing. Note that this control is full-screen. So that's something to understand.
The whole process is simple and convenient. I hope to help some friends.

SOURCE Download: Http://files.cnblogs.com/yinweiliang/TouchMove.zip

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.