The difference between the SRC and background of "Android" ImageView and the magical magic of both

Source: Internet
Author: User
Tags xml attribute

One, the difference between XML attribute src and background in ImageView:

The background is stretched according to the length and width given by the ImageView component, while SRC stores the size of the original and does not stretch. SRC is the picture content (foreground), BG is the background and can be used simultaneously.

In addition: ScaleType only works on SRC; BG can set transparency, such as the ability to use Android:scaletype to control how images are scaled in ImageButton

Second, solve the android:background background picture is stretched problem:

As described above, the picture set by background is stretched with the length-to-width ratio given by the view component. For example, the 36x36 px icon is placed in the xhdpi folder, in 854x480 (FWVGA, corresponding hdpi) environment, according to

Xhdpi:hdpi:mdpi:ldip = 2:1.5:1: 0.75

FWVGA, the actual size of the icon should be 27x27.

But when I put it in a layout_width = 96px, layout_height = 75px linearlayout, the layout code is as follows:

[HTML] view plain copy

    1. < LinearLayout android:gravity = "center" android:layout_width = "96px" android:layout_height = "75px" >
    2. < ImageButton android:layout_width = "Wrap_content" android:layout_height= "wrap_content" android:background = "@ Drawable/toolbar_bg "/>
    3. </linearlayout >

The reality is that the size of the ImageButton we get is 33x27, and obviously the width is stretched, which is what we don't want to see.

Solution One:

The ImageButton layout_width and Layout_width are set dynamically in the code, as follows

[Java] view plain copy

    1. Linearlayout.layoutparams Layoutparam = new Linearlayout.layoutparams (27, 27);
    2. Layout.addview (ImageButton, Layoutparam);

However, in fact we do not want the code to have a "hard-coded" situation.

Solution Two:

When you set the Android:background property through Setbackgroundresource () or in XML, your background is defined as an XML bitmap, as follows:

[HTML] view plain copy

    1. <? XML version = "1.0" encoding = "Utf-8"?>
    2. < bitmap xmlns:android = "Http://schemas.android.com/apk/res/android"
    3. Android:id = "@id/toolbar_bg_bmp"
    4. ANDROID:SRC = "@drawable/toolbar_bg"
    5. Android:tilemode = "Disabled" android:gravity = "Top" >
    6. </Bitmap >

The call is as follows:

Imagebutton.setbackgroundresource (r.drawable.toolbar_bg_bmp)

Or

<imagebutton android:background= "@drawable/toolbar_bg_bmp" .../>

If the background image has a variety of states, you can also refer to Toolbar_bg_selector.xml:

[HTML] view plain copy

  1. <? XML version = "1.0" encoding = "Utf-8"?>
  2. < selector xmlns:android = "Http://schemas.android.com/apk/res/android" >
  3. < Item android:state_pressed = "true" >
  4. < bitmap android:src = "@drawable/toolbar_bg_sel" Android:tilemode = "disabled" android:gravity = "Top"/>
  5. </Item >
  6. < item >
  7. < bitmap android:src = "@drawable/toolbar_bg" Android:tilemode = "disabled" android:gravity = "Top"/>
  8. </Item >
  9. </selector >

So, no matter whether the background is set in code mode Setbackgroundresource () or XML android:background, no stretch is produced.

Third, ImageButton use transparent pictures:

There are many irregular buttons on Android. Such as:

This time, if we want to make an irregular button, the first step is to get a transparent edge of the PNG image, and then specify to him with SRC, this time we will find that the effect has not been achieved, there is still a layer of images around the rendering. At this point also to take the second step: the need to set the background property ImageButton android:background= "#00000000", the implementation of the effect of the irregular button.

A friend may say that it is OK to specify the picture directly with background, but if you specify it with background, Fills the picture with the entire ImageButton. For example, the ImageButton is 200, and the picture is only 50 long, then the image will be stretched 4 times times blurred. And the effect we're trying to achieve is not to fill. Like the hammer icon above. The middle part is OK.

The difference between the SRC and background of "Android" ImageView and the magical magic of both

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.