ImageView of UI components and their subclasses (i) ImageView display pictures

Source: Internet
Author: User

The inheritance relationship of the ImageView family


ImageView inherits from the view component , its main function room displays the picture, can also display the Drawable object.

ImageView the direct subclass is Imagebutton,quickcontactbadge, the indirect subclass is Zoombutton, so all the XML properties of ImageView, methods, can basically be used for ImageButton, Zoombutton


The XML attributes of ImageView are as follows:


Android:adjustviewbounds: Whether you want to adjust your boundaries to keep the picture aspect ratio displayed. Need to be used with maxwidth, maxheight, otherwise no effect is used alone.

Android:cropToPadding:true, the component will be cropped to the padding that retains the ImageView

android:maxheight,android:maxwidth: Sets the maximum height and width of the view, is invalid for use alone, and needs to be used with setadjustviewbounds. If you want to set the image to a fixed size and want to maintain the picture aspect ratio, you need the following settings:
1) Set Setadjustviewbounds to true;
2) Set MaxWidth, MaxHeight;
3) Set settings Layout_width and Layout_height to Wrap_content.

Android:tint: Renders a picture as a specified color

ANDROID:SRC: sets the ID of the Drawable object displayed by ImageView

android:scaletype: Setscaletype (imageview.scaletype),Imageview.scaletype is an inner class, Set how the picture scales or moves to fit the size of the ImageView, with the usual values as follows:


Matrix (ImageView.ScaleType.MATRIC): Use the matric matrix to draw for zooming


Fitxy ( ImageView.ScaleType.FIT_XY ): Horizontal, vertical independent scaling of the picture , so that the picture is completely adapted to the ImageView, the picture aspect ratio may change

Fitstart ( ImageView.ScaleType.FIT_START ): Keep the horizontal aspect ratio to scale the picture until the longer edge is equal to the ImageView's edge, and then place it in the upper- left corner of the ImageView.

Fitend ( ImageView.ScaleType.FIT_END ): Keep the aspect ratio to scale the picture until the longer edge is equal to the imageview edge, and then place it in the lower-right corner of the ImageView when it is finished .

Fitcenter ( ImageView.ScaleType.FIT_CENTER ): Keep the horizontal aspect ratio to scale the picture until the longer edge is equal to the ImageView's edge, and then place it in the center of the ImageView .


Center(ImageView.ScaleType.CENTER): The picture is placed in the middle of the ImageView without any zooming

Centerinside(ImageView.ScaleType.CENTER_INSIDE): maintains a horizontal ratio to scale the picture so that ImageView can fully display the picture

Centercrop(ImageView.ScaleType.CENTER_CROP): Maintain the aspect ratio, so that the picture can completely cover the ImageView. As long as the shortest side of the picture can be displayed.


ImageView There are many ways to set up a picture resource , as follows:Bitmap,drawable,resource

Setimagebitmap (Bitmap BTM); Use the Bitmap bitmap to set the picture that the ImageView displays

Setimagedrawable (drawable drawable); Use the drawable object to set the ImageView display picture, where the drawable object is the actual resource obtained by the resource object, not the resource ID

Setimageresource (int reId); Use the picture resource ID to set the picture that the ImageView displays


example, picture browser, set image transparency.

<?xml version= "1.0" encoding= "Utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:id=" @+id/root "android:layout_width=" match_parent "android:layout_height=" Match_parent "Andro id:orientation= "vertical" > <linearlayout android:layout_width= "match_parent" android:layout_height= "Wrap_content" android:orientation= "Horizontal" > "<button android:id=" @+id/button1 "a Ndroid:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/button1"/ > <button android:id= "@+id/button2" android:layout_width= "Wrap_content" Android:layo          ut_height= "Wrap_content" android:text= "@string/button2"/> <button android:id= "@+id/button3" Android:layout_width= "Match_parent" android:layout_height= "wrap_content" android:text= "@string/ Next "/> </linearlayout><!--Define a ImageView display picture--<imageview android:id= "@+id/imageview1" android:layout_width= Parent "android:layout_height=" 280DP "android:scaletype=" Fitcenter "android:src=" @drawable/shuangta " /> <imageview android:id= "@+id/imageview2" android:layout_width= "120DP" Android:layout_heigh t= "120DP" android:src= "@drawable/ic_launcher"/></linearlayout>

Package Com.hust.imageviewtest;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;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;public class Mainactivity extends Activity {//define an array of access images int[] images = new int[]{ r.drawable.lijiang,r.drawable.qiao,r.drawable.shuangta,r.drawable.shui,r.drawable.xiangbi,};//defines a picture int that is displayed by default    Currentimg = 2;//Defines the initial transparency of the picture private int alpha = 255;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main); Final button plus = (button) Findviewbyid (r.id.button1), final button minus = (button) Findviewbyid (R.id.button2); final Bu Tton next = (Button) Findviewbyid (R.id.button3); FinaL ImageView image1 = (ImageView) Findviewbyid (r.id.imageview1); final ImageView image2 = (ImageView) Findviewbyid ( R.ID.IMAGEVIEW2);//define the listener to view the next picture Next.setonclicklistener (new Onclicklistener () {@Overridepublic void OnClick (View v) {//Control ImageView display next picture image1.setimageresource (images[++currentimg% images.length]);}); /define a method to change the transparency of a picture 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 transparency of the image Image1.setalpha (alpha);}};/ /Add listener Plus.setonclicklistener (listener) for two buttons, Minus.setonclicklistener (listener); Image1.setontouchlistener (new Ontouchlistener () {@Overridepublic Boolean OnTouch (view view, Motionevent event) {bitmapdrawable bitmapdrawable = ( bitmapdrawable) image1.getdrawable ();//Gets the bitmap in the first Picture display box bitmap bitmap = Bitmapdrawable.getbitmap ();// Bitmap picture actual size with the first imageview scaling double scale = bitmap.getwidth ()/320.0;//Gets the start point of the picture that needs to be displayed int x = (int) (EVENT.GETX () * scale), int y = (int) (event.gety () * scale), if (x + > Bitmap.getwidth ()) {x = Bitmap.getwidth ()-1 20;} if (Y + > Bitmap.getheight ()) {y = Bitmap.getheight ()-120;}    Displays the specified area of the picture Image2.setimagebitmap (Bitmap.createbitmap (Bitmap, x, y, +)), Image2.setalpha (Alpha); return false;}); } @Override Public boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu, this adds items to the Act        Ion 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 would//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); }}















ImageView of UI components and their subclasses (i) ImageView display pictures

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.