Multi-touch in Android learning is not mysterious

Source: Internet
Author: User
Tags gety

I recently studied multi-touch and wrote a program that uses multi-touch to control image size and single-touch to control image movement.

 

In Android, The ontouchevent method is used to listen to touch events. Its parameter is motionevent. The following lists some common motionevent methods:

Getpointercount () gets the number of points on the touch screen.

Getx () obtains the X coordinate value of the touch screen.

Gety () obtains the Y coordinate value of the touch screen.

Getaction () obtains the touch screen action.

Action_down: start with the action to be pressed. For example, you can press the screen with your finger.

Action_up: the action of pressing is completed. For example, you can stop pressing the screen with your finger and exit the screen.

Action_move: The movement between the start and completion of an action. For example, the finger slides on the screen.

 

This section also describes the four parameters used in the program, imageview. setframe (), which refer to left, top, right, and bottom.

Left and top refer to the coordinates x and y and right in the upper left corner of imageview. Bottom refers to the coordinates x and y in the lower right corner of imageview.

Next, let's look at the program. The program has a detailed introduction:

 

Package COM. practice. imageviewpic; <br/> Import android. app. activity; <br/> Import android. content. context; <br/> Import android. graphics. *; <br/> Import android. graphics. drawable. bitmapdrawable; <br/> Import android. OS. bundle; <br/> Import android. view. motionevent; <br/> Import android. widget. imageview; <br/> Import android. widget. imageview. scaletype; <br/> public class imageviewpic extends activity {</P> <p>/* <br/> * use multi-touch to control image enlargement and reduction in imageview <br/> * finger-controlled image movement <br/> */</P> <p> private myimageview imageview; <br/> private Bitmap bitmap; </P> <p> // length between two touch screens <br/> private float beforelenght; <br/> private float afterlenght; </P> <p> // coordinates of single point movement <br/> private float afterx and aftery; <br/> private float beforex and beforey; </P> <p>/** called when the activity is first created. */<br/> @ override <br/> Public void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> findview (); <br/> setcontentview (imageview); <br/> config (); <br/>}</P> <p> private void findview () {<br/> imageview = new myimageview (this ); <br/> // obtain the image <br/> bitmap = (bitmapdrawable) getresources (). getdrawable (R. drawable. xing )). getbitmap (); <br/>}</P> <p> private void config () {<br/> // sets the imageview display image. <br/> imageview. setimagebitmap (Bitmap); <br/> // sets the image to fill the imageview. <br/> imageview. setscaletype (scaletype. fit_xy ); <br/>}</P> <p> // create an imageview class <br/> class myimageview extends imageview {<br/> private float scale = 0.1f; </P> <p> Public myimageview (context) {<br/> super (context ); <br/>}< br/> // used to set the location of the imageview <br/> private void setlocation (int x, int y) {<br/> This. setframe (this. getleft () + X, this. gettop () + Y, this. getright () + X, this. getbottom () + y ); <br/>}</P> <p>/* <br/> * used to zoom in and out the imageview <br/> * because the image is filled with the imageview, therefore, the image can be zoomed in or out. <br/> * If the flag is 0, the image will be zoomed in, 1 is smaller than the image <br/> */<br/> private void setscale (float temp, int flag) {</P> <p> If (flag = 0) {<br/> This. setframe (this. getleft ()-(INT) (temp * This. getwidth (), <br/> This. gettop ()-(INT) (temp * This. getheight (), <br/> This. getright () + (INT) (temp * This. getwidth (), <br/> This. getbottom () + (INT) (temp * This. getheight (); <br/>}else {<br/> This. setframe (this. getleft () + (INT) (temp * This. getwidth (), <br/> This. gettop () + (INT) (temp * This. getheight (), <br/> This. getright ()-(INT) (temp * This. getwidth (), <br/> This. getbottom ()-(INT) (temp * This. getheight ())); <br/>}</P> <p> // draw a border <br/> @ override <br/> protected void ondraw (canvas) {<br/> super. ondraw (canvas); <br/> rect rec = canvas. getclipbounds (); <br/> rec. bottom --; <br/> rec. right --; <br/> paint = new paint (); <br/> paint. setcolor (color. red); <br/> paint. setstyle (paint. style. stroke); <br/> canvas. drawrect (REC, paint ); <br/>}</P> <p>/* move an image following the touch screen of your finger <br/> * beforex and Y are used to save the coordinates of the previous position. <br/> * afterx and Y are used to save the coordinates of the current position <br/> * their difference is the increase or decrease of the coordinates of imageview <br/> */<br/> Public void movewithfinger (motionevent event) {</P> <p> switch (event. getaction () {</P> <p> case motionevent. action_down: <br/> beforex = event. getx (); <br/> beforey = event. gety (); <br/> break; <br/> case motionevent. action_move: <br/> afterx = event. getx (); <br/> aftery = event. gety (); </P> <p> This. setlocation (INT) (afterx-beforex), (INT) (aftery-beforey); </P> <p> beforex = afterx; <br/> beforey = aftery; <br/> break; </P> <p> case motionevent. action_up: <br/> break; <br/>}</P> <p>/* <br/> * use a multi-point touch screen to zoom in or out the image <br/> * beforelenght before saving distance between two points at a time <br/> * afterlenght is used to save the distance between two points at the current time <br/> */<br/> Public void scalewithfinger (motionevent event) {<br/> float movex = event. getx (1)-event. getx (0); <br/> float Movey = event. gety (1)-event. gety (0); </P> <p> switch (event. getaction () {<br/> case motionevent. action_down: <br/> beforelenght = (float) math. SQRT (movex * movex) + (Movey * Movey); <br/> break; <br/> case motionevent. action_move: <br/> // get the length between two points <br/> afterlenght = (float) math. SQRT (movex * movex) + (Movey * Movey); </P> <p> float gaplenght = afterlenght-beforelenght; </P> <p> If (gaplenght = 0) {<br/> break; <br/>}</P> <p> // if the distance between the two points in the current time is greater than the distance between the two points in the previous time, 0 is passed, otherwise, 1 <br/> If (gaplenght> 0) {<br/> This. setscale (scale, 0); <br/>} else {<br/> This. setscale (scale, 1); <br/>}</P> <p> beforelenght = afterlenght; <br/> break; <br/>}</P> <p> // monitor the screen touch time <br/> @ override <br/> Public Boolean ontouchevent (motionevent event) {</P> <p>/* <br/> * determines whether an image is touched. <br/> * If the image is a single touch, call the method for controlling image movement. <br/> * if it is a 2-point touch, call the method to control the image size <br/> */<br/> If (event. gety ()> imageview. gettop () & event. gety () <imageview. getbottom () <br/> & event. getx ()> imageview. getleft () & event. getx () <imageview. getright () {<br/> If (event. getpointercount () = 2) {<br/> imageview. scalewithfinger (event); <br/>} else if (event. getpointercount () = 1) {<br/> imageview. movewithfinger (event); <br/>}< br/> return true; <br/>}</P> <p>} 

 

Source program: http://download.csdn.net/source/3281618

 

Thank you. Today is the time.

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.