Android resources-image resources (1)

Source: Internet
Author: User

I used to look at xml Resources in the drawable folder of other people's programs. To be honest, when I first saw such xml image resources, I really didn't know what to do. After learning a resource, I learned how to use it. Let me share this part of knowledge:

The image resource files in Android are stored in the res/drawable directory. In the image Resource Directory, you can not only store image files in various formats (jpg, png, gif, etc.), but also use image resources in various XML formats to control the status and behavior of images.

1. common image resources

Android supports three image formats: png, jpg, and gif. We recommend that you use images in png format. jpg can also be used. However, image files in gif format are not encouraged because the Android SDK currently does not support animated gif files.

The use of common image resources is relatively simple, so I will not mention it here. This article mainly describes XML image resources in Android.

2. XML image resources

XML image resources are used to specify the image resources in the drawable directory in the XML file. XML image resource usage Label definition.

Let's take a look at the following example:

Create an XML image resource file named bitmap_test.xml in the drawable folder. The Code is as follows:

 
 

Let's take a look at how to reference this XML image resource file in java code. The Code is as follows:
Resources res=getResources();Drawable drawable=res.getDrawable(R.drawable.bitmap_test);TextView txt=(TextView)findViewById(R.id.textView);txt.setBackground(drawable);
Of course, referencing this XML image resource can also reference this bitmap_test.xml image resource like referencing a common image resource in an XML file, which is not mentioned here.

2.1 Layer Resources

Some layer resources are similar to <frameLayout> in the layout. The difference is that the <frameLayout> label can contain any control, while each layer of layer resources can only contain images. The layer resource must be defined Label is the root node of the resource file, Tags can contain multiple Tag, each Label indicates an image, the last one The tag is displayed at the top layer. Use the following code Specifies an image.

By default, the image will be filled with the view of the displayed image as much as possible. Therefore, the displayed image may be stretched. To avoid image stretching, you can Label Tags reference images.

The following is an example of a layer resource.

The layer_test.xml code of the layer resource file is as follows:

     
          
       
      
          
       
  
 

Reference this layer resource in the XML layout file as follows:

 
 


The effect of this layer resource case is 1-2:


Figure 1-2 layer Resource Case Study

If there are no special circumstances, we recommend that you use layers to achieve the effect of overlapping multiple images.

Due to time issues, I will talk about image resources of Android resources first, and I will make up the remaining content after graduation tomorrow.

Reprinted please indicate the source: http://blog.csdn.net/android_jiangjun/article/details/30789545



Related Article

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.