標籤:源檔案 wap 不同 off nts ssl fill back ems
映像狀態資源僅僅能定義有限的幾種狀態。
假設須要很多其它的狀態,就要使用映像層級資源。
在該資源檔裡能夠定義隨意多個映像層級。
每一個映像層級是一個整數區間,能夠通過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")@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);//設定level為8。顯示lamp_off.pngivLamp.setImageLevel(8);}//"開燈"button的單擊事件方法public void onClick_LampOn(View view){//設定level為15,顯示lamp_on.pngivLamp.setImageLevel(15);}//"關燈"button的單擊事件方法public void onClick_LampOff(View view){//設定level為6,顯示lamp_off.pngivLamp.setImageLevel(6);}@Overridepublic 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資源之映像資源(映像層級資源)