Android Study Notes 15: image browser

Source: Internet
Author: User
Tags gety

This image browser can implement a predefined Image array in the browser program, and dynamically change the transparency of the image and view the partial details of the image. The imageview control is used, use setimagebitmap to change the image transparency and use setalphe to change the image transparency.

<? 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"> <linearlayoutandroid: orientation = "horizontal" Android: layout_width = "fill_parent" Android: layout_height = "wrap_content" Android: gravity = "center"> <button Android: id = "@ + ID/plus" Android: layout_width = "wrap_content" Android: layout_height = "Wrap_content" Android: text = "Increase transparency"/> <button Android: Id = "@ + ID/minus" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = ""/> <button Android: Id = "@ + ID/next" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Next"/> </linearlayout> <! -- Define the overall imageview of the displayed image --> <imageview Android: Id = "@ + ID/image1" Android: layout_width = "fill_parent" Android: layout_height = "240px" Android: src = "@ drawable/sunyz_1" Android: scaletype = "centercrop"/> <! -- Scale up the image size in the center so that the image length (width) is equal to or greater than the view length (width) --> <! -- Define imageview for displaying partial image details --> <imageview Android: Id = "@ + ID/image2" Android: layout_width = "120dp" Android: layout_height = "120dp" Android: background = "# 0000ff" Android: layout_margintop = "10dp"/> </linearlayout>

Package Org. crazyit. imageview; import android. app. activity; import android. graphics. bitmap; import android. graphics. bitmapfactory; import android. graphics. drawable. bitmapdrawable; import android. OS. bundle; import android. view. motionevent; import android. view. view; import android. view. view. onclicklistener; import android. view. view. ontouchlistener; import android. widget. button; import android. widget. imageview; Public class imageviewtest extends activity {// defines an array int [] images = new int [] {R. drawable. sunyz_1, R. drawable. sunyz_2, R. drawable. sunyz_3, R. drawable. sunyz_4, R. drawable. sunyz_5 ,}; // defines the default image int currentimg = 2; // defines the initial transparency of the image private int alpha = 255; @ overridepublic void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Final button plus = (button) Fin Dviewbyid (R. id. plus); Final button minus = (button) findviewbyid (R. id. minus); Final imageview image1 = (imageview) findviewbyid (R. id. image1); Final imageview image2 = (imageview) findviewbyid (R. id. image2); Final button next = (button) findviewbyid (R. id. next); // define the listener next for viewing the next image. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view v) {If (currentimg> = 4) {currentimg =-1;} bitmapdrawabl E 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 image1.setimagebitmap (bitmapfactory. decoderesource (getresources (), images [++ currentimg]) ;}}); // defines the method for changing the image transparency. onclicklistener listener = new onclicklistener () {@ overridepublic void onclick (view v) {If (V = plus) {Alpha + = 20;} If (V = minus) {alpha-= 20 ;} if (alpha >=255) {alpha = 255;} If (alpha <= 0) {alpha = 0 ;}// change the image transparency image1.setalpha (alpha );}}; // Add the listener plus for the two buttons. setonclicklistener (listener); minus. setonclicklistener (listener); listener (New ontouchlistener () {@ overridepublic Boolean ontouch (view, motionevent event) {bitmapdrawable = (bitmapdrawable) image1.getdrawable (); // obtain the Bitmap bitmap = bitmapdrawable In the first image display box. getbitmap (); // double scale = bitmap. getwidth ()/280.0; // get the start point int x = (INT) (event. getx () * scale); int y = (INT) (event. gety () * scale); // int x = (INT) (event. getx (); // int y = (INT) (event. gety (); If (x + 120> bitmap. getwidth () {x = bitmap. getwidth ()-120;} If (Y + 120> bitmap. getheight () {Y = bitmap. getheight ()-120;} // display the image in the specified image2.setimagebitmap (bitmap. createbitmap (bitmap, X, Y, 120,120); image2.setalpha (alpha); Return false ;}});}}

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.