Android moves pictures at any point on the screen [24x24 evolution of Daming]

Source: Internet
Author: User

I read a book today and saw this effect. I used the ontouchevent (motionevent event) method before, but I didn't think it was so helpful. So I wrote it myself and it felt good, if the game is used in the future, click the screen to slide on the screen, and the picture will slide! Good results! Haha, I 'd like to share with you some inspiration. If you have any questions, please leave a message for source code. You are welcome to leave a message for discussion! Reprinted please indicate the source:

Http://blog.csdn.net/wdaming1986/article/details/6788097

Program start interface: Click the upper left corner of the screen to move the picture.

 

Click the lower-left corner of the screen, move the image, and click the lower-right corner of the screen.

Code Description:

In the touchdemo project, the com.cn. Daming package is as follows:

I. mainactivity. Java code:

Package com.cn. daming; import android. app. activity; import android. graphics. color; import android. graphics. drawable. gradientdrawable; import android. graphics. drawable. gradientdrawable. orientation; import android. OS. bundle; import android. util. displaymetrics; import android. util. log; import android. view. motionevent; import android. view. view; import android. widget. absolutelayout; import android. widget. button; import android. widget. imageview; import android. widget. toast; public class mainactivity extends activity {/* declare imageview variable */private imageview mimageview01;/* declare related variables as storage image width and height, and use */private int intwidth, intheight, intdefaultx, intdefaulty; private float MX, my;/* declare the resolution variable */private int intscreenx, intscreeny; Public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); drawbackground ();/* Get the Screen Object */displaymetrics dm = new displaymetrics (); getwindowmanager (). getdefadisplay display (). getmetrics (DM);/* Get screen resolution pixel */intscreenx = DM. widthpixels; intscreeny = DM. heightpixels;/* set the image width and height */intwidth = 100; intheight = 100;/* Create an imageview object through the findviewbyid constructor */mimageview01 = (imageview) findviewbyid (R. id. myimageview1);/* assign the image value from drawable to imageview for rendering */mimageview01.setimageresource (R. drawable. body);/* center the initialization button position */restorebutton ();/* restore the initial position */mimageview01.setonclicklistener (New button. onclicklistener () {public void onclick (view v) {restorebutton () ;}}) ;}// loads the background color public void drawbackground () {gradientdrawable grad = new gradientdrawable (orientation. tl_br, new int [] {color. RGB (0, 0,127), color. RGB (0, 0,255), color. RGB (127, 0,255), color. RGB (127,127,255), color. RGB (127,255,255), color. RGB (255,255,255)}); this. getwindow (). setbackgrounddrawable (GRAD);}/* override touch event */Public Boolean ontouchevent (motionevent event) {/* obtain the position of the finger touch screen */float x = event. getx (); float y = event. gety (); try {/* handle touch events */switch (event. getaction () {/* click the screen */case motionevent. action_down: picmove (x, y); break;/* move position */case motionevent. action_move: picmove (x, y); break;/* exit screen */case motionevent. action_up: picmove (x, y); break ;}} catch (exception e) {e. printstacktrace ();} return true;}/* method for moving an image */private void picmove (float X, float y) {/* the relative position of the image and pointer is fine-tuned by default */MX = x-(intwidth/2); my = Y-(intheight/2 ); /* processing of image protection against screen exceeding * // * preventing screen exceeding to the right */If (mx + intwidth)> intscreenx) {MX = intscreenx-intwidth ;} /* prevent the screen from going left over the screen */else if (MX <0) {MX = 0 ;} /* prevent the screen from going down the screen */else if (My + intheight)> intscreeny) {My = intscreeny-intheight ;} /* prevent the screen from going up to the screen */else if (My <0) {My = 0;}/* view the image position through log */log. I ("Jay", float. tostring (MX) + "," + float. tostring (my);/* use the setlayoutparams method to reschedule the location on Layout */mimageview01.setlayoutparams (New absolutelayout. layoutparams (intwidth, intheight, (INT) MX, (INT) My);}/* restore imageview location event processing */Public void restorebutton () {intdefaultx = (intscreenx-intwidth)/2); intdefaulty = (intscreeny-intheight)/2 ); /* toast restore coordinate */mmaketexttoast ("(" + integer. tostring (intdefaultx) + "," + integer. tostring (intdefaulty) + ")", true);/* use the setlayoutparams method to reschedule the location on Layout */mimageview01.setlayoutparams (New absolutelayout. layoutparams (intwidth, intheight, intdefaultx, intdefaulty);}/* Custom Method for sending information */Public void mmaketexttoast (string STR, Boolean islong) {If (islong = true) {toast. maketext (mainactivity. this, STR, toast. length_long ). show ();} else {toast. maketext (mainactivity. this, STR, toast. length_short ). show ();}}}

 

Ii. Code in the main. xml layout file:

<?xml version="1.0" encoding="utf-8"?><AbsoluteLayout  android:id="@+id/widget27"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  xmlns:android="http://schemas.android.com/apk/res/android"  >  <TextView    android:id="@+id/myTextView"        android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:layout_x="80dip"    android:text="@string/hello"    />  <ImageView    android:id="@+id/myImageView1"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    >  </ImageView></AbsoluteLayout>

 

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.