This example uses the example in "crazy android handout": paste it in person. The gesture from left to right is enlarged, and the gesture from right to left is reduced.
Package COM. example. demogesture; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. matrix; import android. graphics. drawable. bitmapdrawable; import android. OS. bundle; import android. view. gesturedetector; import android. view. gesturedetector. ongesturelistener; import android. view. motionevent; import android. widget. imageview; public class M Ainactivity extends activity implements ongesturelistener {// define gesture detector gesturedetector detector; imageview; Bitmap bitmap; // initial image resource int width, height; float currentscale = 1; // The image scaling ratio matrix; // The Matrix object controlling the image scaling @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); super. setcontentview (R. layout. activity_main); imageview = (imageview) findviewbyid (R. id. imag Eview1); detector = new gesturedetector (this); matrix = new matrix (); bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. AAA); width = bitmap. getwidth (); Height = bitmap. getheight (); // set the image during imageview initialization. This is required. If you change it to bitmap, an error is returned. setimagebitmap (bitmapfactory. decoderesource (getresources (), R. drawable. AAA);} @ overridepublic Boolean ontouchevent (motionevent event) {// todo auto-generated m Ethod stubreturn detector. ontouchevent (event);} public Boolean ondown (motionevent e) {// todo auto-generated method stubreturn false;} public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {// todo auto-generated method stubvelocityx = velocityx> 4000? 4000: velocityx; velocityx = velocityx <-4000? -4000: velocityx; // calculate the zoom ratio based on the gesture speed. If velocityx> 0, enlarge the image. Otherwise, the image currentscale + = currentscale * velocityx/4000.0f is reduced; // ensure that currentscale is not 0 currentscale = currentscale> 0.01f? Currentscale: 0.01f; // reset matrixmatrix. reset (); matrix. setscale (currentscale, currentscale, 160,200); bitmapdrawable TMP = (bitmapdrawable) imageview. getdrawable (); // if the image has not been recycled, forcibly recycle the image if (! TMP. getbitmap (). isrecycled () {TMP. getbitmap (). recycle ();} // create a new image bitmap bitmap2 = Bitmap Based on the original position and matrix. createbitmap (bitmap, 0, 0, width, height, matrix, true); imageview. setimagebitmap (bitmap2); Return true;} public void onlongpress (motionevent e) {// todo auto-generated method stub} public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stubreturn false;} public void onshowpress (motionevent e) {// todo auto-generated method stub} public Boolean onsingletapup (motionevent E) {// todo auto-generated method stubreturn false ;}}