Android Learning Note-imageview (image view)

Source: Internet
Author: User

Originally from: http://www.runoob.com/w3cnote/android-tutorial-imageview.html

Introduction to this section:

The UI underlying controls described in this section are: ImageView (Image View), known as a view or control that is used to display an image! Official Api:imageview; This section explains the following:

    1. The difference between the SRC attribute and the blackground of ImageView;
    2. Adjustviewbounds whether to set image scaling by aspect ratio
    3. ScaleType Setting the Zoom type
    4. The simplest drawing of a circular imageview
The difference between the 1.src property and the Background property:

In the API documentation we found that ImageView has two properties that can be set for the image: Src and background

Common sense:

①background usually refers to the background , and SRC refers to the content !!

② when you fill a picture with src , it is filled directly with the image size and does not stretch

When you fill a picture with background, it is stretched according to the width of the imageview given.

1) Write Code validation differences:

Write a simple layout test:

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context= "Com.jay.example.imageviewdemo.MainActivity" >        <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:background= "@drawable/pen" />        <ImageViewAndroid:layout_width= "200DP"Android:layout_height= "Wrap_content"Android:background= "@drawable/pen" />        <ImageViewAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"android:src= "@drawable/pen" />        <ImageViewAndroid:layout_width= "200DP"Android:layout_height= "Wrap_content"android:src= "@drawable/pen" />    </LinearLayout> 

As follows:

Results Analysis:

The width and height are wrap_content that is the same, is the original size, but when we fixed the width or height, the difference is obvious, blackground completely filled the whole imageview, and SRC is still so big, and he centered Oh, This involves another attribute of ImageView ScaleType! In addition, here we say only set width or height Oh! Join us at the same time set the width and height, blackground still fill, but the src size may change Oh! For example, let's test the following code:

< ImageView           Android:layout_width = "100DP"           android:layout_height= "50DP"          android:src= "@drawable/pen"/ >

Run:

Ps:scaletype below will speak ~

2) to solve the image deformation caused by blackground stretching method

In front of the second imageview we can see the picture has been stretched deformation, the square into a rectangle, for the same as I have a mild obsessive-compulsive disorder, it is obviously unacceptable, there is no way to set it? The answer must be some, the author temporarily know the following two ways:

    • This applies to the dynamic loading of ImageView, code also gradually, as long as the add view, the size of the dead can be

New Linearlayout.layoutparams (in);            
    • In addition to dynamically loading the view, more often, we will be through the way XML layout introduced ImageView solution is not difficult, that is, through the drawable bitmap resource file to complete, and then the Blackground property is set to the file! This XML file is created under the Drawable folder, this folder is created by yourself!!

Pen_bg.xml:

 <  bitmap  xmlns:android  =" Http://schemas.android.com/apk/res/android " Span style= "COLOR: #ff0000" > Android:id  = "@id/pen_bg"   android:gravity  =" Top "  Android: SRC  = "@drawable/pen"   Android:tilemode  = "disabled"  >  </ bitmap  >  

The above code is not difficult to understand, it is estimated that the most confusing is the Titlemode property bar, this property is tiled, is our windows set the background of the tile, a number of small icons covered the entire screen pinch! Remember it! I don't remember being able to try!disabled just to ban him!

This is the simple code above, as for the invocation method as follows:

Dynamic: Ibtnpen.setbacklgroundresource (R.DRAWABLE.PENBG);

Static: Android:background = "@drawable/penbg"

3) Setting the transparency issue

Say the first two differences, and then the next Setalpha attribute! This is very simple, this property, only src when it is effective!

4) The combination of the two magical:

A picture on the Web:

A look is a simple GridView, each item is a imageview, but careful you may find that the above icon is not a regular, but round, rounded rectangle, and so on, so here to use the SRC + background! To achieve this, you only need two actions: find a transparent PNG image + set a black background (you can also set the PNG transparency to achieve, but the results may be the same as expected!) Let's write a simple example:

, the cute little pig is shown to the ImageView, haha, blackground set a blue background!

Implementation code:

< ImageView       Android:layout_width = "150DP"       android:layout_height= "Wrap_content"      android:src= "@drawable/pig"        android:background= "#6699FF"/>

PS: Of course you can also use Selctor to achieve click Effect, set different circumstances to set different pictures, to achieve the click or touch effect!

5) Set the Blackground and SRC attributes in the Java code:

foreground (corresponding to SRC attribute):setimagedrawable();
Background (corresponds to Background property):setbackgrounddrawable();

2.adjustViewBounds set whether the zoom saves the original aspect ratio

ImageView provides us with the adjustviewbounds property for setting whether to maintain the original aspect ratio when zooming! Individual settings do not work and need to be used in conjunction with the maxWidth and maxheight Properties! The latter two properties also need to be adjustviewbounds to true to take effect ~

  • Android:maxheight: Setting the maximum height of the ImageView
  • Android:maxwidth: Sets the maximum width of the ImageView

