图像状态资源仅仅能定义有限的几种状态。
假设须要很多其它的状态,就要使用图像级别资源。
在该资源文件里能够定义随意多个图像级别。
每一个图像级别是一个整数区间,能够通过ImageView.setImageLevel或Drawable.setLevel方法切换不同状态的图像。
图像级别资源是XML格式的文件,必须将<level-list>标签作为XML的根节点。
<level-list>标签中能够有随意多个<item>标签,每个<item>标签表示一个级别区间。
级别区间用android:minLevel和android:maxLevel属性设置。
setImageLevel或setLevel方法设置的级别在某个区间内(android:minLevel<=level<=android:maxLevel),系统就会先用哪个区间相应的图像(用android:drawable属性设置)。在建立这个资源文件时。採用新建Android XML时,没有根节点为<level_list>的xml。只是这不要紧,能够新建一个全新的普通的XML文件。然后写入对应代码就可以。
以下我给出一个详细的实例(开关灯):
lamp.xml图像级别资源文件例如以下:
<?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="10"></item> <item android:drawable="@drawable/lamp_on" android:minLevel="12" android:maxLevel="20"></item> </level-list>
上面的lamp.xml文件总共定义了两个级别的图像资源。
以下给出主页面布局文件main_layout.xml文件,例如以下:<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="开灯"
      />
   <Button 
      android:onClick="onClick_LampOff"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="关灯"
      />
</LinearLayout>
对应的MainActivity的代码例如以下:
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将军*/
public class MainActivity extends Activity {
	private ImageView ivLamp;
	@SuppressLint("NewApi")
	@Override
	protected 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);
		//设置level为8。显示lamp_off.png
		ivLamp.setImageLevel(8);
		
	}
	//"开灯"button的单击事件方法
	public void onClick_LampOn(View view)
	{
		//设置level为15,显示lamp_on.png
		ivLamp.setImageLevel(15);
	}
	//"关灯"button的单击事件方法
		public void onClick_LampOff(View view)
		{
			//设置level为6,显示lamp_off.png
			ivLamp.setImageLevel(6);
		}
	@Override
	public 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;
	}
}
结合上一篇的博文,大家能够看出,图像状态资源与图像级别资源都能够用来实现button不同状态显示不同的图像的效果,假设你的控制仅仅需显示2个或3个效果你能够使用图像状态资源,可是假设你想显示很多其它的效果。还是使用图像级别资源。
案例效果截图例如以下:
转载请注明出处:
http://blog.csdn.net/android_jiangjun/article/details/32308551原文:http://www.cnblogs.com/tlnshuju/p/7073358.html