BatteryStateViewM.java 8.84 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.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.Drawable;
import android.util.AttributeSet;
import android.view.View;

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

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

    private Paint mPaint;//文字的画笔
    private Paint mBatteryPaint;//电池画笔
    private Paint mPowerPaint;//电量画笔
    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 boolean isShowText;

    private int power = 50;//当前电量(满电100)

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

    private boolean isCharge = false;
    private Bitmap img;


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

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

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

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

    public BatteryStateViewM(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, 81, 210, 242));
        powerColor = typedArray.getColor(R.styleable.BatteryStateView_powerColor,Color.argb(255, 81, 210, 242));
        lowPowerColor = typedArray.getColor(R.styleable.BatteryStateView_lowPowerColor,Color.argb(255, 255, 0, 0));
        isShowText = typedArray.getBoolean(R.styleable.BatteryStateView_showText,false);
        isShowText = true;
        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.setStrokeWidth(mBatteryStroke);
        /**
         * 设置电量画笔
         */
        mPowerPaint = new Paint();
        mPowerPaint.setAntiAlias(true);
        mPowerPaint.setStyle(Paint.Style.FILL);

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

    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 = 26;
        h = 30;
        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 -= 10;
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if( power <= 20 ) {
            mPowerPaint.setColor(lowPowerColor);
            mPaint.setColor(lowPowerColor);
        } else {
            mPowerPaint.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();
            float top = fontMetrics.top;//为基线到字体上边框的距离
            float bottom = fontMetrics.bottom;//为基线到字体下边框的距离
            int baseLineY = (int) (specHeightSize / 2 - top / 2 - bottom / 2);//基线中间点的y轴计算公式
            // canvas.drawText(textString, (int) (width*0.5), baseLineY, mPaint);
            //(-5  距离最右边5)
            //canvas.drawText(textString, specWidthSize-textWidth-10, baseLineY, mPaint);

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

        /**
         * 设置电池矩形
         * 2 间隔距离
         */
        mBatteryRect = new RectF(6, 10, specWidthSize - 6, specHeightSize - 10);
        /**
         * 设置电池盖矩形
         */
        mCapRect = new Rect((int)(specWidthSize/2 - mCapWidth/2), (int)(6), (int)(specWidthSize/2 + mCapWidth/2),
                (int)(10));
        /**
         * 设置电量矩形
         */
        float high;
        textWidth = 0;
        /*if (power<20) {
            high = (specHeightSize-textWidth-20)/100.0f*20+10;
        }else{
            high = (specHeightSize-textWidth-20)/100.0f*power+10;
        }
        mPowerRect = new RectF(mBatteryStroke+6-1, 10+mBatteryStroke, specWidthSize-6*//*+1*//*,
                high);*/
        if ( power < 20 ) {
            high = ( specHeightSize - textWidth - 20) / 100.0f * 20 + 10;
        } else {
            high = ( specHeightSize - textWidth - 20 ) / 100.0f * power + 10;
        }
        /*mPowerRect = new RectF(mBatteryStroke+6-1, specHeightSize - high, specWidthSize-6*//*+1*//*,
                specHeightSize - 10 - 1f);*/
        mPowerRect = new RectF(mBatteryStroke + 6 + 1, specHeightSize - high + 2, specWidthSize - 6 - 2,
                specHeightSize - 10f - 2);

        canvas.drawRoundRect(mBatteryRect, 3, 3, mBatteryPaint);
        //canvas.drawRoundRect(mCapRect, 5, 5, mBatteryPaint);// 画电池盖
        canvas.drawRect(mCapRect, mPowerPaint);// 画电池盖
        canvas.drawRoundRect(mPowerRect, 3, 3, mPowerPaint);// 画电量

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

    public boolean isCharge() {
        return isCharge;
    }

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