前言

LevelListDrawable是通过改变层级值来显示对应的图片,除了下面的开关灯,还有WiFi的状态显示,电池状态的显示也可以用这种。

在公司好像没发现有人使用过LevelListDrawable(或者我看代码太少了哈)。

自己懒得写了,摘抄一些网友写的,以便自己学习。

以下内容都是摘抄的。

LevelListDrawable的语法简介

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <level-list
  3. xmlns:android="http://schemas.android.com/apk/res/android" >
  4. <item
  5. android:drawable="@drawable/drawable_resource"
  6. android:maxLevel="integer"
  7. android:minLevel="integer" />
  8. </level-list>
复制

这必须是根元素。包含一个或多个<item>元素。

属性:

xmlns:android

字符串。必备。定义 XML 命名空间,其必须是 “http://schemas.android.com/apk/res/android“。

<item>

定义要在某特定级别使用的可绘制对象。

属性:

android:drawable

可绘制对象资源。必备。引用要插入的可绘制对象资源。

android:maxLevel

整型。此项目允许的最高级别。

android:minLevel

整型。此项目允许的最低级别。

LevelListDrawable的例子

一:在drawablw文件夹下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <level-list xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/lamp_on" //灯亮的图片
  4. android:minLevel="12"
  5. android:maxLevel="20"/>
  6.  
  7. <item android:drawable="@drawable/lamp_off" //灯灭的图片
  8. android:minLevel="6"
  9. android:maxLevel="10"/>
  10. </level-list>
复制

二:布局文件

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:orientation="vertical"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent">
  6.  
  7. <ImageView
  8. android:src="@drawable/bitmap"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:id="@+id/iv_drawable"/>
  12.  
  13. <Button
  14. android:id="@+id/btn_on"
  15. android:text="light on"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"/>
  18.  
  19. <Button
  20. android:id="@+id/btn_off"
  21. android:text="light off"
  22. android:layout_width="wrap_content"
  23. android:layout_height="wrap_content"/>
  24. </LinearLayout>
复制

三:MainActivity

  1. package com.example.kirito.myapplication.testdrawable;
  2.  
  3. import android.os.Bundle;
  4. import android.support.annotation.Nullable;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.view.View;
  7. import android.widget.Button;
  8. import android.widget.ImageView;
  9.  
  10. import com.example.kirito.myapplication.R;
  11.  
  12. /**
  13. * Created by kirito on 2016.10.31.
  14. */
  15.  
  16. public class TestDrawable extends AppCompatActivity implements View.OnClickListener{
  17. private Button on,off;
  18. private ImageView iv;
  19.  
  20. @Override
  21. protected void onCreate(@Nullable Bundle savedInstanceState) {
  22. super.onCreate(savedInstanceState);
  23. setContentView(R.layout.testdrawable);
  24.  
  25. on = (Button) findViewById(R.id.btn_on);
  26. off = (Button) findViewById(R.id.btn_off);
  27. on.setOnClickListener(this);
  28. off.setOnClickListener(this);
  29. iv = (ImageView) findViewById(R.id.iv_drawable);
  30. iv.setImageLevel(8);
  31. }
  32.  
  33. @Override
  34. public void onClick(View v) {
  35. if (v.getId() == R.id.btn_off){
  36. //设置的level值必须在6-10之间
  37. iv.setImageLevel(8);
  38. }else if (v.getId() == R.id.btn_on){
  39. //设置的level值必须在12-20之间
  40. iv.setImageLevel(18);
  41. }
  42. }
  43. }
复制

来源

  1. android levellistdrawable 基本使用
  2. LevelListDrawable

相关文章

暂无评论

none
暂无评论...