先看效果图:

 直接上代码 


public class BatteryView extends View {
    private int mMargin = 1;    //电池内芯与边框的距离
    private int mBorder = 1;     //电池外框的宽带
    private int mWidth = 0;    //总长
    private int mHeight = 0;   //总高
    private int mHeadWidth = 2;
    private int mHeadHeight = 3;

    private RectF mMainRect;
    private RectF mHeadRect;
    private float mRadius = 0f;   //圆角
    private float mPower;
    private Path path;
    private boolean mIsCharging;    //是否在充电
    private Paint paintOuter;
    private Paint paintInner;
    private Paint paintLight;


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

    public BatteryView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public BatteryView(Context context) {
        this(context,null);
    }

    private void initView() {
        paintOuter = new Paint();

        //画电池头
        paintOuter.setStyle(Paint.Style.STROKE);  //设置空心矩形
        paintOuter.setStrokeWidth(mBorder);          //设置边框宽度
        paintOuter.setColor(Color.WHITE);
        //画电池芯
        paintInner = new Paint();
        paintInner.setStyle(Paint.Style.FILL_AND_STROKE);    //设置空心矩形
        paintInner.setStrokeWidth(mBorder);          //设置边框宽度
        //充电
        paintLight = new Paint();
        paintLight.setColor(Color.WHITE);
        paintLight.setStyle(Paint.Style.FILL_AND_STROKE);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        mWidth = getWidth();
        mHeight = getHeight();
        mHeadRect = new RectF(1, (mHeight - mHeadHeight)/2, mHeadWidth, (mHeight + mHeadHeight)/2);

        float outLeft = mHeadRect.width()+ mBorder;
        float outTop = mBorder;
        float outRight = mWidth- mBorder;
        float outBottom = mHeight- mBorder;
        mMainRect = new RectF(outLeft, outTop, outRight, outBottom);
        LogUtil.d("mMainRect:"+mMainRect.toShortString());
        //画电池头
        canvas.drawRect(mHeadRect, paintOuter);

        //画外框
        paintOuter.setColor(Color.WHITE);
        canvas.drawRoundRect(mMainRect, mRadius, mRadius, paintOuter);

        //画电池芯
        int width   = (int) (mPower * (mMainRect.width() - mMargin*2- mBorder *2));
        int left    = (int) (mMainRect.right - mBorder - mMargin - width);
        int right   = (int) (mMainRect.right - mBorder - mMargin);
        int top     = (int) (mMainRect.top + mBorder + mMargin);
        int bottom  = (int) (mMainRect.bottom- mBorder - mMargin);
        Rect rect = new Rect(left,top,right, bottom);
        if (mIsCharging) {
            paintInner.setColor(Color.GREEN);
            canvas.drawRect(rect, paintInner);
            //绘制完全的闪电
            canvas.drawPath(path, paintLight);
        } else {
            if (mPower < 0.1) {
                paintInner.setColor(Color.RED);
            } else {
                paintInner.setColor(Color.WHITE);
            }
            canvas.drawRect(rect, paintInner);
        }
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        setMeasuredDimension(mWidth, mHeight);

        //使用path绘制闪电形状
        path = new Path();
        path.moveTo(mWidth * 4 / 7, 0);

        path.lineTo(mWidth * 5 / 14, 4 * mHeight / 7);
        path.lineTo(mWidth * 3 / 7, 4 * mHeight / 7);
        path.lineTo(mWidth * 3 / 7, mHeight);
        path.lineTo(mWidth  * 9 / 14, 3 * mHeight / 7);
        path.lineTo(mWidth * 4 / 7, 3 * mHeight / 7);
        path.close();

    }

    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() {
        getContext().registerReceiver(mPowerConnectionReceiver,new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        super.onAttachedToWindow();
    }

    @Override
    protected void onDetachedFromWindow() {
        getContext().unregisterReceiver(mPowerConnectionReceiver);
        super.onDetachedFromWindow();
    }
}

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