Android10 状态栏蓝牙电量图标

Android10 源码状态栏蓝牙电量图标相关类

BatteryMeterDrawableBase:电量图标基类

BluetoothDeviceLayerDrawable: LayerDrawable 包含蓝牙设备图标和电池电量图标

BatteryMeterDrawable:内部类,继承自BatteryMeterDrawableBase

BluetoothDeviceLayerDrawableState:内部类,继承自ConstantState

电量图标

电量图标由BatteryMeterDrawableBase绘制,虽然BluetoothDeviceLayerDrawable的内部类BatteryMeterDrawable继承自BatteryMeterDrawableBase,但并没有重写太多东西

java 复制代码
    public BatteryMeterDrawableBase(Context context, int frameColor) {
        mContext = context;
        final Resources res = context.getResources();
        TypedArray levels = res.obtainTypedArray(R.array.batterymeter_color_levels); //15、100
        TypedArray colors = res.obtainTypedArray(R.array.batterymeter_color_values); //红色、白色

        final int N = levels.length();
        mColors = new int[2 * N];
        for (int i = 0; i < N; i++) {
            mColors[2 * i] = levels.getInt(i, 0);
            if (colors.getType(i) == TypedValue.TYPE_ATTRIBUTE) {
                mColors[2 * i + 1] = Utils.getColorAttrDefaultColor(context,
                        colors.getThemeAttributeId(i, 0));
            } else {
                mColors[2 * i + 1] = colors.getColor(i, 0);
            }
        }
        levels.recycle();  //回收资源
        colors.recycle();

        mWarningString = context.getString(R.string.battery_meter_very_low_overlay_symbol);
        mCriticalLevel = mContext.getResources().getInteger(
                com.android.internal.R.integer.config_criticalBatteryWarningLevel); //=5:临界电量值
        mButtonHeightFraction = context.getResources().getFraction(
                R.fraction.battery_button_height_fraction, 1, 1); //高度百分比
        mSubpixelSmoothingLeft = context.getResources().getFraction( //不知道是什么,也没被调用
                R.fraction.battery_subpixel_smoothing_left, 1, 1);
        mSubpixelSmoothingRight = context.getResources().getFraction( //不知道是什么,也没被调用
                R.fraction.battery_subpixel_smoothing_right, 1, 1);
        ......
    }

想要修改什么,修改对应的资源值就好。

整合

由BluetoothDeviceLayerDrawable将蓝牙图标放在左边,电量图标放在右边。

BluetoothDeviceLayerDrawable的构造函数是私有的,通过createLayerDrawable方法获取实例对象。

java 复制代码
    public static BluetoothDeviceLayerDrawable createLayerDrawable(Context context, int resId,
            int batteryLevel, float iconScale) {
        final Drawable deviceDrawable = context.getDrawable(resId);

        final BatteryMeterDrawable batteryDrawable = new BatteryMeterDrawable(context,
                context.getColor(R.color.meter_background_color), batteryLevel);
        final int pad = context.getResources().getDimensionPixelSize(R.dimen.bt_battery_padding);
        batteryDrawable.setPadding(pad, pad, pad, pad);
        //创建
        final BluetoothDeviceLayerDrawable drawable = new BluetoothDeviceLayerDrawable(
                new Drawable[]{deviceDrawable, batteryDrawable});  //图层
        // Set the bluetooth icon at the left
        drawable.setLayerGravity(0 /* index of deviceDrawable */, Gravity.START); //蓝牙图标
        // Set battery icon to the right of the bluetooth icon
        //下面两句确认电量图标绘制的其实像素点(x,y)=(deviceDrawable.getIntrinsicWidth(),deviceDrawable.getIntrinsicHeight() * (1 - iconScale))
        drawable.setLayerInsetStart(1 /* index of batteryDrawable */,
                deviceDrawable.getIntrinsicWidth());  //电量图标
        drawable.setLayerInsetTop(1 /* index of batteryDrawable */,
                (int) (deviceDrawable.getIntrinsicHeight() * (1 - iconScale)));

        drawable.setConstantState(context, resId, batteryLevel, iconScale);

        return drawable;
    }
相关推荐
码农明明20 分钟前
Google Play 应用上架二三事
android·google
红橙Darren3 小时前
手写操作系统 - 环境搭建
android·微信·操作系统
_一条咸鱼_3 小时前
Android Runtime直接内存管理原理深度剖析(73)
android·面试·android jetpack
你听得到113 小时前
揭秘Flutter图片编辑器核心技术:从状态驱动架构到高保真图像处理
android·前端·flutter
wilinz3 小时前
Flutter Android 端接入百度地图踩坑记录
android·flutter
小袁拒绝摆烂6 小时前
SQL开窗函数
android·sql·性能优化
apihz7 小时前
VM虚拟机全版本网盘+免费本地网络穿透端口映射实时同步动态家庭IP教程
android·服务器·开发语言·网络·数据库·网络协议·tcp/ip
baidu_247438617 小时前
Android MPAndroidChart使用
android
天平8 小时前
react native现代化组件库的推荐 【持续更新...】
android·前端·react native
apihz9 小时前
通用图片搜索-搜狗源免费API接口使用指南
android·java·python·php·音视频