Android resources-image resources (image-level resources)

Source: Internet
Author: User

Image status resources can only define a limited number of statuses. If you need more statuses, you need to use image-level resources. You can define any number of image levels in this resource file. Each image level is an integer interval. You can use the ImageView. setImageLevel or Drawable. setLevel method to switch between images of different States.

Image-level resources are XML files and must Label is the root node of XML. Tags can contain any number of tags. Tag, each A label indicates a level interval. Set the level interval with the android: minLevel and android: maxLevel attributes. The setImageLevel or setLevel method sets the level within a certain range (android: minLevel <= level <= android: maxLevel), and the system uses the image corresponding to the interval (android: drawable attribute settings ). When this resource file is created and Android XML is used, no root node is But it doesn't matter. You can create a new common xml file and write the corresponding code.

I will provide a specific example (switch light) below ):

Lamp. xml:

 

 

The lamp. xml file above defines two levels of image resources in total.

The main_layout.xml file of the homepage layout is provided as follows:

   
  
   
   
  
 

The MainActivity code is as follows:

 

Package com. gc. drawablestudy; import android. OS. bundle; import android. annotation. suppressLint; import android. app. activity; import android. content. res. resources; import android. graphics. bitmap; import android. graphics. color; import android. graphics. drawable. drawable; import android. view. menu; import android. view. view; import android. widget. imageView; import android. widget. textView;/** author: Android General */public class MainActivity extends Activity {private ImageView ivLamp; @ SuppressLint (NewApi) @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // Resources res = getResources (); // Drawable drawable = res. getDrawable (R. drawable. bitmap_test); // TextView txt = (TextView) findViewById (R. id. textView); // txt. setBackground (drawable); ivLamp = (ImageView) findViewById (R. id. imageview_lamp); // set levelto 8 to display lamp_off.png ivLamp. setImageLevel (8);} // when the light is turned on, click the event Method public void onClick_LampOn (View view) {// set levelto 15 to display lamp_on.png ivLamp. setImageLevel (15);} // set the event Method public void onClick_LampOff (View view) {// set levelto 6 and display lamp_off.png ivLamp. setImageLevel (6) ;}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}


 

 

Based on the previous blog, we can see that both image State resources and image level resources can be used to display different images in Different buttons, if you only need to display 2 or 3 effects, you can use image status resources. However, if you want to display more effects, you can still use image-level resources.

The results are as follows:

Reprinted please indicate the source:

Http://blog.csdn.net/android_jiangjun/article/details/32308551

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.