Image Resources for Android resources (Image level resources)

Source: Internet
Author: User

Image State resources can only define a limited number of States.

If you need a lot of other states, you need to use image-level resources.

In this resource file, you can define arbitrarily multiple image levels.

Each image level is an integer interval that allows you to switch between images of different states using the Imageview.setimagelevel or Drawable.setlevel method.

The image level resource is a file in XML format, and the <level-list> tag must be the root node of the XML.

The <level-list> tag can have a random number of <item> tags, each <item> tag represents a level interval.

The level interval is set with the Android:minlevel and Android:maxlevel properties.

Setimagelevel or Setlevel method sets the level within a certain interval (android:minlevel<=level<=android:maxlevel), the system will first use which interval corresponding image (with Android: Drawable property setting). When this resource file is created. When using new Android XML, there is no XML for the root node <level_list>. It's just that it doesn't matter that you can create a new generic XML file. You can then write the corresponding code.

Below I give a detailed example (switching lights):

Lamp.xml Image level resource files such as the following:

<?

XML version= "1.0" encoding= "UTF-8"?

><level-list xmlns:android= "Http://schemas.android.com/apk/res/android" > <item android:drawable= "@drawable/lamp_off" android:minlevel= "6" android:maxlevel= "></item> <item android:drawable=" @ drawable/lamp_on "android:minlevel=" "android:maxlevel=" ></item></level-list>


The Lamp.xml file above defines a total of two levels of image resources.

The following gives the main page layout file Main_layout.xml file, for example:

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "   android:orientation= "vertical"    tools:context= ". Mainactivity ">  <imageview      android:id=" @+id/imageview_lamp "      android:layout_width=" fill_parent "      android:layout_height=" wrap_content "      android:src=" @drawable/lamp "      />  <button       android:onclick= "Onclick_lampon"      android:layout_width= "wrap_content"      android:layout_height= "Wrap_ Content "      android:text=" turn on the Light "      />   <button       android:onclick=" Onclick_lampoff "      Android : layout_width= "wrap_content"      android:layout_height= "wrap_content"      android:text= "lights off"      /></ Linearlayout>

corresponding to the mainactivity The code for example is the following:

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 level to 8. Show Lamp_off.pngivLamp.setImageLevel (8);} Click event method for "Turn on light" button public void Onclick_lampon (view view) {//Set level to 15, display Lamp_on.pngivLamp.setImageLevEl (15);} Click event method for "off light" button public void Onclick_lampoff (view view) {//Set level is 6, display lamp_off.pngivLamp.setImageLevel (6);} @Overridepublic boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; This adds items to the action bar if it is PR Esent.getmenuinflater (). Inflate (R.menu.main, menu); return true;}}


Combined with the previous blog post, you can see that the image State resources and image level resources can be used to achieve the button different state display different images of the effect, assuming that your control only need to display 2 or 3 effects you can use the image state resources, but suppose you want to show a lot of other effects. Or use an image-level resource.

Case effects such as the following:


Reprint please specify the source:

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

Image Resources for Android resources (Image level 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.