一个LeveListDrawable管理着一组交替的drawable资源。LeveListDrawable里面的每一个drawable资源与一个最大数值结合起来,作为LevelListDrawable资源的一项。
调用Drawable的setLevel()方法可以加载level-list或代码中定义的某个drawable资源,判断加载某项的方式:level-list中某项的android:maxLevel数值大于或者等于setLevel设置的数值,就会被加载。
-
文件位置:
-
res/drawable/filename.xml
文件名作为资源ID
编译资源类型:
-
指向LevelListDrawable
的指针
资源引用:
-
In Java:
R.drawable.filename
In XML:@[package:]drawable/filename
语法:
-
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">level-list</a><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="tag">></span><span class="pln"> </span><span class="tag"><</span><a target=_blank style="color: rgb(202, 0, 0);">item</a><span class="pln"> </span><span class="atn">android:drawable</span><span class="pun">=</span><span class="atv">"@drawable/</span><em>drawable_resource</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:maxLevel</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="atn">android:minLevel</span><span class="pun">=</span><span class="atv">"</span><em>integer</em><span class="atv">"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"></level-list></span>
元素:
- 例子:
-
<span class="pun"><?</span><span class="pln">xml version</span><span class="pun">=</span><span class="str">"1.0"</span><span class="pln"> encoding</span><span class="pun">=</span><span class="str">"utf-8"</span><span class="pun">?></span><span class="pln"> </span><span class="tag"><level-list</span><span class="pln"> </span><span class="atn">xmlns:android</span><span class="pun">=</span><span class="atv">"http://schemas.android.com/apk/res/android"</span><span class="pln"> </span><span class="tag">></span><span class="pln"> </span><span class="tag"><item</span><span class="pln"> </span><span class="atn">android:drawable</span><span class="pun">=</span><span class="atv">"@drawable/status_off"</span><span class="pln"> </span><span class="atn">android:maxLevel</span><span class="pun">=</span><span class="atv">"0"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"><item</span><span class="pln"> </span><span class="atn">android:drawable</span><span class="pun">=</span><span class="atv">"@drawable/status_on"</span><span class="pln"> </span><span class="atn">android:maxLevel</span><span class="pun">=</span><span class="atv">"1"</span><span class="pln"> </span><span class="tag">/></span><span class="pln"> </span><span class="tag"></level-list></span>
当应用于一个View的时后,可以使用setLevel()或者setImageLevel()方法来改变level。setLevel()山谷LevelListDrawable的方法,setImageLevel是某些View的方法
参考:
下面看下具体的使用实例:
1、在XML文件中定义
level_list.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <level-list xmlns:android="http://schemas.android.com/apk/res/android">
- <item android:maxLevel="1" android:drawable="@drawable/image1" />
- <item android:maxLevel="2" android:drawable="@drawable/image2" />
- <item android:maxLevel="3" android:drawable="@drawable/image3" />
- <item android:maxLevel="4" android:drawable="@drawable/image4" />
- <item android:maxLevel="5" android:drawable="@drawable/image5" />
- </level-list>
在布局文件main.xml中:
<ImageView
android:id="@+id/imgView"
android:src="@drawable/level_list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
2、在代码中使用:
因为我所使用的图片过大,会导致内存泄露,所以做了一些处理。
- package com.example.drawabletest;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.graphics.drawable.BitmapDrawable;
- import android.graphics.drawable.LevelListDrawable;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private int[] ids = new int[] { R.drawable.image1, R.drawable.image2, R.drawable.image3,
- R.drawable.image4, R.drawable.image5 };
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- final ImageView imageView = (ImageView) findViewById(R.id.imgView);
- BitmapFactory.Options opts = new BitmapFactory.Options();
- opts.inJustDecodeBounds = true;
- BitmapFactory.decodeResource(getResources(), R.drawable.image1, opts);
- opts.inSampleSize = computeSampleSize(opts, -1, 500 * 500);
- opts.inJustDecodeBounds = false;
- LevelListDrawable levelListDrawable = new LevelListDrawable();//定义一个LevelDrawable
- try {
- for (int i = 0; i < ids.length; i++) {//for循环,加载5个drawable资源
- Bitmap bmp = BitmapFactory.decodeResource(getResources(),ids[i], opts);
- BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
- levelListDrawable.addLevel(i, i+1, bitmapDrawable);//添加到LevelListDrawable
- }
- imageView.setImageDrawable(levelListDrawable);//设置
- } catch (OutOfMemoryError err) {
- err.printStackTrace();
- }
- imageView.setImageLevel(1);//默认的level为0,将到设置为1
- Button btn = (Button) findViewById(R.id.btn);
- btn.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- int i = imageView.getDrawable().getLevel();
- if (i >=5)
- i = 0;
- // imageView.setImageLevel(++i);//改变level
- imageView.getDrawable().setLevel(++i); //能达到同样的效果
- }
- });
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- getMenuInflater().inflate(R.menu.activity_main, menu);
- return true;
- }
- public static int computeSampleSize(BitmapFactory.Options options, int minSideLength,
- int maxNumOfPixels) {
- int initialSize = computeInitialSampleSize(options, minSideLength, maxNumOfPixels);
- int roundedSize;
- if (initialSize <= 8) {
- roundedSize = 1;
- while (roundedSize < initialSize) {
- roundedSize <<= 1;
- }
- } else {
- roundedSize = (initialSize + 7) / 8 * 8;
- }
- return roundedSize;
- }
- private static int computeInitialSampleSize(BitmapFactory.Options options, int minSideLength,
- int maxNumOfPixels) {
- double w = options.outWidth;
- double h = options.outHeight;
- int lowerBound = (maxNumOfPixels == -1) ? 1 : (int) Math.ceil(Math.sqrt(w * h
- / maxNumOfPixels));
- int upperBound = (minSideLength == -1) ? 128 : (int) Math.min(
- Math.floor(w / minSideLength), Math.floor(h / minSideLength));
- if (upperBound < lowerBound) {
- // return the larger one when there is no overlapping zone.
- return lowerBound;
- }
- if ((maxNumOfPixels == -1) && (minSideLength == -1)) {
- return 1;
- } else if (minSideLength == -1) {
- return lowerBound;
- } else {
- return upperBound;
- }
- }
- }
使用LevelDrawable注意几点:
1、默认的level为0,如果没有和0匹配的level,那么就不显示。
2、level匹配以maxLevel优先。即如果有个item,min:1,max:2。 另一份item,min:2,max:3。
如果此时设置level=2,那么会匹配第一个item。