java.lang.Object | |||
↳ | android.graphics.drawable.Drawable | ||
↳ | android.graphics.drawable.DrawableContainer | ||
↳ | android.graphics.drawable.LevelListDrawable |
LevelListDrawable可以帮我们切换图层,Drawable不是有个setLevel方法吗。
stat_sys_battery_charge.xml
<?xml version="1.0" encoding="utf-8"?>
<level-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:maxLevel="14">
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="2000" android:drawable="@drawable/stat_sys_battery_charge_anim0" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim1" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim2" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim3" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim4" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim5" />
</animation-list>
</item>
<item android:maxLevel="29">
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="2000" android:drawable="@drawable/stat_sys_battery_charge_anim1" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim2" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim3" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim4" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim5" />
</animation-list>
</item>
<item android:maxLevel="49">
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="2000" android:drawable="@drawable/stat_sys_battery_charge_anim2" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim3" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim4" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim5" />
</animation-list>
</item>
<item android:maxLevel="69">
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="2000" android:drawable="@drawable/stat_sys_battery_charge_anim3" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim4" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim5" />
</animation-list>
</item>
<item android:maxLevel="89">
<animation-list android:oneshot="false"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:duration="2000" android:drawable="@drawable/stat_sys_battery_charge_anim4" />
<item android:duration="1000" android:drawable="@drawable/stat_sys_battery_charge_anim5" />
</animation-list>
</item>
<item android:drawable="@drawable/stat_sys_battery_charge_anim5" android:maxLevel="101" />
</level-list>
stat_sys_battery.xml
<?xml version="1.0" encoding="utf-8"?>
<level-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/stat_sys_battery_0" android:maxLevel="4" />
<item android:drawable="@drawable/stat_sys_battery_10" android:maxLevel="14" />
<item android:drawable="@drawable/stat_sys_battery_20" android:maxLevel="29" />
<item android:drawable="@drawable/stat_sys_battery_40" android:maxLevel="49" />
<item android:drawable="@drawable/stat_sys_battery_60" android:maxLevel="69" />
<item android:drawable="@drawable/stat_sys_battery_80" android:maxLevel="89" />
<item android:drawable="@drawable/stat_sys_battery_100" android:maxLevel="100" />
</level-list>
/**
* 监听电池的电量变化
*
*/
private class BatteryInfoBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
int status = intent.getIntExtra("status", 0);
int level = intent.getIntExtra("level", 0);
switch (status) {
case BatteryManager.BATTERY_STATUS_UNKNOWN:
play_iv_battery
.setImageResource(R.drawable.stat_sys_battery_unknown);
break;
case BatteryManager.BATTERY_STATUS_CHARGING:
play_iv_battery
.setImageResource(R.drawable.stat_sys_battery_charge);
play_iv_battery.getDrawable().setLevel(level);
break;
case BatteryManager.BATTERY_STATUS_DISCHARGING:
play_iv_battery.setImageResource(R.drawable.stat_sys_battery);
play_iv_battery.getDrawable().setLevel(level);
break;
case BatteryManager.BATTERY_STATUS_NOT_CHARGING:
play_iv_battery.setImageResource(R.drawable.stat_sys_battery);
play_iv_battery.getDrawable().setLevel(level);
break;
case BatteryManager.BATTERY_STATUS_FULL:
play_iv_battery
.setImageResource(R.drawable.stat_sys_battery_100);
break;
}
}
}
/**
* 时间改变接收器(分钟)
*/
private class TimeChangeRecever extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate);
play_tv_cur_time.setText(str);
}
}
private BatteryInfoBroadcastReceiver batteryInfo;
private TimeChangeRecever timeChangeRecever;
private DateFormat formatter = new SimpleDateFormat("HH:mm");
private ImageView play_iv_battery;
private TextView play_tv_cur_time;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.levelist_drawable_activity);
play_iv_battery = (ImageView) findViewById(R.id.play_iv_battery);
play_tv_cur_time = (TextView) findViewById(R.id.play_tv_cur_time);
IntentFilter mIntentFilter = new IntentFilter();
mIntentFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
batteryInfo = new BatteryInfoBroadcastReceiver();
registerReceiver(batteryInfo, mIntentFilter);
timeChangeRecever = new TimeChangeRecever();
IntentFilter timeChangeFilter = new IntentFilter();
timeChangeFilter.addAction(Intent.ACTION_TIME_TICK); //以分钟为单位,每分钟会发一次广播
registerReceiver(timeChangeRecever, timeChangeFilter);
}
@Override
protected void onDestroy() {
unregisterReceiver(batteryInfo);
unregisterReceiver(timeChangeRecever);
super.onDestroy();
}