Image Resources for Android resources (state image resources)

Source: Internet
Author: User

In the previous blog post. I mainly explain the XML image resources in the layer resources, in this image resources blog post I will give you a succession of XML image resources of the 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 appears somewhat monotonous. And this style is very uncoordinated with the dazzling interface. Of course. We were able to use the ImageView or Imgaebutton controls to make a very cool button with different state images, 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) {                      //once again set the background picture                 ((ImageButton) v) when pressed. Setimagedrawable ( Getresources (). getdrawable (R.drawable.send_down));                                          } else if (event.getaction () = = motionevent.action_up) {                       //re-change to normal picture when lifted (                  (ImageButton) v). Setimagedrawable ( Getresources (). getdrawable (R.drawable.send));                 }              return false;           }       });  

The above code is used to implement the button's picture switching effect.

As you can see, you need to write quite a lot of Java code to achieve the button's down effect. To do this, Android provides a way to change the default style of a button without having to write a single line of Java code.

Different styles are displayed when the button is in a different state (normal, pressed, get focus, and so on), which are typically rendered with different images, which requires an image that corresponds to a different state, and 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 corresponding image resources.

Take a detailed look at an example:

The file name is Button.xml, which corresponds to the Drawable folder.

<?xml version= "1.0" encoding= "Utf-8"? ><selector xmlns:android= "http://schemas.android.com/apk/res/ Android >    <!--press the status attribute state_pressed to True when the button is pressed, use the corresponding picture of this item as the background of the button-    <item Android:state_pressed= "true" android:drawable= "@drawable/pressed"/>    <!--get Focus  State_ The Focused property has a meaning similar to state_pressed--    <item android:state_focused= "true" android:drawable= "@drawable/ Focused "/>    <!--default state--    <item android:drawable=" @drawable/normal "/></selector>

The effect is as you see it:

In the days to come, I'll try to get as much time as I can to write a lot of other better posts.

Reprint Please specify source: http://blog.csdn.net/android_jiangjun/article/details/31341353


Image Resources for Android resources (state image resources)

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.