Android programming to achieve image browsing, scaling, dragging and automatic centering effect _android

Source: Internet
Author: User

This article illustrates the method of Android programming to browse, zoom, drag and automatically center the image. Share to everyone for your reference, specific as follows:

Touch.java

/** * Picture browsing, scaling, dragging, automatic centering/public class touch extends activity implements Ontouchlistener {matrix matrix = new Matrix ()
 ;
 Matrix Savedmatrix = new Matrix ();
 Displaymetrics DM;
 ImageView Imgview;
 Bitmap Bitmap; Float minscaler;//minimum scaling static final float Max_scale = 4f;//maximum scaling static final int NONE = 0;//Initial state static final in
 T DRAG = 1;//Drag the static final int ZOOM = 2;//scale 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 Resource imgview.setimagebitmap (bitmap);//Fill Control Imgview.setontouchli
  Stener (this);/Set touch screen monitor DM = new Displaymetrics ();
  Getwindowmanager (). Getdefaultdisplay (). Getmetrics (DM);//Get Resolution minzoom ();
  Center (); ImgvieW.setimagematrix (matrix); /** * Touchscreen monitor/public boolean Ontouch (View V, motionevent event) {switch (Event.getaction () & Motionevent.ac
   Tion_mask) {///main point Press case MotionEvent.ACTION_DOWN:savedMatrix.set (matrix);
   Prev.set (Event.getx (), event.gety ());
   mode = DRAG;
  Break
   Sub-point Press case MotionEvent.ACTION_POINTER_DOWN:dist = spacing (event);
    If the two-point distance is greater than 10, the multiple-point mode if (Spacing (event) > 10f) {savedmatrix.set (matrix) is determined;
    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, automatic 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 scaling, maximum 100%/private void Minzoom () {Minscaler = Math.min (float) dm.widthpixels/(float) bitm
  Ap.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) {//Picture is smaller than the screen size, the center is displayed.
   is larger than the screen, the top is left blank to move up, the bottom of the blank is moved 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); /** * Two midpoint/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

<?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>

I hope this article will help you with the Android program.

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.