Double-click android to enlarge the image, and then double-click to zoom out the image. [Android evolution 9]

Source: Internet
Author: User

Today, I wrote a method to handle the ondoubletap event. Double-click the program to zoom in and out the screen event, double-click the screen image to zoom in, and then double-click the screen image to zoom in to the original effect. See the results!

Source image: double-click the screen to enlarge the effect:

Paste the code below:

I. Code in mainactivty. Java class:

Package com.cn. android; import android. app. activity; import android. OS. bundle; import android. util. log; import android. view. gesturedetector; import android. view. keyevent; import android. view. motionevent; import android. view. gesturedetector. ongesturelistener; public class mainactivity extends activity implements ongesturelistener {private imageshowview listener = NULL; private synchronized gestureener; private double scale = 1.0; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); mimageshowview = new imageshowview (this); setcontentview (mimageshowview); gesturetor = new gesturedetector (this); gesturetor. setondoubletaplistener (New gesturedetector. ondoubletaplistener () {@ override public Boolean ondoubletap (motionevent e) {// todo auto-generated method stub // If (scale = 1.0) {scale = scale + 0.5; mimageshowview. setscale (scale);} else if (scale = 1.5) {scale = scale-0.5; mimageshowview. setscale (scale);} log. V ("Daming", "ondoubletap"); Return true ;}@ override public Boolean ondoubletapevent (motionevent E) {// todo auto-generated method stub // double-click to generate two logs. V ("Daming", "ondoubletapevent"); Return false ;}@ override public Boolean onsingletapconfirmed (motionevent e) {// Click Log once. V ("Daming", "onsingletapconfirmed"); Return false ;}}) ;}@ override public Boolean ontouchevent (motionevent me) {return gesturetries. ontouchevent (me) ;}@ override public Boolean ondown (motionevent e) {// todo auto-generated method stub return false;} @ override public Boolean onfling (motionevent E1, motionevent E2, float velocityx, float velocityy) {// todo auto-generated method stub return false;} @ override public void onlongpress (motionevent E) {// todo auto-generated method stub} @ override public Boolean onscroll (motionevent E1, motionevent E2, float distancex, float distancey) {// todo auto-generated method stub return false;} @ override public void onshowpress (motionevent E) {// todo auto-generated method stub} @ override public Boolean onsingletapup (motionevent e) {// todo auto-generated method stub return false ;}}

Ii. Code in imageshowview. Java:

Package com.cn. android; import android. content. context; import android. graphics. bitmap; import android. graphics. canvas; import android. graphics. matrix; import android. graphics. drawable. bitmapdrawable; import android. view. keyevent; import android. view. view; public class imageshowview extends view implements runnable {bitmap mbitmap = NULL; int mbitmapwidth = 0; // define the image width int mbitmapheight = 0; // define the image's high private Double scale = 1.0; matrix mmatrix = new matrix (); Public imageshowview (context) {super (context); mbitmap = (bitmapdrawable) getresources (). getdrawable (R. drawable. image1 )). getbitmap (); mbitmapwidth = mbitmap. getwidth (); mbitmapheight = mbitmap. getheight (); // enable the thread new thread (this ). start ();} public void setscale (double scale) {This. scale = scale ;}@ override protected void ondraw (canvas C Anvas) {super. ondraw (canvas); mmatrix. reset (); mmatrix. postscale (float) scale, (float) scale); // you can specify bitmap mbitmap2 = bitmap. createbitmap (mbitmap, 0, 0, mbitmapwidth, mbitmapheight, mmatrix, true); // draw the image imageshowview after rotation. drawimage (canvas, mbitmap2, (320-mbitmapwidth)/); mbitmap2 = NULL ;}@ override public void run () {While (! Thread. currentthread (). isinterrupted () {try {thread. sleep (100);} catch (interruptedexception e) {// todo auto-generated Catch Block E. printstacktrace () ;}postinvalidate () ;}} Private Static void drawimage (canvas, Bitmap bitmap, int X, int y) {// todo auto-generated method stub canvas. drawbitmap (bitmap, X, Y, null );}}

Note: The above is all my code, and the images in the project will not be uploaded. You can upload an image by yourself. If you need the complete code, you can leave the email address, if you have any questions, please leave a message and I will reply to them! If you have good suggestions, you can come up with them, learn together, and make progress together!


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.