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>
- 元素:
- 例子:
<?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。
- Android Drawable Resource学习(六)、LevelListDrawable
- Android Drawable Resource学习(六)、LevelListDrawable
- Android Drawable Resource学习(六)、LevelListDrawable
- Android Drawable Resource学习(六)、LevelListDrawable
- Android Drawable Resource学习(六)、LevelListDrawable
- Android Graphics.drawable之—LevelListDrawable学习
- Drawable学习之----LevelListDrawable
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(一)、Drawable Resource简介
- Android Drawable Resource学习(四)、LayerDrawable
- Android Drawable Resource学习(五)、StateListDrawable
- Android Drawable Resource学习(七)、TransitionDrawable
- Android Drawable Resource学习(八)、InsetDrawable
- 【phpcms-v9】phpcms-v9数据源调用的控制器文件分析phpcms/modules/dbsource/call.php
- 历史上留下浓重一笔的黑客们
- EGLHelper
- This FTP server is anonymous only 解决方案
- Java基本类型与byte数组之间相互转换
- Android Drawable Resource学习(六)、LevelListDrawable
- Linux-2.6.32.2内核在mini2440上的移植(一)---构建自主开发环境
- 样片申请公司
- 检测单链表是否是回文
- Android 网络访问框架 NiceDay 发布
- C#使用LitJson解析JSON
- Linux-2.6.32.2内核在mini2440上的移植(二)---yaffs2文件系统移植
- Linux-2.6.32.2内核在mini2440上的移植(三)---DM9000网卡驱动移植
- 字号对应尺寸磅