code example:

<LinearLayoutxmlns: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"Tools:context=". Mainactivity ">    <!--the normal picture -    <ImageViewAndroid:id= "@+id/imageview1"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margin= "5px"android:src= "@mipmap/meinv" />    <!--limits the maximum width and height, and sets the adjustment boundary to maintain the aspect ratio of the displayed image -    <ImageViewAndroid:id= "@+id/imageview2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:layout_margin= "5px"Android:adjustviewbounds= "true"Android:maxheight= "200px"Android:maxwidth= "200px"android:src= "@mipmap/meinv" /></LinearLayout>

Run:

Results Analysis: The big picture is no processing of the picture, the size is: 541374; and the following one we limit imageview maximum width and height to 200px through maxwidth and MaxHeight, is to display a maximumof 200 pictures, we set a Adjustviewbounds = "true" to adjust our boundaries to maintain the aspect ratio of the picture, when the imageview width is 128*200~

3.scaleType Setting the Zoom type

Android:scaletype is used to set how the displayed picture is scaled or moved to fit the size of the ImageView Java code can be Imageview.setscaletype (ImageView.ScaleType.CENTER) To set the ~ optional values are as follows:

  • fitxy: Independent scaling of the image's landscape and portrait, making the picture fully adaptable to the ImageView, but the picture's aspect ratio may change
  • Fitstart: Maintain aspect ratio zoom picture, know long edge is equal to image programming, put picture in upper left corner of ImageView when Zoom is finished
  • fitcenter: ibid., after zooming in the middle;
  • fitend: ibid., after zooming in the lower right corner;
  • Center: Keep the original size, displayed in the center of the ImageView. When the size of the original image is larger than the size of ImageView, the partial cropping is exceeded.
  • centercrop: Keep the horizontal ratio zoom picture, know completely overwrite ImageView, may appear the picture is not displayed completely
  • centerinside: Keep the horizontal aspect ratio to scale the picture until ImageView can display the picture completely
  • Matrix: Default value, do not change the size of the original image, from the upper left corner of the ImageView to draw the original image, the original is more than the ImageView part for cropping

Next we compare a group of groups:

1) 1.fitend,fitstart,fitcenter

Here, for example Fitend, the other two are similar:

Example code:

 <!--  Keep the picture's aspect ratio scaled, knowing that the picture can be displayed on the ImageView component , and display the scaled picture in the lower right corner of the ImageView  -->  <  imageview  android:id  =" @+id/imageview3 "  android:layout_width  =" 300px "  android:layout_height  =" 300px "  Android:layout_margin  =" 5px "  Android:scaletype  =" Fitend "" Span style= "COLOR: #ff0000" > android:src  = "@mipmap/meinv"  />  

Run:

2) Centercrop and Centerinside
    • Centercrop: Scale by horizontal ratio, directly cover whole imageview completely
    • Centerinside: Scale by aspect ratio, so that imageview can display this picture completely

Example code:

<ImageViewAndroid:layout_width= "300px"Android:layout_height= "300px"Android:layout_margin= "5px"Android:scaletype= "Centercrop"android:src= "@mipmap/meinv" />    <ImageViewAndroid:layout_width= "300px"Android:layout_height= "300px"Android:layout_margin= "5px"Android:scaletype= "Centerinside"android:src= "@mipmap/meinv" />

Run:

3) Fitxy

Do not scale the picture, the goal is to fill the picture with the entire view

Example code:

< ImageView         Android:layout_width = "300px"         android:layout_height= "300px"        android:layout_margin= "5px"         Android:scaletype = "Fixxy"         android:src= "@mipmap/meinv"/>

Run:

Well, it's obviously flat, =-=~.

4) Matrix

Draw the original image from the upper left corner of the ImageView, with the original over the ImageView part for cropping

Example code:

< ImageView         Android:layout_width = "300px"         android:layout_height= "300px"        android:layout_margin= "5px"         Android:scaletype = "Matrix"         android:src= "@mipmap/meinv"/>

Run:

5) Center

Keep the size of the original, displayed in the center of the ImageView. When the size of the original image is larger than the size of ImageView, the partial cropping is exceeded.

Example code:

< ImageView         Android:layout_width = "300px"         android:layout_height= "300px"        android:layout_margin= "5px"         Android:scaletype = "Center"         android:src= "@mipmap/meinv"/>

Run:

4. The simplest drawing of a circular imageview

I believe that the round or round corner of the ImageView is not strange, now a lot of apps are like the round head is it ~

Here is a simple to write a round ImageView bar, of course, this is just an example, do not consider performance and anti-aliasing case!!!

Can be said to be written to play, the actual project can be considered on GitHub on the control of the cattle, such as the following two:

Git how to play the previous has taught everyone ~ the project clone down to copy the relevant files to their own projects can ~

Roundedimageview

Circleimageview

Android Learning Note-imageview (image view)

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.