Drawable学习之----LevelListDrawable
一个LevelListDrawable管理着一组交替的drawable资源。LevelListDrawable里面的每一个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
- 语法:
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/drawable_resource" android:maxLevel="integer" android:minLevel="integer" /> </level-list>
- 元素:
- 例子:
<?xml version="1.0" encoding="utf-8"?> <level-list xmlns:android="http://schemas.android.com/apk/res/android" > <item android:drawable="@drawable/status_off" android:maxLevel="0" /> <item android:drawable="@drawable/status_on" android:maxLevel="1" /> </level-list>
当应用于一个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。