Browse, zoom, drag and Auto center of Android developed images

Source: Internet
Author: User
Tags gety prev touch

Touch.java

The code is as follows Copy Code

/**
* Picture browsing, zooming, dragging, centering automatically
*/
public class touch extends activity implements Ontouchlistener {

Matrix matrix = new Matrix ();
Matrix Savedmatrix = new Matrix ();
Displaymetrics DM;
ImageView Imgview;
Bitmap Bitmap;

Float minscaler;//Minimum zoom ratio
Static final Float Max_scale = 4f;//maximum Scaling

static final int NONE = 0;//initial state
static final int DRAG = 1;//drag
static final int ZOOM = 2;//Scaling
int mode = NONE;

PointF prev = new PointF ();
PointF mid = new PointF ();
Float dist = 1f;

@Override
public void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.scale);
Imgview = (ImageView) Findviewbyid (R.ID.IMAG);//Get control
Bitmap = Bitmapfactory.decoderesource (Getresources (), This.getintent ()
. Getextras (). GetInt ("IMG");//Get Picture resources
Imgview.setimagebitmap (bitmap)//Fill control
Imgview.setontouchlistener (this);/Set Touch screen monitor
DM = new Displaymetrics ();
Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);//Get Resolution
Minzoom ();
Center ();
Imgview.setimagematrix (matrix);
}

/**
* Touch screen Monitor
*/
public boolean Ontouch (View V, motionevent event) {

Switch (event.getaction () & Motionevent.action_mask) {
The main point is pressed
Case Motionevent.action_down:
Savedmatrix.set (matrix);
Prev.set (Event.getx (), event.gety ());
mode = DRAG;
Break
Click on the secondary point
Case Motionevent.action_pointer_down:
Dist = spacing (event);
If the two-point distance is greater than 10, then the multiple point pattern is determined.
if (Spacing (event) > 10f) {
Savedmatrix.set (matrix);
Midpoint (Mid, event);
mode = ZOOM;
}
Break
Case MOTIONEVENT.ACTION_UP:
Case MOTIONEVENT.ACTION_POINTER_UP:
mode = NONE;
Break
Case Motionevent.action_move:
if (mode = = DRAG) {
Matrix.set (Savedmatrix);
Matrix.posttranslate (Event.getx ()-prev.x, Event.gety ()
-PREV.Y);
else if (mode = = ZOOM) {
float newdist = spacing (event);
if (Newdist > 10f) {
Matrix.set (Savedmatrix);
float TScale = newdist/dist;
Matrix.postscale (TScale, TScale, mid.x, MID.Y);
}
}
Break
}
Imgview.setimagematrix (matrix);
Checkview ();
return true;
}

/**
* Limit maximum minimum scaling, auto Center
*/
private void Checkview () {
float p[] = new FLOAT[9];
Matrix.getvalues (P);
if (mode = = ZOOM) {
if (P[0] < Minscaler) {
Matrix.setscale (Minscaler, Minscaler);
}
if (P[0] > Max_scale) {
Matrix.set (Savedmatrix);
}
}
Center ();
}

/**
* Minimum zoom ratio, maximum is 100%
*/
private void Minzoom () {
Minscaler = Math.min (
(float) dm.widthpixels/(float) bitmap.getwidth (),
(float) dm.heightpixels/(float) bitmap.getheight ());
if (Minscaler < 1.0) {
Matrix.postscale (Minscaler, Minscaler);
}
}

private void Center () {
Center (true, true);
}

/**
* Horizontal, Vertical Center
*/
protected void Center (Boolean horizontal, Boolean vertical) {

Matrix M = new Matrix ();
M.set (matrix);
RECTF rect = new RECTF (0, 0, bitmap.getwidth (), Bitmap.getheight ());
M.maprect (rect);

float height = rect.height ();
Float width = rect.width ();

Float deltax = 0, DeltaY = 0;

if (vertical) {
If the picture is smaller than the screen size, it is centered. Larger than screen, left above left blank to move up, drop down
int screenheight = Dm.heightpixels;
if (height < screenheight) {
DeltaY = (screenheight-height)/2-rect.top;
else if (Rect.top > 0) {
DeltaY =-rect.top;
else if (Rect.bottom < screenheight) {
DeltaY = Imgview.getheight ()-rect.bottom;
}
}

if (horizontal) {
int screenwidth = Dm.widthpixels;
if (Width < screenwidth) {
DeltaX = (screenwidth-width)/2-rect.left;
else if (Rect.left > 0) {
DeltaX =-rect.left;
else if (Rect.right < screenwidth) {
DeltaX = Screenwidth-rect.right;
}
}
Matrix.posttranslate (DeltaX, DeltaY);
}

/**
* Two-point distance
*/
Private float spacing (motionevent event) {
float x = event.getx (0)-event.getx (1);
Float y = event.gety (0)-event.gety (1);
return FLOATMATH.SQRT (x * x + y * y);
}

/**
* Midpoint of two points
*/
private void Midpoint (PointF point, motionevent event) {
float x = event.getx (0) + event.getx (1);
Float y = event.gety (0) + event.gety (1);
Point.set (X/2, Y/2);
}
}

Scale.xml

  code is as follows copy code

<?xml Version= "1.0" encoding= "Utf-8" "
<framelayout xmlns:android=" http://schemas.android.com/apk/res/android "
    android:layout_width=" fill_parent "
    android:layout_height=" Fill_ Parent "
    android:layout_gravity=" center "

    <imageview
        android:id= "@+id/imag"
        Android:layout_width= "Fill_parent"
        android:layout_height= "Fill_ Parent
        android:layout_gravity= "center"
         android:scaletype= "Matrix"
    </imageview>

</ Framelayout>

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.