Android drawable resource learning (6) and levellistdrawable

Source: Internet
Author: User

A levelistdrawable manages a set of alternate drawable resources. Each drawable resource in levelistdrawable is combined with a maximum value, which is an item of levellistdrawable resources.

You can call the setlevel () method of drawable to load a level-list or a drawable resource defined in the Code and determine how to load an item: Android of an item in level-list: if the maxlevel value is greater than or equal to the value set by setlevel, it is loaded.

File Location:
res/drawable/filename.xml
File name as resource ID
Compilation Resource Type:
Point to levellistdrawablePointer
Resource reference:
In Java: R.drawable.filename
In XML: @[package:]drawable/filename
Syntax:
<?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>
Element:
<level-list>
Must be used as the root node. Contains multiple <item> nodes.

Attribute:

xmlns:android
StringType. Required. The namespace that defines the XML file, which must be "http://schemas.android.com/apk/res/android".
<item>
Define the drawable resource used by a Level

Attribute:

android:drawable
Drawable resource.Required. Reference A drawable Resource
android:maxLevel
IntegerType. The maximum level allowed by this item.
android:minLevel
IntegerType. The minimum level allowed by this item.
Example:
<?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>

When applied to a view, you can use the setlevel () or setimagelevel () method to change the level. Setlevel () valley levellistdrawable method, setimagelevel is some view Method

Refer:
  • LevelListDrawable

The following describes how to use an instance:

1. Define in XML file

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>

In the layout file main. xml:

<Imageview
Android: Id = "@ + ID/imgview"
Android: src = "@ drawable/level_list"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"/>

2. Use the following code:

Because the image I used is too large, it may cause memory leakage, so I did some processing.

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 C Lass 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) findviewbyid (R. id. imgview); bitmapfactory. options opts = N EW bitmapfactory. options (); opts. injustdecodebounds = true; bitmapfactory. decoderesource (getresources (), R. drawable. image1, opts); opts. insamplesize = computesamplesize (OPTs,-1,500*500); opts. injustdecodebounds = false; levellistdrawable = new levellistdrawable (); // defines a leveldrawable try {for (INT I = 0; I <IDs. length; I ++) {// For Loop, load 5 drawable resources bitmap BMP = bitmapfactory. Decoderesource (getresources (), IDS [I], opts); bitmapdrawable = new bitmapdrawable (BMP); levellistdrawable. addlevel (I, I + 1, bitmapdrawable); // Add to levellistdrawable} imageview. setimagedrawable (levellistdrawable); // set} catch (outofmemoryerror ERR) {err. printstacktrace ();} imageview. setimagelevel (1); // The default level is 0. Set it to 1 button BTN = (button) findviewbyid (R. id. BTN); BTN. setonclicklisten Er (new view. onclicklistener () {public void onclick (view v) {int I = imageview. getdrawable (). getlevel (); if (I> = 5) I = 0; // imageview. setimagelevel (++ I); // change level imageview. getdrawable (). setlevel (++ I); // achieves the same effect}) ;}@ override public Boolean oncreateoptionsmenu (menu) {getmenuinflater (). inflate (R. menu. activity_main, menu); Return true;} public static int computesamplesize (bitmapfactory. Options options, int minsidelength, int maxnumofpixels) {int initialsize = values (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 op Tions, 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 ;}}}

Note the following when using leveldrawable:

1. The default level is 0. If no level matches 0, it is not displayed.

2. maxlevel is preferred for level matching. That is, if there is an item, Min: 1, Max: 2. Another item, Min: 2, Max: 3.

If level = 2 is set at this time, the first item is matched.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.