drawable Resources in Android

Source: Internet
Author: User

In Android applications, drawable resources are often used, such as tablet resources, and in Android development we use drawable classes to drawable types of resources.

drawable resources are generally stored in the application directory of the \res\drawable directory, of course, depending on the resolution of the high and low can be stored in different resolutions of the resources to the following directories:

\res\drawable-hdpi

\res\drawable-ldpi

\res\drawable-mdpi

\res\drawable-xdpi

The following is stated in its SDK documentation:

We see that drawable is an abstract class, which has a lot of subclasses (subclass) to manipulate specific types of resources, such as bitmapdrawable for manipulating bitmaps, colordrawable for manipulating colors,

The clipdrawable is used to operate the shearing plate.

Picture Resources

Image resources are simple drawable resources, currently supported by Android image format: gif, PNG, JPG and so on. We only need to put the picture resources into the \res\drawable, then in the compiled R.java class will generate the resource ID of the picture resource

We can get the picture resource by invoking the relevant method in the program (if you want to access Drawable_resource_file_name in the program, you can: [PackageName]. R.drawable.drawable_resource_file_name).

Note: The resource name in Drawable in Android is constrained and must be: [a-z0-9_.] (that is, only alphanumeric and _ and.), and cannot start with a number, otherwise the compilation will error: Invalid file name:must contain only [a-z0-9_.]

The following code snippet shows how to access a picture resource (resource name Drawablefilename):

ImageView imageview= (ImageView) Findviewbyid (com.jeriffe.app.r.id.imageview1); Imageview.setimageresource ( Com.jeriffe.app.r.drawable.drawablefilename);

Statelistdrawable Resources

A set of drawable resources can be assigned within statelistdrawable, statelistdrawable is defined in an XML file, <selector> starting with the element. Each of its internal drawable resources is embedded in a<item>元素中。

When the statlistdrawable resource is a component background or foreground drawable resource, the corresponding resource can be automatically switched as the component state changes, for example, a button can be in a different state (buttons pressed, get focus)

We can use a statelistdrawable resource to provide different background images for each state. , when the state of the component changes, the statelistdrawable corresponding XML file is automatically traversed to find the first matching item.

The component States supported by the Statlistdrawable resource are as follows:

The following code snippet is a sample XML file description for a statelistdrawable resource:

The XML file is stored in: res/drawable/button_statelist.xml :

<?XmlVersion= "1.0"Encoding= "Utf-8"?><SelectorXmlns:android= "Http://schemas.android.com/apk/res/android"><ItemAndroid:state_pressed= "true"Android:drawable= "@drawable/button_pressed"/><!--pressed--<item android:state_focused= " True "android:drawable=" @drawable/button_focused "/> <!--focused-to <item android : State_hovered= "true" android:drawable= "@drawable/ button_focused "/> <!--hovered--<item android:drawable= "@drawable/button_normal" /> <!--default--></ Selector>            

The following is the layout file for the button:

<button    android:layout_height= "Wrap_content"    android:layout_widthandroid: Background/>     

Of course we can also use the code to set the button's background image:

Button imagebutton= (button) Findviewbyid (R.id.imagebutton); Imagebutton.setbackgroundresource ( Com.jeriffe.app.r.drawable.button_statelist);

Shapedrawable Resources

The shapedrawable resource draws a specific shape, such as a rectangle, ellipse, and so on. If you want to draw two-bit graphics on your own, then we can use the Shapedrawable resource object, using Shapedrawable, we can draw the shape we can imagine. A shapedrawable requires a Shape object to manage rendering resources to UI screen, and if there is no shape setting, the Rectshape object is used by default.

Shapedrawable is defined in an XML file, starting with an <shape> element. Each of its internal drawable resources is embedded in a<item>元素中

以下代码片段便是一个ShapeDrawable的XML定义

<?XmlVersion= "1.0"Encoding= "UTF-8"?><ShapeXmlns:android= "Http://schemas.android.com/apk/res/android"Android:shape= "Oval"><!--define fill gradient color--<GradientAndroid:startcolor= "#00f"android:endcolor= "#00f" android:angle = "" android:type= "Sweep" /> Span class= "REM" ><!--set-up-<padding android: Left= "7DP" android:top= "7DP" android : Right= "7DP" android:bottom= "7DP" / > <!--set Rounded Rectangle--<corners  Android:radius= "8DP" /> </ Shape>             

