In the previous blog post, I mainly explained the XML image resources in the layer resources, in this Image resource blog post I will give you a succession of XML image resources of image state resources, image level resources, fade resources, embedded image resources, cut image resources and shape resources.
1. Image Status Resources:
The default style of the button control provided by the Android SDK is somewhat monotonous, and this style is very uncoordinated with the flashy interface. Of course, we can use ImageView or Imgaebutton controls to make cool buttons with images of different states, here I give the effect of using Java code to push the button down with the normal state:
Btn.setontouchlistener (New View.ontouchlistener () {public Boolean OnTouch (View V, motionevent event) { if ( Event.getaction () = = Motionevent.action_down) { //reset the background picture when pressed ( (ImageButton) v). Setimagedrawable ( Getresources (). getdrawable (R.drawable.send_down)); } else if (event.getaction () = = motionevent.action_up) { //re-modify the normal picture ((ImageButton) v) when lifted. Setimagedrawable ( Getresources (). getdrawable (R.drawable.send)); } return false; } });
The above code is used to implement the image switch effect of the button. As you can see, you need to write quite a few Java code to implement the button's down effect. To do this, Android provides a way to change the default style of the button, which does not require writing a single line of Java code.
Different styles are displayed when the buttons are in different states (normal, pressed, get focus, and so on), which are typically rendered using different images, which requires that you specify the images that correspond to different states, and that the image state resource is used to specify those images.
The image state resource is a file in XML format and must be a <selector> tag as the root node. A number of <item> tags are included in the <selector> tab to specify the appropriate image resources. Here is a concrete example:
The file is named Button.xml, and it is in the corresponding drawable directory.
<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android > <!--Press the Status property state_pressed to True when the button is pressed, use this item's corresponding picture to act as the background of the button- <item android: State_pressed= "true" android:drawable= "@drawable/pressed"/> <!--get the meaning of the focus state_focused property and State_ Pressed similar-- <item android:state_focused= "true" android:drawable= "@drawable/focused"/> <!-- Default state- <item android:drawable= "@drawable/normal"/></selector>
The effect is as follows:
In the days to come, I'll try to get as much time as I can to write more good posts.
Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/31341353