Android 绘制电池电量图标在视频播放等全屏情况下有用到。
先在布局文件中写一个ImageView
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="/apk/res/android"xmlns:tools="/tools"android:id="@+id/activity_main2"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"android:background="@color/colorAccent"tools:context="com.example.mybluetooth.Main2Activity"><!--电池电量 --><ImageView android:id="@+id/image_battery"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:layout_marginLeft="300dp"android:src="@drawable/battery"/></RelativeLayout>
准备好两张电池电量图片(满和空),在drawable中定义battery.xml。
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="/apk/res/android" ><!-- 空的电池 --><item><bitmap android:src="@drawable/battery_empty" /></item><!-- 裁剪满的电池图标 --><item android:id="@+id/clip_drawable"><clip android:clipOrientation="horizontal"android:drawable="@drawable/battery_full"android:gravity="left" /></item></layer-list>
在Activity中实现。
public class Main2Activity extends AppCompatActivity {private ClipDrawable clipDrawable;private int currentPower = 0;private boolean isCharging = false;private Handler handler = new Handler();private boolean canAnimater;private ImageView imageBattery;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);imageBattery = (ImageView) findViewById(R.id.image_battery);LayerDrawable layerDrawable = (LayerDrawable) imageBattery.getDrawable();clipDrawable = (ClipDrawable) layerDrawable.findDrawableByLayerId(R.id.clip_drawable);}@Overrideprotected void onResume() {super.onResume();canAnimater =true;handler.removeCallbacks(chargingRunnable);handler.post(chargingRunnable);IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);intentFilter.addAction(Intent.ACTION_POWER_CONNECTED);intentFilter.addAction(Intent.ACTION_POWER_DISCONNECTED);registerReceiver(batteryChangedReceiver, intentFilter);}@Overrideprotected void onPause() {super.onPause();canAnimater=false;unregisterReceiver(batteryChangedReceiver);handler.removeCallbacks(chargingRunnable);}// 电量变化广播// TODO 假设进入Activity之前就已经在充电,进入之后无法收到充电的广播private BroadcastReceiver batteryChangedReceiver = new BroadcastReceiver() {public void onReceive(Context context, Intent intent) {if (Intent.ACTION_BATTERY_CHANGED.equals(intent.getAction())) {int level = intent.getIntExtra("level", 0);int scale = intent.getIntExtra("scale", 100);int power = level * 100 / scale;currentPower = power;clipDrawable.setLevel(calculateLevel(power));} else {int status = intent.getIntExtra("status", currentPower);if (Intent.ACTION_POWER_CONNECTED.equals(intent.getAction())) {isCharging = true;} else if (Intent.ACTION_POWER_DISCONNECTED.equals(intent.getAction())) {isCharging = false;}Toast.makeText(Main2Activity.this, "充电:" + isCharging + "," + status, Toast.LENGTH_SHORT).show();if (isCharging) {handler.removeCallbacks(chargingRunnable);handler.post(chargingRunnable);}//在自定义充电状态的图片时在这里//else {//imageBattery.setImageResource(R.drawable.battery_full);//}}}};private int calculateLevel(int progress) {int leftOffest = dip2px(this, 2);int powerLength = dip2px(this, 26.5f);// 40 px in hdpiint totalLength = dip2px(this, 32.5f);// 49 px in hdpiint level = (leftOffest + powerLength * progress / 100) * 10000 / totalLength;return level;}private int dip2px(Context context, float v) {final float scale = context.getResources().getDisplayMetrics().density;return (int) (v * scale + 0.5f);}private Runnable chargingRunnable = new Runnable() {@Overridepublic void run() {Log.e("test", "isCharging:" + isCharging + ",canAnimater:" + canAnimater);if (!isCharging || !canAnimater) {return;}// imageBattery.setImageResource(R.drawable.battery_empty); 充电状态图片ValueAnimator animator = ValueAnimator.ofInt(100, currentPower, 100);animator.setDuration(1500);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {int animatedValue = (Integer) animation.getAnimatedValue();clipDrawable.setLevel(calculateLevel(animatedValue));}});animator.addListener(new AnimatorListenerAdapter() {@Overridepublic void onAnimationEnd(Animator animation) {if (isCharging && canAnimater) {handler.postDelayed(chargingRunnable, 600);} else {clipDrawable.setLevel(calculateLevel(currentPower));}}});animator.start();}};}