frameworks/base/packages/SystemUI/src/com/android/systemui/BatteryMeterView.java 显示电池图标变化状态,setImageDrawable(mDrawable) 就是设置的电池图标。
public BatteryMeterView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setOrientation(LinearLayout.HORIZONTAL);
setGravity(Gravity.CENTER_VERTICAL | Gravity.START);
TypedArray atts = context.obtainStyledAttributes(attrs, R.styleable.BatteryMeterView,
defStyle, 0);
final int frameColor = atts.getColor(R.styleable.BatteryMeterView_frameColor,
context.getColor(R.color.meter_background_color));
mDrawable = new BatteryMeterDrawableBase(context, frameColor);
atts.recycle();
mSettingObserver = new SettingObserver(new Handler(context.getMainLooper()));
mSlotBattery = context.getString(
com.android.internal.R.string.status_bar_battery);
mBatteryIconView = new ImageView(context);
//设置图标,矢量图
mBatteryIconView.setImageDrawable(mDrawable);
.......
}
};
}
监听电池变化,当电池发生变化时,实现 BatteryController.BatteryStateChangeCallback
@Override
//电池状态的变化,充放电,电量
public void onBatteryLevelChanged(int level, boolean pluggedIn, boolean charging) {
//显示电量等级
mDrawable.setBatteryLevel(level);
//是否充电状态,充电状态图标会有闪电的标记
mDrawable.setCharging(pluggedIn);
mLevel = level;
updatePercentText();
setContentDescription(
getContext().getString(charging ? R.string.accessibility_battery_level_charging
: R.string.accessibility_battery_level, level));
}