Android handout 2: simple image browser with screenshot effect (for imageview)

Source: Internet
Author: User

Layout highlights:

// Distance from the bottom of the previous control

Android: layout_margintop = "10dp"

CodePhase Analysis:

// Return the printable view and convert it down to the printable view of Bitmap.

Bitmapdrawable = (bitmapdrawable) image1.getdrawable ();

// If the image has not been recycled, forcibly recycle the image first.

If (! Bitmapdrawable. getbitmap (). isrecycled ())
{
Bitmapdrawable. getbitmap (). Recycle ();
}

// Change the image displayed in the imageview
Image1.setimagebitmap (bitmapfactory. decoderesource (getresources (), images [++ currentimg]);

// Capture the original image for display

1. Use setontouchlistener to listen

2. Obtain the screen width for obtaining the actual proportion in step 3.

Displaymetrics dm = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
System. Out. println (DM. widthpixels );

3. ObtainThe actual bitmap image size and the scaling ratio of the first imageview

Double scale = bitmap. getwidth ()/DM. widthpixels;

4. Get the start point of the image to be displayed

Int x = (INT) (event. getx () * scale );
Int y = (INT) (event. Gety () * scale );

5.AlgorithmThe specified start point and display area cannot be greater than the image area

If (x + 120> bitmap. getwidth ())
{
X = bitmap. getwidth ()-120;
}
If (Y + 120> bitmap. getheight ())
{
Y = bitmap. getheight ()-120;
}

6. display the specified area of the image
Image2.setimagebitmap (bitmap. createbitmap (bitmap, X, Y, 120,120 ));

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.