Before looking at someone else's program in the Drawable folder has XML resources, to tell the truth that the first time to see such XML image resources, I really do not know what to do. Take time to learn a similar resource, only to understand the magical image of such resources. Let me share some of this knowledge:
Image resource files in Android are saved 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 various XML-formatted image resources to control the state and behavior of images.
1. Common Image Resources
Android supports 3 image formats: PNG, JPG, and GIF. The official recommendation is to use PNG-formatted image resources, JPG can also be considered. But GIF format image files are not encouraged to use because the Android SDK currently does not support animated GIFs.
The use of ordinary image resources is relatively simple, so I do not have too much to tell. This article is mainly about XML image resources in Android.
2. XML Image Resources
The XML image resource is essentially the image resource specified in the drawable directory in the XML file. XML image resource use <bitmap> tag definition.
Let's take a look at the following example:
Create a new XML image resource file under a drawable folder, with the file name Bitmap_test.xml. The code is as follows:
<?xml version= "1.0" encoding= "Utf-8"? ><bitmap xmlns:android= "Http://schemas.android.com/apk/res/android" android:src= "@drawable/ic_launcher" android:tilemode= "Mirror" />
Here's a look at how to refer to this XML image resource file in Java code, the code is as follows:
Resources res=getresources ();D rawable 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 refer to the Bitmap_test.xml image resource in the same way that the XML file refers to the normal image resource, and there is not much to say here.
2.1. Layer Resources
Layer resources are somewhat similar to <framelayout> in layouts, except that the <FrameLayout> tag can contain arbitrary controls, and each layer in a layer resource can contain only images. The definition layer resource must use the <layer-list> tag as the root node of the resource file,<layer-list> tags can contain multiple <item> tags, each <item> tag represents an image, The last <item> tag will be displayed at the top level. The following code uses <item> to specify an image.
<item android:drawable= "@drawable/image" >
By default, the image is as full as possible to display the view of the image. Therefore, the displayed image may be stretched. To avoid image stretching, you can use the <bitmap> tag to refer to an image in the <item> tab.
Let's look at an example of a layer resource
The Layer_test.xml code for the layer resource file is as follows:
<layer-list xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:top= "20DP" android:left= "20DP" > <bitmap android:src= "@drawable/qrcode" android:gravity= "center"/> </item> <item android:top= "20DP" android:left= "20DP" > <bitmap android:src= "@drawable/ Desktoplog " android:gravity=" center "/> </item></layer-list>
Reference the layer resource in the XML layout file, as follows:
<imageview android:layout_width= "fill_parent" android:layout_height= "fill_parent" android:src= "@drawable/layer_test" />
The layer resource case is shown in effect 1-2:
Figure 1-2 Layer Resource Case Demo effect
It is recommended to use layers to achieve multiple image coincident effects without special circumstances.
Because of the time problem, the image resources of Android Resources I will be here first, tomorrow after the graduation reply, then the rest of the content.
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/30789545