Android's Scalegesturedetector Zoom class

Source: Internet
Author: User

The articles are composed of multiple-out combinations, which come from:

http://elvajxw.iteye.com/blog/1308452

Http://www.cnblogs.com/lknlfy/archive/2012/03/11/2390274.html

class overview

Detects deformation gestures caused by multiple contacts (Multitouch), based on the received motionevent. callback method
Scalegesturedetector.onscalegesturelistener notifies the user when a particular gesture event occurs. This class can only be used in conjunction with the motionevent triggered by the touch event.   Use this class to create a scalegesturedetector instance of your View to ensure that ontouchevent (motionevent) is called in the Ontouchevent (motionevent) method. When an event occurs, the method defined in callback is called.

(Scalegesturedetector is a new class for Android2.2, allowing views to detect and process gesture change information, including multi-touch, through the provided motionevents.) )


Inner class

Interface Scalegesturedetector.onscalegesturelistener
Listeners that receive notifications when a gesture occurs

Class Scalegesturedetector.simpleonscalegesturelistener
A convenient class to use. If you want to listen to only a subset of scaling events, you can inherit the class.

Public Construction Methods

Public Scalegesturedetector (context context, Scalegesturedetector.onscalegesturelistener listener)

constructor function
Public methods

Public float Getcurrentspan ()

Returns the current distance of the two contacts that make up the gesture during a gesture.
return value
The distance, in pixels, of the contact point.

Public Long Geteventtime ()

Returns the time when the event was captured.
return value
The event time in milliseconds.

Public float Getfocusx ()

Returns the X-coordinate of the current gesture focus. If the gesture is in progress, the focus is between the two contacts that make up the gesture. If the gesture is ending, focus is on the position of the contact that remains on the screen. If Isinprogress () returns FALSE, the return value of the method is undefined.
return value
Returns the X-coordinate value of the focus, in pixels.

Public float Getfocusy ()

Returns the Y-coordinate of the current gesture focus. If the gesture is in progress, the focus is between the two contacts that make up the gesture. If the gesture is ending, focus is on the position of the contact that remains on the screen. If Isinprogress () returns FALSE, the return value of the method is undefined.
return value
Returns the Y-coordinate value of the focus, in pixels.

Public float Getpreviousspan ()

Returns the previous distance of the two contacts that make up the gesture during a gesture.
return value
The previous distance of two points, in pixels.

Public float Getscalefactor ()

Returns the scaling ratio from the previous scaling event to the current scaling event. The value is defined as (Getcurrentspan ()/Getpreviousspan ()).
return value
The current scaling ratio.

Public Long Gettimedelta ()

Returns the time difference, in milliseconds, from the scaling event received before the current scaling event.
return value
The time difference, in milliseconds, from the start of the previous scaling event.

Public Boolean isinprogress ()

Returns true if the gesture is in progress.
return value
Returns true if the gesture is in progress. Otherwise, false is returned.

Simple example:

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

<?xml version= "1.0" encoding= "Utf-8"?>
<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:orientation= "Vertical" >

<button
Android:id= "@+id/button"
Android:layout_width= "Fill_parent"
android:layout_height= "Wrap_content"
android:text= "Show Pictures"
/>

<surfaceview
Android:id= "@+id/surfaceview"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
/>

</LinearLayout>

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

Package Com.nan.scale;

Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.BitmapFactory;
Import Android.graphics.Canvas;
Import Android.graphics.Color;
Import Android.graphics.Matrix;
Import Android.graphics.Rect;
Import Android.os.Bundle;
Import android.view.MotionEvent;
Import Android.view.ScaleGestureDetector;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceView;
Import Android.view.View;
Import Android.widget.Button;



public class Myscaleactivity extends Activity
{
Private Button Mbutton = null;
Private Surfaceview Msurfaceview = null;
Private Surfaceholder msurfaceholder = null;
Private Scalegesturedetector mscalegesturedetector = null;
Private Bitmap mbitmap = null;

/** called when the activity is first created. */
@Override
public void OnCreate (Bundle savedinstancestate)
{
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.main);

Msurfaceview = (Surfaceview) This.findviewbyid (R.id.surfaceview);
Msurfaceholder = Msurfaceview.getholder ();
Mscalegesturedetector = new Scalegesturedetector (this,new Scalegesturelistener ());
Mbutton = (Button) This.findviewbyid (R.id.button);
Button monitoring
Mbutton.setonclicklistener (New View.onclicklistener ()
{

@Override
public void OnClick (View v)
{
TODO auto-generated Method Stub
Mbitmap = Bitmapfactory.decoderesource (Getresources (), r.drawable.mmm);
Lock the entire Surfaceview
Canvas Mcanvas = Msurfaceholder.lockcanvas ();
Drawing
Mcanvas.drawbitmap (Mbitmap, 0f, 0f, NULL);
Draw complete, Commit changes
Msurfaceholder.unlockcanvasandpost (Mcanvas);
Re-lock once
Msurfaceholder.lockcanvas (New Rect (0, 0, 0, 0));
Msurfaceholder.unlockcanvasandpost (Mcanvas);
}
});

}

@Override
public boolean ontouchevent (Motionevent event)
{
Return to Scalegesturedetector to handle
Return Mscalegesturedetector.ontouchevent (event);
}


public class Scalegesturelistener implements Scalegesturedetector.onscalegesturelistener
{

@Override
public boolean Onscale (Scalegesturedetector detector)
{
TODO auto-generated Method Stub

Matrix Mmatrix = new Matrix ();
Zoom ratio
Float scale = Detector.getscalefactor ()/3;
Mmatrix.setscale (scale, scale);

Lock the entire Surfaceview
Canvas Mcanvas = Msurfaceholder.lockcanvas ();
Clear Screen
Mcanvas.drawcolor (Color.Black);
Draw a scaled graph
Mcanvas.drawbitmap (Mbitmap, Mmatrix, NULL);
Draw complete, Commit changes
Msurfaceholder.unlockcanvasandpost (Mcanvas);
Re-lock once
Msurfaceholder.lockcanvas (New Rect (0, 0, 0, 0));
Msurfaceholder.unlockcanvasandpost (Mcanvas);

return false;
}

@Override
public boolean Onscalebegin (Scalegesturedetector detector)
{
TODO auto-generated Method Stub
Be sure to return true before entering Onscale () this function
return true;
}

@Override
public void Onscaleend (Scalegesturedetector detector)
{
TODO auto-generated Method Stub

}

}

}

Android's Scalegesturedetector Zoom class

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.