3.APP Resources-resource types/drawable Resources

Source: Internet
Author: User



1. drawable Resources



There is several different types of drawables as follow



2. Bitmap



A bitmap image. Android supports bitmap files in a three formats: (preferred), (acceptable), (discouraged).png.jpg.gif.



Bitmap files May is automatically optimized with lossless image compression by theaapttool during the build PR Ocess.



If you plan on reading an image as a bit stream in order to convert it to a bitmap, put your images in T He res/raw/ folder instead, where



They is not being optimized.



3. Nine-patch



A isNinePatcha PNG image in which you can define stretchable regions that Android scales when content within the Vi EW exceeds the normal



Image bounds. You typically assign this type of image as the background of a View the have at least one dimension set"wrap_content"to,



And when the view grows to accomodate the content, the Nine-patch image was also scaled to match the size of the view. An example



Use of a nine-patch image was the background used by Android's standardButtonwidgets, which must stretch to accommodate the Text



(or image) inside the button.



4. Layer List



A isLayerDrawablea drawable object This manages an arrayof the other drawables. Each drawable in the list was drawn in the order of the list



-the last drawable in the list was drawn on top.


//res/drawable/layers.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item>
      <bitmap android:src="@drawable/android_red"
        android:gravity="center" />
    </item>
    <item android:top="10dp" android:left="10dp">
      <bitmap android:src="@drawable/android_green"
        android:gravity="center" />
    </item>
    <item android:top="20dp" android:left="20dp">
      <bitmap android:src="@drawable/android_blue"
        android:gravity="center" />
    </item>
</layer-list>


 

This layout XML applies the drawable to a View:


<ImageView
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/layers" />



The result is a stack of increasingly offset images:









5. State List



A isStateListDrawablea drawable object defined in XML this uses a several different images to represent the same graphic, depending



The state of the object


//res/drawable/button.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:drawable="@drawable/button_pressed" /> <!-- pressed -->
    <item android:state_focused="true"
          android:drawable="@drawable/button_focused" /> <!-- focused -->
    <item android:state_hovered="true"
          android:drawable="@drawable/button_focused" /> <!-- hovered -->
    <item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>


<Button
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:background="@drawable/button" />

6. Transition drawable



A isTransitionDrawablea drawable object that can cross-fade between the drawable resources.



Each drawable are represented by an<item>element inside a single<transition>element. No more than the other items are supported. To



Transition forward , callstarttransition (). To transition backward, callreversetransition ().


//res/drawable/transition.xml
<?xml version="1.0" encoding="utf-8"?>
<transition xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/on" />
    <item android:drawable="@drawable/off" />
</transition>
<ImageButton
    android:id="@+id/button"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"
    android:src="@drawable/transition" />
//And the following code performs a 500ms transition from the first item to the second:
ImageButton button = (ImageButton) findViewById(R.id.button);
TransitionDrawable drawable = (TransitionDrawable) button.getDrawable();
drawable.startTransition(500);


7 .....



3.APP Resources-resource types/drawable Resources


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.