Android Internship notes (ImageView)---src Properties VS blackground Properties

Source: Internet
Author: User

Android Internship notes (ImageView)---src Properties VS blackground Properties

Reprint Please specify Source: Coder-pig



Problem Analysis

I believe you are not unfamiliar with imageview picture components, see the name of the idea, is used to display pictures of it!

The display of images can be set by the SRC attribute, or the Blackground property!

These people know, but there is no way to tangle under what circumstances use this, under what circumstances use that?

It is estimated that many of my friends ' answers are "no", haha, so in this section you will have to explain the two properties!



First, the most people know:


A. Common sense

① background usually refers to the background , while src refers to content !!

② when using src when filling in a picture, it is by image size directly filled, and no stretching is done

and using background fill in the picture, it will be stretches according to the width of the imageview given


Say no more useless, write a simple layout test:

Main.xml:

<linearlayout xmlns: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_hei    ght= "match_parent" android:orientation= "vertical" tools:context= "com.jay.example.imageviewdemo.MainActivity" > <imageview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" Android:back ground= "@drawable/pen"/> <imageview android:layout_width= "200DP" android:layout_height= "Wrap_conte NT "android:background=" @drawable/pen "/> <imageview android:layout_width=" Wrap_content "and roid:layout_height= "Wrap_content" android:src= "@drawable/pen"/> <imageview android:layout_width= "2 00DP "android:layout_height=" wrap_content "android:src=" @drawable/pen "/></linearlayout>

the code is simple, the front two with Blackground, the back two with SRC, as follows:


Analysis:

The width and height are wrap_content that's the same as the original size, but when we fixed the wide or High words,

The difference is obvious, blackground completely fills the whole imageview, and SRC is still so big,

and he's centered, which involves ImageView another property of ScaleType out!

In addition, here we say only set width or height Oh! Join us at the same time set the

width and height words , the Blackground is still filled, 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 "/>

:



PS: As for ImageView's ScaleType here is not explained in detail, the details can be read the author's other blog post:

ImageView image view of Android-ui component instance Daquan

In addition, this property only takes effect on the SRC attribute Oh!! Remember!!





two. To solve the image deformation caused by blackground stretching method

In the second ImageView in the previous section we can see that the picture has been stretched and deformed,

The square becomes a rectangle, which is obviously unacceptable to people like me who have mild obsessive-compulsive disorder.

Is there any way to set it up? The answer must be some, the author temporarily know the following two ways:


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

Linearlayout.layoutparams Layoutparam = new Linearlayout.layoutparams (;  Layout.addview (Ibtnpen, Layoutparam);  

2) In addition to dynamically loading the view, more often, we will introduce the XML layout to the ImageView

The solution is also not difficult, is through the drawable bitmap resource file to complete, and then Blackground property 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"        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 Settings

When the background is tiled, multiple small icons spread over 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"



three. Setting the transparency issue

Say the first two differences, then the next Setalpha attribute!

This is very simple, this property, only src when it is effective!!


!!!! src setting Setalpha () ONLY WORKS!!!!!



Four. The combination of the two magical:

Here is a picture on the Web:



A look is a simple GridView, each item is a imageview, but careful you may find out,

The above icon is not a regular, but round, rounded rectangle and so on, so here is the SRC + background!



To achieve the above effect, you only need two actions:

Find a transparent PNG image + set a black background

(Of course, you can also set the transparency of PNG to achieve, but the results may be the same as expected Oh!)


Haha, the pig is so cute to show on the ImageView, haha, blackground set a blue background!

The code is as follows:

    <imageview        android:layout_width= "150DP"        android:layout_height= "wrap_content"        android:src= "@ Drawable/pig "        android:background=" #6699FF "/>

In addition, you can use Selctor to achieve click Effect, set different circumstances to set different pictures, to achieve the click or touch effect!





Well, this section will be here, if you have any questions about this article or find any flaws, welcome to inform, extremely grateful!!





Android Internship notes (ImageView)---src Properties VS blackground Properties

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.