Android UI-zoomcontrols to zoom in and out images

Source: Internet
Author: User

The zoomcontrols control is a zoomcontrols control that can be scaled but has the following effect:

The following are some of the main methods

Hasfocus (): determines the focus

Hide (): Hide

Ontouchevent (motionevent event): Now this method is used to handle touch screen mobile events.

Setiszoominenabled (Boolean isenabled): whether to allow Amplification

Setiszoomoutenabled (Boolean isenabled): whether to allow downgrading

Setonzoominclicklistener (view. onclicklistener listener): registers a listener for amplification.

Setonzoomoutclicklistener (view. onclicklistener listener): registers the listener for downgrading.

Setzoomspeed (long speed): sets the scaling speed.

Show (): Display

In this case, if you set the setiszoominenabled () method to false, the enlarged button turns gray and cannot be used. In fact, this control is only two buttons, but it only has an appearance, no function. If you want to zoom in or zoom out an image, you still need to implement it in the listener event.

Start viewing code

Main. xml file:

<? XML version = "1.0" encoding = "UTF-8"?>
<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
Android: Orientation = "vertical"
Android: layout_width = "fill_parent"
Android: layout_height = "fill_parent"
Android: Id = "@ + ID/layout1"
>
<Imageview
Android: Id = "@ + ID/imgview"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: src = "@ drawable/Yuanyuan"
/>

<Zoomcontrols
Android: Id = "@ + ID/zoomcontrol"
Android: layout_gravity = "bottom"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
/>
</Linearlayout>

 

Zoomexampleactivity. Java file:

Package com. loulijun. zoomcontroltest;

Import Android. App. activity;
Import Android. Graphics. Bitmap;
Import Android. Graphics. bitmapfactory;
Import Android. Graphics. matrix;
Import Android. OS. Bundle;
Import Android. util. displaymetrics;
Import Android. View. view;
Import Android. View. View. onclicklistener;
Import Android. widget. imageview;
Import Android. widget. linearlayout;
Import Android. widget. zoomcontrols;

Public class zoomexampleactivity extends activity {
Private linearlayout layout1;
Private zoomcontrols zoom;
Private imageview IMG;
Private int id = 0;
Private int displaywidth;
Private int displayheight;
Private float scalewidth = 1;
Private float scaleheight = 1;
Private bitmap BMP;
@ Override
Public void oncreate (bundle savedinstancestate ){
Super. oncreate (savedinstancestate );
Setcontentview (R. layout. Main );

Layout1 = (linearlayout) findviewbyid (R. Id. layout1 );
// Obtain the screen resolution size
Displaymetrics dm = new displaymetrics ();
Getwindowmanager (). getdefaultdisplay (). getmetrics (DM );
Displaywidth = DM. widthpixels;
// Screen height minus zoomcontrols height
Displayheight = DM. heightpixels;
BMP = bitmapfactory. decoderesource (getresources (), R. drawable. Yuanyuan );
IMG = (imageview) findviewbyid (R. Id. imgview );
// Zoom. Hide (); hide zoomcontrols
// Zoom. Show (); display zoomcontrols

Zoom = (zoomcontrols) findviewbyid (R. Id. zoomcontrol );
IMG = (imageview) findviewbyid (R. Id. imgview );
Zoom. setiszoominenabled (true );
Zoom. setiszoomoutenabled (true );
// Enlarge the image
Zoom. setonzoominclicklistener (New onclicklistener ()
{
Public void onclick (view V)
{
Int BMP width = BMP. getwidth ();
Int bmpheight = BMP. getheight ();
// Set the zoomed-in ratio of the image
Double scale = 1.25;
// Calculate the scale to be enlarged this time
Scalewidth = (float) (scalewidth * scale );
Scaleheight = (float) (scaleheight * scale );
// Generate new size but bitmap object
Matrix matrix = new matrix ();
Matrix. postscale (scalewidth, scaleheight );
Bitmap resizebmp = bitmap. createbitmap (BMP, 0, 0, BMP width, bmpheight, matrix, true );
IMG. setimagebitmap (resizebmp );

}
});
// Reduce the image size
Zoom. setonzoomoutclicklistener (New onclicklistener ()
{

Public void onclick (view v ){
Int BMP width = BMP. getwidth ();
Int bmpheight = BMP. getheight ();
// Set the zoomed-in ratio of the image
Double scale = 0.8;
// Calculate the scale to be enlarged this time
Scalewidth = (float) (scalewidth * scale );
Scaleheight = (float) (scaleheight * scale );
// Generate new size but bitmap object
Matrix matrix = new matrix ();
Matrix. postscale (scalewidth, scaleheight );
Bitmap resizebmp = bitmap. createbitmap (BMP, 0, 0, BMP width, bmpheight, matrix, true );
IMG. setimagebitmap (resizebmp );
}

});
}
}

 

The effect is as follows:

 

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.