Android Application Development BASICS (16) -- scalegesturedetector (scaling gesture detection)

Source: Internet
Author: User

I. Overview

Scalegesturedetector is used to detect the scaling gesture of two fingers on the screen. The simplest application is to zoom in or out the image or webpage.


Ii. Requirements

Use scalegesturedetector to scale images.


Iii. Implementation

Create a project myscale, modify the main. xml file, and add a button and a surfaceview in it, as shown below:

1 <? XML version = "1.0" encoding = "UTF-8"?>
2 <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
3 Android: layout_width = "fill_parent"
4 Android: layout_height = "fill_parent"
5 Android: Orientation = "vertical">
6
7 <button
8 Android: Id = "@ + ID/button"
9 Android: layout_width = "fill_parent"
10 Android: layout_height = "wrap_content"
11 Android: text = "show pictures"
12/>
13
14 <surfaceview
15 Android: Id = "@ + ID/surfaceview"
16 Android: layout_width = "fill_parent"
17 Android: layout_height = "fill_parent"
18/>
19
20 </linearlayout>

Modify the myscaleactivity. Java file to implement button listening and defining a class to implement the scalegesturedetector. onscalegesturelistener interface:

 

1 package com. Nan. scale;
2
3 Import Android. App. activity;
4 Import Android. Graphics. Bitmap;
5 import Android. Graphics. bitmapfactory;
6 Import Android. Graphics. Canvas;
7 Import Android. Graphics. color;
8 Import Android. Graphics. matrix;
9 Import Android. Graphics. rect;
10 Import Android. OS. Bundle;
11 import Android. View. motionevent;
12 Import Android. View. scalegesturedetector;
13 Import Android. View. surfaceholder;
14 Import Android. View. surfaceview;
15 Import Android. View. view;
16 Import Android. widget. Button;
17
18
19
20 public class myscaleactivity extends Activity
21 {
22 private button mbutton = NULL;
23 private surfaceview msurfaceview = NULL;
24 private surfaceholder msurfaceholder = NULL;
25 private scalegesturedetector mscalegesturedetector = NULL;
26 private bitmap mbitmap = NULL;
27
28/** called when the activity is first created .*/
29 @ override
30 public void oncreate (bundle savedinstancestate)
31 {
32 super. oncreate (savedinstancestate );
33 setcontentview (R. layout. Main );
34
35 msurfaceview = (surfaceview) This. findviewbyid (R. Id. surfaceview );
36 msurfaceholder = msurfaceview. getholder ();
37 mscalegesturedetector = new scalegesturedetector (this, new scalegesturelistener ());
38 mbutton = (button) This. findviewbyid (R. Id. Button );
39 // button listener
40 mbutton. setonclicklistener (New View. onclicklistener ()
41 {
42
43 @ override
44 public void onclick (view V)
45 {
46 // todo auto-generated method stub
47 mbitmap = bitmapfactory. decoderesource (getresources (), R. drawable. Mmm );
48 // lock the whole surfaceview
49 canvas mcanvas = msurfaceholder. lockcanvas ();
50 // drawing
51 mcanvas. drawbitmap (mbitmap, 0f, 0f, null );
52 // The painting is complete. Submit the modification.
53 msurfaceholder. unlockcanvasandpost (mcanvas );
54 // lock again
55 msurfaceholder. lockcanvas (New rect (0, 0, 0, 0 ));
56 msurfaceholder. unlockcanvasandpost (mcanvas );
57}
58 });
59
60}
61
62 @ override
63 public Boolean ontouchevent (motionevent event)
64 {
65 // return to scalegesturedetector for processing
66 return mscalegesturedetector. ontouchevent (event );
67}
68
69
70 public class scalegesturelistener implements scalegesturedetector. onscalegesturelistener
71 {
72
73 @ override
74 public Boolean onscale (scalegesturedetector detector)
75 {
76 // todo auto-generated method stub
77
78 matrix mmatrix = new matrix ();
79 // scaling ratio
80 float scale = detector. getscalefactor ()/3;
81 mmatrix. setscale (scale, scale );
82
83 // lock the whole surfaceview
84 canvas mcanvas = msurfaceholder. lockcanvas ();
85 // clear screen
86 mcanvas. drawcolor (color. Black );
87 // draw a scaled Image
88 mcanvas. drawbitmap (mbitmap, mmatrix, null );
89 // The painting is complete. Submit the modification.
90 msurfaceholder. unlockcanvasandpost (mcanvas );
91 // lock again
92 msurfaceholder. lockcanvas (New rect (0, 0, 0, 0 ));
93 msurfaceholder. unlockcanvasandpost (mcanvas );
94
95 return false;
96}
97
98 @ override
99 Public Boolean onscalebegin (scalegesturedetector detector)
100 {
101 // todo auto-generated method stub
102 // you must return true to enter the onscale () function.
103 return true;
104}
105
106 @ override
107 Public void onscaleend (scalegesturedetector detector)
108 {
109 // todo auto-generated method stub
110
111}
112
113}
114
115}

Run the program:

 

Click "show image:

 

Then zoom out the image with two fingers:

 

Enlarge the image:

 

Okay. It can be found that there is a mutation each time the image is scaled at the beginning, and this program needs to be improved if it is used for applications.


PS: images from the Internet


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.