Android Image Browsing, scaling, dragging, and auto center

Source: Internet
Author: User
Tags gety

Http://www.cnblogs.com/dwinter/archive/2012/01/12/2321082.html

Activity:

/*** Image Browsing, scaling, dragging, automatic center */public class touch extends activity implements ontouchlistener {matrix = new matrix (); matrix savedmatrix = new matrix (); displaymetrics DM; imageview imgview; Bitmap bitmap; float minscaler; // static final float max_scale = 4f; // static final int none = 0; // initial state static final int drag = 1; // drag static final int zoom = 2; // zoom int mode = none; pointf PR Ev = 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); // obtain the control bitmap = bitmapfactory. decoderesource (getresources (), this. getintent (). getextras (). getint ("IMG"); // obtain the image resource imgview. setimagebitmap (Bitmap); // fill control imgview. Setontouchlistener (this); // set the touch screen listener dm = new displaymetrics (); getwindowmanager (). getdefadisplay display (). getmetrics (DM); // get the resolution minzoom (); Center (); imgview. setimagematrix (matrix);}/*** touch screen listener */Public Boolean ontouch (view V, motionevent event) {Switch (event. getaction () & motionevent. action_mask) {// The master node presses case motionevent. action_down: savedmatrix. set (matrix); Prev. set (event. getx (), event. gety () ); Mode = drag; break; // subpoint: press case motionevent. action_pointer_down: Dist = spacing (event); // if the distance between two consecutive points is greater than 10, it is determined that the multi-point mode 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 the maximum and minimum scaling ratio, automatically 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 () ;}/ *** min zoom ratio, Max.: 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);}/*** center horizontally and vertically */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 image is smaller than the screen size, it is displayed in the center. Greater than the screen. If it is left empty, it is moved up. If it is left empty, int screenheight = DM is moved down. 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);}/*** distance between two points */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 + 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 );}}

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