概述
Android 绘制电池电量图标在视频播放等全屏情况下有用到。
先在布局文件中写一个ImageView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/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="http://schemas.android.com/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;
@Override
protected 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);
}
@Override
protected 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);
}
@Override
protected 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 hdpi
int totalLength = dip2px(this, 32.5f);// 49 px in hdpi
int 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() {
@Override
public 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() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animatedValue = (Integer) animation.getAnimatedValue();
clipDrawable.setLevel(calculateLevel(animatedValue));
}
});
animator.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
if (isCharging && canAnimater) {
handler.postDelayed(chargingRunnable, 600);
} else {
clipDrawable.setLevel(calculateLevel(currentPower));
}
}
});
animator.start();
}
};
}
最后
以上就是贤惠犀牛为你收集整理的Android 绘制电池电量图标(含充电状态)。的全部内容,希望文章能够帮你解决Android 绘制电池电量图标(含充电状态)。所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复