BatteryStateView.java 10.4 KB
package com.sw.laryngoscope.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffColorFilter;
import android.graphics.Rect;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import com.sw.laryngoscope.R;
import com.sw.laryngoscope.utils.Logger;

/**
 * 电量显示控件
 */
public class BatteryStateView extends View {

    private Paint mPaint;//文字的画笔
    private Paint mBatteryPaint;//电池画笔
    private Paint mCurPowerPaint;//电量画笔
    private Paint mCapPowerPaint;//电量画笔
    private float mBatteryStroke = 1;//电池框宽度

    private RectF mBatteryRect;//电池矩形
    private Rect mCapRect;//电池盖矩形
    private RectF mPowerRect;//电量矩形

    public static Typeface typeFace;
    private int specWidthSize, specHeightSize;
    private  float textSize;
    private int batteryColor;//电池框颜色
    private int powerColor;//电量颜色
    private int lowPowerColor;//低电颜色
    private int noPowerColor;//低电颜色

    private boolean isShowText;

    private int lowerPower = 30;
    private int noPower = 20;
    private int power = 19;//当前电量(满电100)

    private float textWidth = 0;//电量文字长度
    private float mCapWidth;
    private float mCapHeight = 12;

    private boolean isCharge = false;
    private boolean isWirelessCharge = false;
    private Bitmap wireimg;
    private Bitmap img;


    public void setPower(int power) {
        if ( power < 0 ) {
            power = 0;
        } else if ( power > 100) {
            power = 100;
        }
        this.power = power;
        invalidate();
    }

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

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

    public BatteryStateView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public BatteryStateView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        //typeFace = Typeface.createFromAsset(context.getAssets(), "fonts/" + Datas.logoFont);
        TypedArray typedArray = context.obtainStyledAttributes(attrs,R.styleable.BatteryStateView);
        textSize = typedArray.getDimension(R.styleable.BatteryStateView_batteryTextSize,22);//#0198BD 008AAC #51d2f2
        batteryColor = typedArray.getColor(R.styleable.BatteryStateView_batteryColor,Color.argb(255, 77, 77, 77));
        powerColor = typedArray.getColor(R.styleable.BatteryStateView_powerColor,Color.argb(255, 84, 189, 23));
        lowPowerColor = typedArray.getColor(R.styleable.BatteryStateView_lowPowerColor,Color.argb(255, 255, 175, 77));
        noPowerColor = typedArray.getColor(R.styleable.BatteryStateView_lowPowerColor,Color.argb(255, 255, 10, 10));

        isShowText = typedArray.getBoolean(R.styleable.BatteryStateView_showText,false);
        isShowText = false;
        mCapWidth = typedArray.getDimension(R.styleable.BatteryStateView_mCapWidth,20);
        typedArray.recycle();
        initPaint();
    }

    public void initPaint() {
        /**
         * 设置电池画笔
         */
        mBatteryPaint = new Paint();
        mBatteryPaint.setColor(batteryColor);
        mBatteryPaint.setAntiAlias(true);
        //mBatteryPaint.setStyle(Paint.Style.STROKE);
        mBatteryPaint.setStyle(Paint.Style.FILL);
        //mBatteryPaint.setStrokeWidth(mBatteryStroke);
        /**
         * 设置电量画笔
         */
        mCurPowerPaint = new Paint();
        mCurPowerPaint.setAntiAlias(true);
        mCurPowerPaint.setStyle(Paint.Style.FILL);

        /**
         * 设置电量画笔
         */
        mCapPowerPaint = new Paint();
        mCapPowerPaint.setAntiAlias(true);
        mCapPowerPaint.setStyle(Paint.Style.FILL);

        /**
         * 设置文字画笔
         */
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        textSize = 12;
        mPaint.setTextSize(textSize);
        //wireimg = BitmapFactory.decodeResource(getResources(), R.mipmap.img_charge);
        Drawable drawable = getResources().getDrawable(R.mipmap.img_charge);
        PorterDuffColorFilter porterDuffColorFilter = new PorterDuffColorFilter(Color.parseColor("#E3E1E1"),
                PorterDuff.Mode.SRC_ATOP);
        drawable.setColorFilter(porterDuffColorFilter);
        wireDrawableToBitamp(drawable);

        Drawable drawable1 = getResources().getDrawable(R.mipmap.img_wireless_charging_fill_1);
        PorterDuffColorFilter porterDuffColorFilter1 = new PorterDuffColorFilter(Color.parseColor("#E3E1E1"),
                PorterDuff.Mode.SRC_ATOP);
        drawable1.setColorFilter(porterDuffColorFilter1);
        drawableToBitamp(drawable1);
        //img = ((BitmapDrawable) drawable).getBitmap();
    }

    private void wireDrawableToBitamp(Drawable drawable) {
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        //System.out.println("Drawable转Bitmap");
        Bitmap.Config config =
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565;
        w = 15;
        h = 27;
        wireimg = Bitmap.createBitmap(w, h, config);
        //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
        Canvas canvas = new Canvas(wireimg);
        drawable.setBounds(0, 0, w, h);
        Logger.e("specWidthSize: w " + w + " h " + h);
        drawable.draw(canvas);
    }

    private void drawableToBitamp(Drawable drawable) {
        int w = drawable.getIntrinsicWidth();
        int h = drawable.getIntrinsicHeight();
        //System.out.println("Drawable转Bitmap");
        Bitmap.Config config =
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
                        : Bitmap.Config.RGB_565;
        w = 24;
        h = 24;
        img = Bitmap.createBitmap(w, h, config);
        //注意,下面三行代码要用到,否在在View或者surfaceview里的canvas.drawBitmap会看不到图
        Canvas canvas = new Canvas(img);
        drawable.setBounds(0, 0, w, h);
        Logger.e("specWidthSize: w " + w + " h " + h);
        drawable.draw(canvas);
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);

        specWidthSize = MeasureSpec.getSize(widthMeasureSpec);//宽
        specHeightSize = MeasureSpec.getSize(heightMeasureSpec);//高

        Logger.e("specWidthSize:" + specWidthSize);
        Logger.e("specHeightSize:" + specHeightSize);
        setMeasuredDimension(specWidthSize, specHeightSize);
        specHeightSize -= 0;//10
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int blankSpace = 2;
        if ( power <= noPower ) {
            mCapPowerPaint.setColor(batteryColor);
            mCurPowerPaint.setColor(noPowerColor);
            mPaint.setColor(lowPowerColor);
        } else if ( power <= lowerPower ) {
            mCapPowerPaint.setColor(batteryColor);
            mCurPowerPaint.setColor(lowPowerColor);
            mPaint.setColor(lowPowerColor);
        } else {
            mCapPowerPaint.setColor(powerColor);
            mCurPowerPaint.setColor(powerColor);
            mPaint.setColor(powerColor);
        }

        if( isShowText ) {
            String textString = power + "%";
            Rect textRect = new Rect();
            mPaint.getTextBounds(textString, 0, textString.length(), textRect);
            textWidth = textRect.width();
            float textHeight = textRect.height();
            Logger.e("textWidth:" + textWidth + "textHeight:" + textHeight);
            Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();

            //canvas.drawText(textString, (specWidthSize - textWidth) / 2, specHeightSize + 3, mPaint);
        } else {
            textWidth = 0;
        }

        /**
         * 设置电池矩形
         * 2 间隔距离
         */
        mBatteryRect = new RectF(0, 0, specWidthSize - mCapWidth - blankSpace, specHeightSize);
        /**
         * 设置电池盖矩形
         */
        mCapRect = new Rect((int)(specWidthSize - mCapWidth), (int)(specHeightSize / 2 - mCapHeight / 2),
                specWidthSize, (int)(specHeightSize / 2 + mCapHeight / 2));
        /**
         * 设置电量矩形
         */
        float Width;
        if ( power < 5 ) {
            Width = ( specWidthSize - mCapWidth - blankSpace ) / 100.0f * 5;
        } else {
            Width = ( specWidthSize - mCapWidth - blankSpace ) / 100.0f * power;
        }

        mPowerRect = new RectF(0, 0, Width, specHeightSize);

        canvas.drawRoundRect(mBatteryRect, 3, 3, mBatteryPaint);// 画电池背景
        canvas.drawRect(mCapRect, mCapPowerPaint);// 画电池盖
        canvas.drawRoundRect(mPowerRect, 3, 3, mCurPowerPaint);// 画电量

        if ( isCharge ) {
            try {
                Paint tempPaint = new Paint();
                canvas.drawBitmap(wireimg,
                        (specWidthSize - wireimg.getWidth()) / 2 - mCapWidth / 2,
                        (specHeightSize - wireimg.getHeight()) / 2, tempPaint);
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
        if ( isWirelessCharge ) {
            try {
                Paint tempPaint = new Paint();
                canvas.drawBitmap(img,
                        (specWidthSize - img.getWidth()) / 2 - mCapWidth / 2,
                        (specHeightSize - img.getHeight()) / 2, tempPaint);
            } catch (Exception e) {
                e.getStackTrace();
            }
        }
    }

    public boolean isCharge() {
        return isCharge;
    }

    public void setCharge(boolean charge) {
        isCharge = charge;
    }

    public void setWirelessCharge(boolean charge) {
        isWirelessCharge = charge;
    }

}