文章目录

  • 1、简介
  • 2、自定义 BatteryView
  • 3、 xml 文件


1、简介

使用canvas 根据电池电量变化 绘制电池图标
1)充电 显示绿色
2)电量低于10%使用 红色
3)其余白色

Android  canvas 监听绘制电池电量_android

2、自定义 BatteryView
package recognizerdemo.speech.baidu.com.mybatteryshow;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.os.BatteryManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class BatteryView   extends View {
    private int mMargin = 5;    //电池内芯与边框的距离
    private int mBoder = 4;     //电池外框的宽带
    private int mWidth = 70;    //主体外框总长
    private int mHeight = 40;   //主体总高
    private int mHeadWidth = 8; //头部宽度
    private int mHeadHeight = 10; //头部高度

    private RectF mMainRect;   //主体区域方位
    private RectF mHeadRect;  //头部区域方位
    private float mRadius = 4f;   //圆角
    private float mPower;

    private boolean mIsCharging;    //是否在充电


    public BatteryView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    public BatteryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public BatteryView(Context context) {
        super(context);
        initView();
    }

    private void initView() {


        mMainRect = new RectF(mBoder, mBoder, mWidth , mHeight - mBoder); //绘制主体区域大小

        Log.i("lum","mMainRect: " + mMainRect);

        float left = mMainRect.width() + (mBoder*2)  ;
        float top = (mHeight - mHeadHeight)/2;
        float right = mMainRect.width() + (mBoder) + mHeadWidth;
        float bottom = (mHeight + mHeadHeight)/2;
        mHeadRect = new RectF(left, top, right, bottom);
        Log.i("lum","mHeadRect: " + mHeadRect);;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint1 = new Paint();
        //画外框
        paint1.setStyle(Paint.Style.STROKE);    //设置空心矩形
        paint1.setStrokeWidth(mBoder);          //设置边框宽度
        paint1.setColor(Color.WHITE);
        canvas.drawRoundRect(mMainRect, mRadius, mRadius, paint1);

        //画电池头
        paint1.setStyle(Paint.Style.FILL);  //实心
        paint1.setColor(Color.WHITE);
        canvas.drawRect(mHeadRect, paint1);


        //画电池芯
        Paint paint = new Paint();
        if (mIsCharging) {
            paint.setColor(Color.GREEN);
        } else {
            if (mPower < 0.1) {
                paint.setColor(Color.RED);
            } else {
                paint.setColor(Color.WHITE);
            }
        }

        int width   = (int) (mPower * (mMainRect.width() - mMargin*2));
        int left    = (int) (mMainRect.left + mMargin );
        int right   = (int) (mMainRect.left + mMargin +  width);
        int top     = (int) (mMainRect.top + mMargin);
        int bottom  = (int) (mMainRect.bottom - mMargin);
        Rect rect = new Rect(left,top,right, bottom);
        canvas.drawRect(rect, paint);
    }

/*    @Override  //设置 view 大小
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //setMeasuredDimension(mWidth, mHeight);   
    }*/

    private void setPower(float power) {
        mPower = power;
        invalidate();
    }

    private BroadcastReceiver mPowerConnectionReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            int status = intent.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
            mIsCharging = status == BatteryManager.BATTERY_STATUS_CHARGING ||
                    status == BatteryManager.BATTERY_STATUS_FULL;

            int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

            setPower(((float) level)/scale);
        }
    };

    @Override
    protected void onAttachedToWindow() {  //自定义 view 里 添加广播监听
        getContext().registerReceiver(mPowerConnectionReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {   //自定义view 里注销广播监听
        getContext().unregisterReceiver(mPowerConnectionReceiver);
        super.onDetachedFromWindow();
    }
}
3、 xml 文件
<....BatteryView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />