Android Drawable Resource学习(六)、LevelListDrawable

来源:互联网 发布:flash player修复软件 编辑:程序博客网 时间:2024/03/29 00:23

一个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
语法:
<?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>
元素:
<level-list>
必须作为根节点。包含多个<item>节点。

属性:

xmlns:android
String类型。必须的。定义XML文件的命名空间,必须是 "http://schemas.android.com/apk/res/android".
<item>
定义某个level使用的drawable资源

属性:

android:drawable
Drawable 资源。必须的。引用一个Drawable资源
android:maxLevel
Integer类型。该项所允许的最大level。
android:minLevel
Integer类型。该项所允许的最小level。
例子:
<?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的方法

参考:
  • LevelListDrawable



下面看下具体的使用实例:

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。






原创粉丝点击