Definition
A resource that manages a number of
alternate Drawables.
It’s inflated into the LevelListDrawable java object.
Each drawable is assigned a maximum
numerical level.
You can set the level value of the
object with setLevel(int) or an equivalent method.
This
will load the image/drawable whose maximum level attribute you have passed in
the parameter.
Examples:
Battery level indicator
icon, with different images to indicate the current battery level, as well as
network strength indicator icon, we shall use the latter for our example.
The XML
It has element as root.
Each Drawable level is defined in a
nested element.
This resource can be referenced as the background drawable for an ImageView.
The default image is the first in the list. It can then be prorammatically changed to one of
the other levels with setImageLevel(int).
Example
Create an android project called LevelList.
Create a drawable xml
resource like so:
network_level.xml
xml version="1.0"
encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="0"
android:drawable="@drawable/stat_sys_wifi_signal_0" />
<item android:maxLevel="1"
android:drawable="@drawable/stat_sys_wifi_signal_1" />
<item android:maxLevel="2"
android:drawable="@drawable/stat_sys_wifi_signal_2" />
<item android:maxLevel="3"
android:drawable="@drawable/stat_sys_wifi_signal_3" />
<item android:maxLevel="4"
android:drawable="@drawable/stat_sys_wifi_signal_4" />
</level-list>
I copied the drawables from the android 2.2 jar res\drawable-mdpi folder, they may be named
differently in the archive you have but should be having something like
**wifi_signal_** in the name, check accordingly.
main.xml
create an ImageView to use the new drawable resource as a
background and 5 buttons to switch the image level, like so:
xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Network level indicator" />
<ImageView
android:id="@+id/network_level"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/network_level" />
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:weightSum="5">
<Button
android:layout_height="40dp"
android:layout_width="wrap_content"
android:text="0"
android:id="@+id/networklevel0"
android:layout_weight="1"
android:onClick="onClick" />
<Button
android:layout_height="40dp"
android:layout_width="wrap_content"
android:id="@+id/networklevel1"
android:text="1"
android:layout_weight="1"
android:onClick="onClick" />
<Button
android:layout_height="40dp"
android:layout_width="wrap_content"
android:id="@+id/networklevel2"
android:text="2"
android:layout_weight="1"
android:onClick="onClick" />
<Button
android:layout_height="40dp"
android:layout_width="wrap_content"
android:id="@+id/networklevel3"
android:text="3"
android:layout_weight="1"
android:onClick="onClick" />
<Button
android:layout_height="40dp"
android:layout_width="wrap_content"
android:id="@+id/networklevel4"
android:text="4"
android:layout_weight="1"
android:onClick="onClick" />
</LinearLayout>
</LinearLayout>
The buttons must have id’s since we shall have to access them
programmatically. Notice how I have referenced an onClick from xml, there fore
we have to create such a method in the activity and pass to it a view like so:
package com.simplejavan.level;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class LevelListActivity extends Activity {
ImageView
networkLevel;
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
networkLevel=
(ImageView) findViewById(R.id.network_level);
}
public void
onClick(View v){
int idd=v.getId();
if(idd==R.id.networklevel0)
networkLevel.setImageLevel(0);
if(idd==R.id.networklevel1)
networkLevel.setImageLevel(1);
if(idd==R.id.networklevel2)
networkLevel.setImageLevel(2);
if(idd==R.id.networklevel3)
networkLevel.setImageLevel(3);
if(idd==R.id.networklevel4)
networkLevel.setImageLevel(4);
}
}
Load the app and switch network level from the buttons.
This is it for the basics of LevelListDrawable, I will be
adding any new ideas or new findings I get in subsequent blog posts. Thanks for
reading.
No comments:
Post a Comment