We can use the shapedrawable to set the component's background color (with setBackgroundDrawable()方法 ), such as the code fragment above can be set to a textedit background color blue oval shape. Of course, we can also draw the custom view

When we build a custom shape view, because Shapedrawable has its own draw () method, we can construct a subclass of the view view and then override the View.ondraw () method, the following snippet is a sample:

Class Customdrawableview extends View {      private shapedrawable mdrawable;      Public Customdrawableview (context context) {      super (context);      int x = ten;      new shapedrawable (void OnDraw (canvas canvas) {mdrawable.draw (canvas);}}    

Based on the code above, we can build custom views programmatically in our activity:

Customdrawableview Mcustomdrawableview;       void OnCreate (Bundle savedinstancestate) {      super.oncreate (savedinstancestate);      New Customdrawableview (this);      Setcontentview (Mcustomdrawableview);      }  

Of course we can also use the XML file to describe: The custom drawable class must overload the view (Context, AttributeSet) constructor. Then we add the layout file as follows:

<com.example.shapedrawable.CustomDrawableView      android:layout_width= "Fill_parent"      android: layout_height= "wrap_content"      />   

Clipdrawable

The clipdrawable resource is defined in an XML that represents a crop (Clips) that is based on the level of the clipdrawable resource. You can control the width height and gravity properties of the cropped drawable, clipdrawable is often used as a progressbars implementation.

The following example is a clipdrawable resource:

<?  versionencoding= "Utf-8"?  ><xmlns:android= "http://schemas.android.com/apk/res/android"android:drawable= "@ Drawable/android "android:cliporientation=" Horizontal "android:gravity/>  

The following ImageView layout file applies the clipdrawable resource:

<ImageViewandroid:id= "@+id/image"android:background= "@drawable/clip"android:layout_ Height= "wrap_content"android:layout_width/>        

The following code gets the drawable and increases its clipping so that the image is displayed in an incremental

ImageView ImageView = (ImageView) Findviewbyid (r.id.image); Clipdrawable drawable = (clipdrawable) imageview.getdrawable ();d Rawable.setlevel (Drawable.getlevel () + 1000);

Of course we can use a timer to realize the image's incremental display.

Note: The default level value is 0, which means that the picture is clipped, so the picture is not visible. When the value reaches 10000, the code is clipped to 0, and the picture can be displayed completely.

Animationdrawable

Animationdrawable

Animationdrawable constructs a frame-based animation (Frame-by-frame animations) by defining a series of drawable objects that can be used as the background color of the view.

The simplest way to build a frame animation is to construct an animation in an XML file, and we can set the animation as the background color of the view and run the animation by calling the Animationdrawable.start () method.

The following code fragment is an XML file of a animationdrawable resource, resource file location: Res\drawable\spin_animation.xml

<Animation-listXmlns:android= "Http://schemas.android.com/apk/res/android"android:oneshot= "true" > << span class= "HTML" >item android:drawable= "@drawable/rocket_thrust1" android:duration= "$" /> <item android:drawable= "@drawable/rocket_thrust2" android:duration= "$" /> << span class= "HTML" >item android:drawable= "@drawable/rocket_thrust3" android:duration= "$" /></animation-list>          

We can see that the animationdrawable resource file<animation-list>元素为根,包含一系列的<Item>节点,每一个节点定义了一个帧(frame)及持续时常。

上述动画运行了3个帧,通过设置android:oneshot 属性(attribute)为true,动画会循环一次并停留在最后一帧,如果为false那么会轮询(loop)的运行动画

We can load the playback animation by encoding:

注意:AnimationDrawable. start()方法不能够在Activity的onCreate()方法中调用,因为AnimationDrawable还未完全的附加(attached)到Window,如果你不需要交互而立即播放动画,那么可以在onWindowFocusChanged() 方法中,这个方法会在你的Activity Windows获取焦点是触发。

Conclusion

At this point our drawable resources are introduced, of course, there are a lot of drawable sub-class is not introduced, interested in the SDK to focus on documentation.

drawable Resources in Android

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.