Android資源之映像資源(映像層級資源)

來源:互聯網
上載者:User

標籤:android平台   ui   xml   imageview   布局   

映像狀態資源只能定義有限的幾種狀態。如果需要更多的狀態,就要使用映像層級資源。在該資源檔中可以定義任意多個映像層級。每個映像層級是一個整數區間,可以通過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);}//"開燈"按鈕的單擊事件方法public void onClick_LampOn(View view){//設定level為15,顯示lamp_on.pngivLamp.setImageLevel(15);}//"關燈"按鈕的單擊事件方法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;}}


 

結合上一篇的博文,大家可以看出,映像狀態資源與映像層級資源都可以用來實現按鈕不同狀態顯示不同的映像的效果,如果你的控制只需顯示2個或3個效果你可以使用映像狀態資源,但是如果你想顯示更多的效果,還是使用映像層級資源。

案例效果如下:


轉載請註明出處:

http://blog.csdn.net/android_jiangjun/article/details/32308551
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.