Android LevelList使用实例

大家对android系统电池状态改变的显示已经很熟悉了,但它是如何实现的呢?它是利用了什么技术呢?也许你有你自己的实现方式,但android系统是利用LevelList来实现的。你知道么?

下面通过一个具体的实例来说明一下:

运行示意图:1.为初始化的界面;2.为输入25时的界面;3.为输入45时的界面;4.为输入65时的界面;5.为输入0时的界面,6.为输入100时的图片,7为输入105时的图片

                

 

新建一个名称为LevelList的Adnroid项目工程

 

编写界面文件activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent">  
  5.   
  6.     <ImageView  
  7.         android:id="@+id/level_list_img"   
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:src="@drawable/levellist"  
  11.         android:layout_alignParentTop="true"  
  12.         android:layout_centerHorizontal="true"  
  13.         android:contentDescription="@string/app_name"  
  14.         />  
  15.     <EditText   
  16.         android:id="@+id/level_et"  
  17.         android:layout_width="match_parent"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_centerVertical="true"  
  20.         android:inputType="numberSigned"  
  21.         />  
  22.     <Button   
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:onClick="change"  
  26.         android:text="@string/button_text"  
  27.         android:layout_alignParentBottom="true"  
  28.         android:layout_centerHorizontal="true"  
  29.         android:layout_marginBottom="20dp"/>  
  30. </RelativeLayout>  


在ImageView中使用了LevelList图形资源,该图形资源使用了LevelList的技术,levellist.xml位于drawable文件夹下。

Levellist.xml文件如下所示:

 

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <level-list xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.     <!-- 0到 20显示这个图片-->  
  4.     <item android:drawable="@drawable/s1" android:minLevel="0" android:maxLevel="20"></item>  
  5.      <!-- 21到 40显示这个图片-->  
  6.     <item android:drawable="@drawable/s2" android:minLevel="21" android:maxLevel="40"></item>  
  7.     <!-- 41到 60显示这个图片-->  
  8.     <item android:drawable="@drawable/s3" android:minLevel="41" android:maxLevel="60"></item>  
  9.     <!-- 61到100显示这个图片-->  
  10.     <item android:drawable="@drawable/s4" android:minLevel="61" android:maxLevel="100"></item>  
  11. </level-list>  


MainAcitivity.java文件如下所示:

 

 

  1. package com.shen.levellist;  
  2.   
  3. import android.app.Activity;  
  4. import android.graphics.drawable.LevelListDrawable;  
  5. import android.os.Bundle;  
  6. import android.view.Menu;  
  7. import android.view.MenuItem;  
  8. import android.view.View;  
  9. import android.widget.EditText;  
  10. import android.widget.ImageView;  
  11.   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private ImageView levelImg;  
  15.     private EditText levelEt;  
  16.     @Override  
  17.     protected void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.activity_main);  
  20.         levelImg = (ImageView) findViewById(R.id.level_list_img);  
  21.         levelEt = (EditText) findViewById(R.id.level_et);  
  22.     }  
  23.   
  24.     @Override  
  25.     public boolean onCreateOptionsMenu(Menu menu) {  
  26.         // Inflate the menu; this adds items to the action bar if it is present.  
  27.         getMenuInflater().inflate(R.menu.main, menu);  
  28.         return true;  
  29.     }  
  30.   
  31.     @Override  
  32.     public boolean onOptionsItemSelected(MenuItem item) {  
  33.         // Handle action bar item clicks here. The action bar will  
  34.         // automatically handle clicks on the Home/Up button, so long  
  35.         // as you specify a parent activity in AndroidManifest.xml.  
  36.         int id = item.getItemId();  
  37.         if (id == R.id.action_settings) {  
  38.             return true;  
  39.         }  
  40.         return super.onOptionsItemSelected(item);  
  41.     }  
  42.       
  43.     public void change(View v)  
  44.     {  
  45.         int iLevel = 0;  
  46.         String level = levelEt.getText().toString();  
  47.         LevelListDrawable levelListDrawable = (LevelListDrawable) levelImg.getDrawable();  
  48.         try {  
  49.             iLevel = Integer.valueOf(level);  
  50.         } catch (Exception e) {  
  51.             iLevel = 0;  
  52.         }  
  53.         levelListDrawable.setLevel(iLevel);  
  54.     }  
  55. }  

项目中用到的图片资源:

 

项目源码下载

posted @ 2015-01-05 19:17  MMLoveMeMM  阅读(722)  评论(0编辑  收藏  举报