Android Scalegesturedetector (zoom gesture detection)

Source: Internet
Author: User

I. Overview

The Scalegesturedetector class is designed to detect two finger gestures for zooming on the screen, and the simplest application is to scale the image or zoom the page.


Second, the requirements

Use the Scalegesturedetector class to achieve picture scaling.


Third, the realization

New Project Myscale, modify the Main.xml file, add a button and a surfaceview in it, as follows:

1<?XML version= "1.0" encoding= "Utf-8"?>2<LinearLayoutXmlns:android= "Http://schemas.android.com/apk/res/android"3Android:layout_width= "Fill_parent"4Android:layout_height= "Fill_parent"5Android:orientation= "Vertical">67<Button8Android:id= "@+id/button"9Android:layout_width= "Fill_parent"10Android:layout_height= "Wrap_content"11Android:text= "Show Picture"12/>1314 <surfaceview Span style= "color: #008080;" >15 android:id= "@+id/surfaceview" 16  android:layout_width = "fill_parent" 17 Span style= "color: #ff0000;" > Android:layout_height= "fill_parent" 18 />19 20 </linearlayout>      

Modify the Myscaleactivity.java file, mainly to implement the button listener and define a class implementation of the Scalegesturedetector.onscalegesturelistener interface:

1Package Com.nan.scale;23Import android.app.Activity;4Import Android.graphics.Bitmap;5Import Android.graphics.BitmapFactory;6Import Android.graphics.Canvas;7Import Android.graphics.Color;8Import Android.graphics.Matrix;9Import Android.graphics.Rect;10Import Android.os.Bundle;11Import android.view.MotionEvent;12Import Android.view.ScaleGestureDetector;13Import Android.view.SurfaceHolder;14Import Android.view.SurfaceView;15Import Android.view.View;16Import Android.widget.Button;17181920PublicClass MyscaleactivityExtends Activity21 {22Private Button Mbutton =Null23Private Surfaceview Msurfaceview =Null24Private Surfaceholder Msurfaceholder =Null25Private Scalegesturedetector Mscalegesturedetector =Null26Private Bitmap Mbitmap =Null2728/**Called when the activity is first created.*/@Override30Publicvoid OnCreate (Bundle savedinstancestate)31 {32Super.oncreate (savedinstancestate);Setcontentview (R.layout.main);34Msurfaceview = (Surfaceview)This.findviewbyid (R.id.surfaceview);Msurfaceholder = Msurfaceview.getholder ();PNS Mscalegesturedetector =New Scalegesturedetector (ThisNew Scalegesturelistener ());Mbutton = (Button)This.findviewbyid (R.id.button);39//Button monitoringMbutton.setonclicklistener (New View.onclicklistener ()41 {42@Override44Publicvoid OnClick (View v)45 {46//TODO auto-generated Method StubMbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.mmm);48//Lock the entire SurfaceviewMcanvas Canvas = Msurfaceholder.lockcanvas ();50//DrawingWuyi Mcanvas.drawbitmap (Mbitmap, 0f, 0f,NULL);52//Draw complete, Commit changesMsurfaceholder.unlockcanvasandpost (Mcanvas);54//Re-lock onceMsurfaceholder.lockcanvas (New Rect (0, 0, 0, 0));Msurfaceholder.unlockcanvasandpost (Mcanvas);57}58});5960}61@Override63PublicBoolean ontouchevent (Motionevent event)64 {65//Return to Scalegesturedetector to handle66Return Mscalegesturedetector.ontouchevent (event);67}686970PublicClass ScalegesturelistenerImplements Scalegesturedetector.onscalegesturelistener71 {72@Override74PublicBoolean Onscale (Scalegesturedetector detector)75 {76//TODO auto-generated Method Stub77Mmatrix Matrix =New Matrix ();79//Zoom ratio80Float scale = Detector.getscalefactor ()/3;Bayi Mmatrix.setscale (scale, scale);8283//Lock the entire SurfaceviewCanvas Mcanvas = Msurfaceholder.lockcanvas ();85//Clear ScreenMcanvas.drawcolor (Color.Black);87//Draw a scaled graphMcanvas.drawbitmap (Mbitmap, Mmatrix,NULL);89//Draw complete, Commit changes Msurfaceholder.unlockcanvasandpost (Mcanvas);  // re-lock new Rect (0, 0, 0, 0)); Span style= "color: #008080;" > Msurfaceholder.unlockcanvasandpost (Mcanvas);  94  return false;             

Run the program:

Click on the "Show Picture" button:

Then reduce the image with two fingers:

Enlarge Image:

All right. It can be found that there is a mutation every time the image is scaled, and if it is used for the application, the program needs to be improved.


    • Use Dimen to define dimensions in previous Android
    • Next Imageswitcher in Android (note the difference from the picture browser)

Android Scalegesturedetector (zoom gesture detection)

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.