Android LevelList使用实例
来源:互联网 发布:当涂到马鞍山网络大学 编辑:程序博客网 时间:2024/06/09 19:44
大家对android系统电池状态改变的显示已经很熟悉了,但它是如何实现的呢?它是利用了什么技术呢?也许你有你自己的实现方式,但android系统是利用LevelList来实现的。你知道么?
下面通过一个具体的实例来说明一下:
运行示意图:1.为初始化的界面;2.为输入25时的界面;3.为输入45时的界面;4.为输入65时的界面;5.为输入0时的界面,6.为输入100时的图片,7为输入105时的图片
新建一个名称为LevelList的Adnroid项目工程
编写界面文件activity_main.xml
<RelativeLayout 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"> <ImageView android:id="@+id/level_list_img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/levellist" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" android:contentDescription="@string/app_name" /> <EditText android:id="@+id/level_et" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerVertical="true" android:inputType="numberSigned" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="change" android:text="@string/button_text" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="20dp"/></RelativeLayout>
在ImageView中使用了LevelList图形资源,该图形资源使用了LevelList的技术,levellist.xml位于drawable文件夹下。
Levellist.xml文件如下所示:
<?xml version="1.0" encoding="utf-8"?><level-list xmlns:android="http://schemas.android.com/apk/res/android" > <!-- 0到 20显示这个图片--><item android:drawable="@drawable/s1" android:minLevel="0" android:maxLevel="20"></item> <!-- 21到 40显示这个图片--><item android:drawable="@drawable/s2" android:minLevel="21" android:maxLevel="40"></item><!-- 41到 60显示这个图片--><item android:drawable="@drawable/s3" android:minLevel="41" android:maxLevel="60"></item><!-- 61到100显示这个图片--><item android:drawable="@drawable/s4" android:minLevel="61" android:maxLevel="100"></item></level-list>
MainAcitivity.java文件如下所示:
package com.shen.levellist;import android.app.Activity;import android.graphics.drawable.LevelListDrawable;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.EditText;import android.widget.ImageView;public class MainActivity extends Activity {private ImageView levelImg;private EditText levelEt;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);levelImg = (ImageView) findViewById(R.id.level_list_img);levelEt = (EditText) findViewById(R.id.level_et);}@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;}@Overridepublic boolean onOptionsItemSelected(MenuItem item) {// Handle action bar item clicks here. The action bar will// automatically handle clicks on the Home/Up button, so long// as you specify a parent activity in AndroidManifest.xml.int id = item.getItemId();if (id == R.id.action_settings) {return true;}return super.onOptionsItemSelected(item);}public void change(View v){int iLevel = 0;String level = levelEt.getText().toString();LevelListDrawable levelListDrawable = (LevelListDrawable) levelImg.getDrawable();try {iLevel = Integer.valueOf(level);} catch (Exception e) {iLevel = 0;}levelListDrawable.setLevel(iLevel);}}项目中用到的图片资源:
项目源码下载
1 0
- Android LevelList使用实例
- Drawable基础知识总结----drawable标签LevelList、inset、scale、clip的使用
- android skia 使用实例
- Android NDK使用实例
- Android 使用Matrix实例
- [Android] SurfaceView使用实例
- android使用wifi实例
- Android Service 使用实例
- android skia 使用实例
- Android传感器使用实例
- Android ExpandableListView 使用实例
- [Android] SurfaceView使用实例
- ANDROID-----------------AsyncTask使用实例
- Android使用sqlite 实例
- Android:WebService使用实例
- Android AIDL使用实例
- android startActivityForResult 使用实例
- [Android] SurfaceView使用实例
- codevs1003电话连线
- HDU_5147 Sequence II [逆序数]
- android 开源框架Universal-Image-Loader完全解析(三)---源代码解读
- 测试用例规范v1.0
- 为什么绝大多数创业公司活不过5年?
- Android LevelList使用实例
- Android4.0新增的 网格布局:计算器
- 最近的一次敏捷项目Scrum经验总结
- Fedora下搭建gtk开发环境(2014-12-20)
- 10316 - Airline Hub
- xcode Analyze进行代码静态检查
- java学习day23
- 【树形DP】 HDOJ 5148 Cities
- mac OS X日常操作