Android implements a method of controlling picture size scaling by gesture _android

Source: Internet
Author: User

This article describes the Android implementation of the method of controlling picture size by gestures. Share to everyone for your reference, specific as follows:

This program is achieved by hand gestures to scale the picture, from left to right when the picture is magnified, waving pictures from right to left when the picture is reduced, the faster the wave speed, the larger the scale. The program thought as follows: Define a ImageView in the interface to display the picture, use a gesturedetector to detect the user's gesture, and scale the picture according to the user's gesture at the horizontal speed.

Before introducing this example, let's introduce the general framework for dealing with gesture-touching events in Android.

First, add the statement implementation Ongesturelistener gesture Listener, the code is as follows:

Public Classgesturezoom extends activity implements Ongesturelistener

Defining a global instance of a gesture listener and initializing it in the OnCreate function, the code is as follows:

Gesturedetector detector;
@Override public
void OnCreate (Bundle savedinstancestate)
{
  ...
  detector = new Gesturedetector (this);
}

Third, rewrite the Ontouchevent function, the activity of the touch of the event to Gesturedetector processing, the code is as follows:

@Override Public
Boolean ontouchevent (motionevent me)
{return
  detector.ontouchevent (me);
}

To rewrite the function of the gesture you need to listen to, the default includes the following gestures:

Booleanondown (motionevent E): Press.
Booleanonfling (motionevent E1, motionevent E2, float Velocityx, float velocityy): Drag over, slide.
Abstract voidonlongpress (motionevent E): Long press.
Booleanonscroll (motionevent E1, motionevent E2, float Velocityx, float velocityy): scrolling.
Voidonshowpress (motionevent E): Pressed and not moved and loosened.
Booleanonsingletapup (motionevent e): Light hit.

These gestures are provided by default, according to the description you may still not quite clear these gestures, the best way is that you can implement a simple program experiment to understand. Of course, in addition to these default gestures, you can add their own gestures, space is no longer to repeat.

Next, we give an example of zooming by sliding, comparing the basic framework given above, in fact, we rewrite the onfling function, in which we define how to handle sliding events.

First define some global objects except gesture listeners, and do the corresponding initialization in the OnCreate function:

Gesturedetectordetector;
Imageviewimageview;
Bitmap bitmap;//Save Picture Resource
int width,height;//record picture's width, height
floatcurrentscale = 1;//record Current zoom ratio
Matrix matrix;// The matrix object that controls picture scaling
@Override public
voidoncreate (Bundle savedinstancestate)
{
    super.oncreate ( Savedinstancestate);
    Setcontentview (r.layout.main);
    detector = new Gesturedetector (this);
    ImageView = (ImageView) Findviewbyid (r.id.show);
    Matrix = new Matrix ();
    Bitmap =bitmapfactory.decoderesource (This.getresources (), < your picture resources >)//Get the scaled source picture, because the original picture cannot be modified, so must be converted to a bitmap
    width = bitmap.getwidth ();
    Height = bitmap.getheight ();
    Imageview.setimagebitmap (Bitmapfactory.decoderesource (This.getresources (), < your picture resource >)); Set the picture to display when ImageView is initialized
}

One, touch time binding gesture listener, and the front is the same, no longer paste code.

Second, rewrite the onfling function:

 @Override Publicboolean onfling (motionevent event1, motionevent event2, float Velocityx
   , float velocityy) {Velocityx = Velocityx > 4000? 4000:velocityx; Velocityx = Velocityx <-4000?
   -4000:velocityx;
   Calculates the scaling ratio according to the speed of the gesture, if the velocityx>0, enlarge the image, otherwise shrink the image.
   Currentscale + = Currentscale * velocityx/4000.0f;
   Guaranteed Currentscale will not equal 0 Currentscale = currentscale > 0.01? currentscale:0.01f;
   Reset matrix Matrix.reset ();
   Zoom Matrix Matrix.setscale (Currentscale,currentscale, 160, 200);
   bitmapdrawable tmp = (bitmapdrawable) imageview.getdrawable ();
   If the picture has not been reclaimed, force the recycle of the picture if (!tmp.getbitmap (). isrecycled ()) {Tmp.getbitmap (). Recycle ();
   ///Create new picture based on original bitmap and matrix Bitmap bitmap2 =bitmap.createbitmap (Bitmap, 0, 0, width, height, matrix, true);
   Displays the new bitmap Imageview.setimagebitmap (BITMAP2);
return true; }

The layout file simply adds a ImageView control, and you draw it yourself. There is no screenshot, because the screenshot also can not see the effect, we will try it on their own. OK, so this is achieved by gesture sliding to achieve image scaling, the above content learned from the Crazy Android book.

For more information on Android-related content readers can view the site topics: "Android graphics and image processing skills summary", "Android View Overview", "Android Development introduction and Advanced Course", " Android Debugging tips and FAQ Solutions Summary, Android Multimedia tips summary (audio, video, audio, etc.), "Android Basic Components Usage Summary", "Android Layout layout tips" and "Android Control usage Summary"

I hope this article will help you with the Android program.

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.