1. 程式人生 > >Android資源之圖像資源(圖像級別資源)

Android資源之圖像資源(圖像級別資源)

ons 分享 博文 target button track off http tails

圖像狀態資源僅僅能定義有限的幾種狀態。

假設須要很多其它的狀態,就要使用圖像級別資源。

在該資源文件裏能夠定義隨意多個圖像級別。

每一個圖像級別是一個整數區間,能夠通過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

Android資源之圖像資源(圖像級別資源)