Image Resources for Android resources (Image level resources)

Source: Internet
Author: User

Image State resources can only define a limited number of States. If more states are needed, use an image-level resource. You can define any number of image levels in the resource file. Each image level is an integer interval that can be toggled by Imageview.setimagelevel or Drawable.setlevel methods for different state images.

The image level resource is a file in XML format, and the <level-list> tag must be the root node of the XML. <level-list> tags can have any 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 to the image (with Android: Drawable property setting). When creating this resource file, when using the new Android XML, there is no root node for <level_list> XML, but it does not matter, you can create a new normal XML file, and then write the corresponding code.

Below I give a specific example (switching lights):

Lamp.xml Image level resource files are as follows:

<?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.

Here is the main page layout file Main_layout.xml file, as follows:

<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>

the corresponding mainactivity the 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 level to 8, display lamp_off.pngivLamp.setImageLevel (8);} Click event method for the Turn on light button public void onclick_lampon (view view) {//Set level to 15, display Lamp_on.pngivLamp.setImageLevel (15);} Click event method for the off light button public void Onclick_lampoff (view view) {//Set level to 6, showing 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 different state of the button display different images of the effect, if your control only need to display 2 or 3 effects you can use the image state resources, but if you want to show more effects, or use image level resources.

The case effect is as follows:


Reprint please specify the source:

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

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.