Android资源之图像资源(图像级别资源)

图像状态资源只能定义有限的几种状态。如果需要更多的状态,就要使用图像级别资源。在该资源文件中可以定义任意多个图像级别。每个图像级别是一个整数区间,可以通过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);
		
	}

	//"开灯"按钮的单击事件方法
	public void onClick_LampOn(View view)
	{
		//设置level为15,显示lamp_on.png
		ivLamp.setImageLevel(15);
	}
	//"关灯"按钮的单击事件方法
		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;
	}

}


 

结合上一篇的博文,大家可以看出,图像状态资源与图像级别资源都可以用来实现按钮不同状态显示不同的图像的效果,如果你的控制只需显示2个或3个效果你可以使用图像状态资源,但是如果你想显示更多的效果,还是使用图像级别资源。

案例效果截图如下:


转载请注明出处:

http://blog.csdn.net/android_jiangjun/article/details/32308551
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值