Android Picture viewer (images can be moved, scaled)

Source: Internet
Author: User

There are several ways to move and zoom a picture after a finger is clicked, which is achieved through Ontouch.

The instance code is as follows:

First, there's a ImageView in the view.

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    android:paddingbottom= "@dimen/activity_vertical_margin"    android:paddingleft= "@dimen/activity_ Horizontal_margin "    android:paddingright=" @dimen/activity_horizontal_margin "    android:paddingtop=" @dimen /activity_vertical_margin "    tools:context=". Mainactivity ">    <imageview        android:id=" @+id/iv "        android:scaletype=" Matrix "        android: Layout_width= "Wrap_content"        android:layout_height= "wrap_content"        android:src= "@drawable/b"/></ Relativelayout>

Then set the ImageView for the OnTouchClick events

Iv.setontouchlistener (this);

Rewrite Ontouch

Private PointF StartPoint = new PointF ();//Get the original coordinates of the picture//set touch mode private int mode = 0, single = 1, multi = 2;//Set the click Mode, is a one touch or multipoint private matrix matrix = new Matrix ();p rivate matrix Currentmatrix = new Matrix ();p rivate float startdistance; Two o'clock starting at the distance private PointF midpoint; Center position of two points


@Overridepublic boolean OnTouch (View V, motionevent event) {switch (Event.getaction () & Motionevent.action_mask) { Case motionevent.action_down://indicates that the user is starting to touch mode = Single;currentmatrix.set (Iv.getimagematrix ()); Startpoint.set ( Event.getx (), Event.gety ()) Break;case motionevent.action_pointer_down://when there is already a contact (finger) on the screen, and another finger presses the screen mode = multi; Startdistance = Getdistance (event); Getmidpoint (event); Currentmatrix.set (Iv.getimagematrix ()); midpoint = Getmidpoint (event); Break;case motionevent.action_move://the finger moves on the screen, the event will constantly trigger if (mode = = single) {Float x = Event.getx ()-S Tartpoint.x;float y = event.gety ()-Startpoint.y;matrix.set (Currentmatrix); matrix.posttranslate (x, y);} else if (mode = = multi) {Float enddistance = getdistance (event); if (Enddistance > 10f) {float sclace = enddistance/st Artdistance;matrix.set (Currentmatrix); Matrix.postscale (Sclace, Sclace, Midpoint.x, Midpoint.y);}} Break;case motionevent.action_up://Fingers leave the screen case motionevent.action_pointer_up://when one finger leaves the screen, but there are still contacts on the screen. mode = 0;break;d Efault:break;} Iv.setimagematrix (matrix); return true;}

Here mode is used to determine what type of touch is currently used to move a picture using a single finger, and multi represents a two-finger operation for image scaling.

To move a picture:

Currentmatrix represents the current position of the picture when clicked

StartPoint is the coordinate point of the picture when clicked

Matrix.set (Currentmatrix); matrix.posttranslate (x, y);

First get the position of the picture and then pan, the distance of the shift is the distance the finger moves.

Image zoom:

As the image is scaled to be implemented with two fingers, use the motionevent.action_pointer_down

startdistance is the distance between two fingers at the start of a press.

Midpoint is the center of the two-point press.

Matrix.postscale (Sclace, Sclace, Midpoint.x, midpoint.y); These parameters represent the scale of the x-axis and y-axis scaling, and the scaling begins at that coordinate point.

Here are getmidpoint and getdistance .

Private PointF Getmidpoint (Motionevent event) {float midx= (event.getx (0) +event.getx (1))/2; float midy= (event.gety (0) + Event.gety (1))/2; return new PointF (MIDX, midy);} Private float Getdistance (motionevent event) {float dx=event.getx (1)-event.getx (0); float dy=event.gety (1)-event.gety (0); return floatmath.sqrt (Dx*dx+dy*dy);}



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.