Android small project-scaling images with gestures

Source: Internet
Author: User

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 ;}}

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.