Ui review-component: imageview (image scaling)

Source: Internet
Author: User
Package COM. brady. est; import android. annotation. suppresslint; import android. graphics. bitmap; import android. graphics. drawable. bitmapdrawable; import android. OS. bundle; import android. support. v7.app. actionbaractivity; import android. view. menu; import android. view. menuitem; 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; import android. widget. textview; @ suppresslint ({"handlerleak", "clickableviewaccessibility"}) public class mainactivity extends actionbaractivity {private int [] images = new int [] {R. drawable. a, R. drawable. b, R. drawable. c, R. drawable. d}; int currentimage = 0; @ override protected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. activity_main); Final button = (button) findviewbyid (R. id. button); Final imageview imageview1 = (imageview) findviewbyid (R. id. image1); Final imageview imageview2 = (imageview) findviewbyid (R. id. image2); Final textview detail = (textview) findviewbyid (R. id. detail); button. setonclicklistener (New onclicklistener () {@ override public void onclick (view v) {imageview1.setimageresource (images [currentimage % images. length]); currentimage ++ ;}}); imageview1.setontouchlistener (New ontouchlistener () {@ override public Boolean ontouch (view V, motionevent event) {bitmapdrawable = (bitmapdrawable) imageview1.getdrawable (); Bitmap bitmap = bitmapdrawable. getbitmap (); // scaling ratio double scale = bitmap. getwidth ()/imageview1.getwidth (); int x = (INT) (event. getx () * scale); int y = (INT) (event. gety () * scale); If (x + 150> bitmap. getwidth () {x = bitmap. getwidth ()-150;} If (Y + 100> bitmap. getheight () {Y = bitmap. getheight ()-150;} detail. settext (scale + "\ n" + "X:" + x + "\ n" + bitmap. getwidth () + "\ n" + "Y:" + Y + "\ n" + bitmap. getheight (); imageview2.setimagebitmap (bitmap. createbitmap (bitmap, X, Y, 100,100); Return false ;}}) ;}@ override public Boolean oncreateoptionsmenu (menu) {// inflate the menu; this adds items to the action bar if it is present. getmenuinflater (). inflate (R. menu. main, menu); Return true ;}@ override public Boolean onoptionsitemselected (menuitem item) {// handle action bar item clicks here. the action bar will // automatically handle clicks on the home/Up button, so long // As you specify a parent activity in androidmanifest. XML. int id = item. getitemid (); If (ID = R. id. action_settings) {return true;} return Super. onoptionsitemselected (item );}}

Layout XML files

<Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: Orientation = "vertical" Android: id = "@ + ID/root" Android: padding = "5dip" tools: context = "com. brady. EST. mainactivity "> <imageview Android: Id =" @ + ID/image1 "Android: layout_width =" 200px "Android: layout_height =" wrap_content "Android: src = "@ drawable/a"/> <button Android: Id = "@ + ID/button" Android: text = "Next" Android: layout_width = "match_parent" Android: layout_height = "wrap_content"/> <imageview Android: Id = "@ + ID/image2" Android: layout_width = "120px" Android: layout_height = "wrap_content"/> <textview Android: id = "@ + ID/detail" Android: layout_width = "match_parent" Android: layout_height = "wrap_content"/> </linearlayout>

Interface:

Ui review-component: imageview (image scaling)

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.