概述
根据客户需求,更改Android电池图标为横向显示。因为Android的电池图标是根据代码画出的,直接更改源码较为繁琐。
所以我的思路是自己通过代码画出横向电池图标,替换原有电池图标。最后效果如图:
屏蔽原有电池图标/添加新图标
vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/BatteryMeterView.java
import com.android.systemui.util.Utils.DisableStateTracker;
-
+import com.android.systemui.BatteryView;
import java.io.FileDescriptor;
import java.io.PrintWriter;
import java.lang.annotation.Retention;
@@ -93,6 +93,8 @@ public class BatteryMeterView extends LinearLayout implements
private final ImageView mBatteryIconView;
private final CurrentUserTracker mUserTracker;
private TextView mBatteryPercentView;
+ private BatteryView mCrossBatteryView;
+ private ImageView mElectricityView;
private BatteryController mBatteryController;
private SettingObserver mSettingObserver;
@@ -160,6 +162,23 @@ public class BatteryMeterView extends LinearLayout implements
getResources().getDimensionPixelOffset(R.dimen.battery_margin_bottom));
addView(mBatteryIconView, mlp);
+ //添加电池图标
+ mBatteryIconView.setVisibility(View.GONE);
+ mCrossBatteryView = (BatteryView) LayoutInflater.from(getContext())
+ .inflate(R.layout.cross_battery_view, null);
+ addView(mCrossBatteryView,new ViewGroup.LayoutParams(
+ 44,
+ 22));
+ //添加电池充电图标
+ mElectricityView = (ImageView) LayoutInflater.from(getContext())
+ .inflate(R.layout.cross_battery_electricity_view, null);
+ addView(mElectricityView,new ViewGroup.LayoutParams(
+ 20,
+ 22));
+
updateShowPercent();
mDualToneHandler = new DualToneHandler(context);
// Init to not dark at all.
@@ -304,6 +323,9 @@ public class BatteryMeterView extends LinearLayout implements
getContext(), newValue);
setVisibility(icons.contains(mSlotBattery) ? View.GONE : View.VISIBLE);
}
+ //屏蔽原有电池图标
+ mBatteryIconView.setVisibility(View.GONE);
+
}
@Override
@@ -347,6 +369,16 @@ public class BatteryMeterView extends LinearLayout implements
mCharging = pluggedIn && charging;
mLevel = level;
updatePercentText();
+
+ mCrossBatteryView.setProgress(mLevel);//设置电池图标内电量值
+ //设置充电图标
+ if (charging && (level != 100)) {
+ mElectricityView.setVisibility(View.VISIBLE);
+ } else {
+ mElectricityView.setVisibility(View.GONE);
+ }
+
}
@Override
@@ -426,6 +458,10 @@ public class BatteryMeterView extends LinearLayout implements
|| mShowPercentMode == MODE_ON || mShowPercentMode == MODE_ESTIMATE) {
if (!showing) {
mBatteryPercentView = loadPercentView();
+ //屏蔽原有电池图标显示
+ mBatteryPercentView.setVisibility(View.GONE);
+
if (mPercentageStyleId != 0) { // Only set if specified as attribute
mBatteryPercentView.setTextAppearance(mPercentageStyleId);
}
@@ -489,6 +525,15 @@ public class BatteryMeterView extends LinearLayout implements
if (mBatteryPercentView != null) {
mBatteryPercentView.setTextColor(singleToneColor);
}
+ //设置电池图标颜色
+ if (mCrossBatteryView != null) {
+ mCrossBatteryView.setColor(singleToneColor);
+ }
+ //设置电池充电图标颜色
+ if (mElectricityView != null) {
+ mElectricityView.setColorFilter(singleToneColor);
+ }
+ //ssy end
}
public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
自定义电池图标
vendor/mediatek/proprietary/packages/apps/SystemUI/src/com/android/systemui/BatteryView.java
参考如下https://blog.csdn.net/qq_16064871/article/details/107419332
package com.android.systemui;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.View;
import android.os.BatteryManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.BroadcastReceiver;
public class BatteryView extends View {
private float percent = 0f;
Paint paint = new Paint();
Paint paint1 = new Paint();
Paint paint2 = new Paint();
public BatteryView(Context context, AttributeSet set) {
super(context, set);
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.FILL);
paint.setColor(Color.GRAY);
paint1.setAntiAlias(true);
paint1.setStyle(Paint.Style.STROKE);
paint1.setStrokeWidth(dip2px(1.5f));
paint1.setColor(-1728053248);
paint2.setAntiAlias(true);
paint2.setStyle(Paint.Style.FILL);
paint2.setColor(-1728053248);
DisplayMetrics dm = getResources().getDisplayMetrics();
int mScreenWidth = dm.widthPixels;
int mScreenHeight = dm.heightPixels;
float ratioWidth = (float) mScreenWidth / 720;
float ratioHeight = (float) mScreenHeight / 1080;
float ratioMetrics = Math.min(ratioWidth, ratioHeight);
int textSize = Math.round(20 * ratioMetrics);
paint2.setTextSize(textSize);
}
private int dip2px(float dpValue) {
final float scale = getResources().getDisplayMetrics().density;
return (int) (dpValue * scale + 0.5f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int a = getWidth() - dip2px(2f);
int b = getHeight() - dip2px(1.5f);
float d = a * percent;
float left = dip2px(0.5f);
float top = dip2px(0.5f);
float right = dip2px(2.5f);
float bottom = dip2px(1.5f);
RectF re1 = new RectF(left, top, d - right, b + bottom);
RectF re2 = new RectF(0, 0, a - right, b + bottom);
RectF re3 = new RectF(a - right + 2, b / 5, a, b + bottom - b / 5);
canvas.drawRect(re1, paint);
canvas.drawRect(re2, paint1);
canvas.drawRect(re3, paint1);
canvas.drawText(String.valueOf((int) (percent * 100)), getWidth() / 4 - dip2px(3), getHeight() - getHeight() / 5, paint2);
}
public synchronized void setProgress(int percent) {
this.percent = (float) (percent / 100.0);
postInvalidate();
}
public void setColor(int color) {
paint1.setColor(color);
paint2.setColor(color);
postInvalidate();
}
}
vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/cross_battery_view.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License
-->
<!-- Loaded into BatteryMeterView as necessary -->
<com.android.systemui.BatteryView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cross_battery_view"
android:layout_width="30dp"
android:layout_height="10dp"
android:singleLine="true"
android:gravity="center_vertical|start"
/>
自定义充电图标
vendor/mediatek/proprietary/packages/apps/SystemUI/res/layout/cross_battery_electricity_view.xml
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (C) 2017 The Android Open Source Project
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License
-->
<!-- Loaded into BatteryMeterView as necessary -->
<ImageView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cross_battery_electricity_view"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="@drawable/ic_electricity"
/>
vendor/mediatek/proprietary/packages/apps/SystemUI/res/drawable/ic_electricity.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="16dp"
android:height="16dp"
android:viewportWidth="1024"
android:viewportHeight="1024">
<path
android:pathData="M788.48,410.11h-112.13c-24.06,0 -92.16,11.78 -115.2,-2.56 -4.61,-16.38 10.24,-46.08 14.34,-60.42 11.78,-41.47 23.04,-82.43 34.82,-124.42L660.48,41.98c6.66,-23.55 -24.06,-33.79 -36.86,-15.87L218.11,582.66c-9.22,12.8 3.58,30.72 17.92,30.72h164.35c11.26,0 54.78,-7.17 61.95,3.58 6.66,8.7 -6.14,35.84 -8.7,45.57 -30.72,106.5 -60.42,212.99 -90.11,319.49 -6.66,23.55 24.06,33.79 36.86,15.87l404.99,-556.54c9.73,-13.31 -2.56,-31.23 -16.9,-31.23z"
android:fillColor="#FAF6F6"/>
</vector>