Android电量显示控件

2021-03-03  本文已影响0人  雁过留声_泪落无痕

原始出处:http://blog.csdn.net/donkor_/article/details/53175903

<resources>
    <declare-styleable name="Battery">
        <attr name="batteryOrientation">
            <enum name="horizontal" value="0" />
            <enum name="vertical" value="1" />
        </attr>
        <attr name="batteryLowColor" format="color" />
        <attr name="batteryMiddleColor" format="color" />
        <attr name="batteryHighColor" format="color" />
        <attr name="batteryBorderColor" format="color" />
        <attr name="batteryPower" format="integer" />
    </declare-styleable>
</resources>
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import com.cmii.xr.easy_class.teacher.R;

public class BatteryView extends View {

    private static final int DEFAULT_LOW_BATTERY_COLOR = Color.RED;
    private static final int DEFAULT_MIDDLE_BATTERY_COLOR = Color.YELLOW;
    private static final int DEFAULT_HIGH_BATTERY_COLOR = Color.GREEN;
    private static final int DEFAULT_BORDER_COLOR = Color.BLACK;

    private int mPower = 100;
    private int orientation;
    private int width;
    private int height;

    private int mLowBatteryColor;
    private int mMiddleBatteryColor;
    private int mHighBatteryColor;
    private int mBorderColor;

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

    public BatteryView(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.Battery);
        mLowBatteryColor = typedArray.getColor(R.styleable.Battery_batteryLowColor, DEFAULT_LOW_BATTERY_COLOR);
        mMiddleBatteryColor = typedArray.getColor(R.styleable.Battery_batteryMiddleColor, DEFAULT_MIDDLE_BATTERY_COLOR);
        mHighBatteryColor = typedArray.getColor(R.styleable.Battery_batteryHighColor, DEFAULT_HIGH_BATTERY_COLOR);
        mBorderColor = typedArray.getColor(R.styleable.Battery_batteryBorderColor, DEFAULT_BORDER_COLOR);
        orientation = typedArray.getInt(R.styleable.Battery_batteryOrientation, 0);
        mPower = typedArray.getInt(R.styleable.Battery_batteryPower, 100);
        width = getMeasuredWidth();
        height = getMeasuredHeight();
        /**
         * recycle() :官方的解释是:回收TypedArray,以便后面重用。在调用这个函数后,你就不能再使用这个TypedArray。
         * 在TypedArray后调用recycle主要是为了缓存。当recycle被调用后,这就说明这个对象从现在可以被重用了。
         *TypedArray 内部持有部分数组,它们缓存在Resources类中的静态字段中,这样就不用每次使用前都需要分配内存。
         */
        typedArray.recycle();
    }

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

        width = getMeasuredWidth();
        height = getMeasuredHeight();
        //判断电池方向    horizontal: 0   vertical: 1
        if (orientation == 0) {
            drawHorizontalBattery(canvas);
        } else {
            drawVerticalBattery(canvas);
        }
    }

    /**
     * 绘制水平电池
     *
     * @param canvas
     */
    private void drawHorizontalBattery(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(mLowBatteryColor);
        paint.setStyle(Paint.Style.STROKE);
        float strokeWidth = width / 20.f;
        float strokeWidth_2 = strokeWidth / 2;
        paint.setStrokeWidth(strokeWidth);
        RectF r1 = new RectF(strokeWidth_2, strokeWidth_2, width - strokeWidth - strokeWidth_2, height - strokeWidth_2);
        //设置外边框颜色为黑色
        paint.setColor(mBorderColor);
        canvas.drawRect(r1, paint);
        paint.setStrokeWidth(0);
        paint.setStyle(Paint.Style.FILL);
        //画电池内矩形电量
        float offset = (width - strokeWidth * 2) * mPower / 100.f;
        RectF r2 = new RectF(strokeWidth, strokeWidth, offset, height - strokeWidth);
        //根据电池电量决定电池内矩形电量颜色
        if (mPower < 30) {
            paint.setColor(mLowBatteryColor);
        }
        if (mPower >= 30 && mPower < 50) {
            paint.setColor(mMiddleBatteryColor);
        }
        if (mPower >= 50) {
            paint.setColor(mHighBatteryColor);
        }
        canvas.drawRect(r2, paint);
        //画电池头
        RectF r3 = new RectF(width - strokeWidth, height * 0.25f, width, height * 0.75f);
        //设置电池头颜色为黑色
        paint.setColor(mBorderColor);
        canvas.drawRect(r3, paint);
    }

    /**
     * 绘制垂直电池
     *
     * @param canvas
     */
    private void drawVerticalBattery(Canvas canvas) {
        Paint paint = new Paint();
        paint.setColor(mBorderColor);
        paint.setStyle(Paint.Style.STROKE);
        float strokeWidth = height / 20.0f;
        float strokeWidth2 = strokeWidth / 2;
        paint.setStrokeWidth(strokeWidth);
        int headHeight = (int) (strokeWidth + 0.5f);
        RectF rect = new RectF(strokeWidth2, headHeight + strokeWidth2, width - strokeWidth2, height - strokeWidth2);
        canvas.drawRect(rect, paint);

        paint.setStrokeWidth(0);
        float topOffset = (height - headHeight - strokeWidth) * (100 - mPower) / 100.0f;
        RectF rect2 = new RectF(strokeWidth, headHeight + strokeWidth + topOffset, width - strokeWidth, height - strokeWidth);
        paint.setStyle(Paint.Style.FILL);
        if (mPower < 30) {
            paint.setColor(mLowBatteryColor);
        }
        if (mPower >= 30 && mPower < 50) {
            paint.setColor(mMiddleBatteryColor);
        }
        if (mPower >= 50) {
            paint.setColor(mHighBatteryColor);
        }
        canvas.drawRect(rect2, paint);

        paint.setColor(mBorderColor);
        RectF headRect = new RectF(width / 4.0f, 0, width * 0.75f, headHeight);
        canvas.drawRect(headRect, paint);
    }

    /**
     * 设置电池电量
     *
     * @param power
     */
    public void setPower(int power) {
        this.mPower = power;
        if (mPower < 0) {
            mPower = 100;
        }
        invalidate();//刷新VIEW
    }

    /**
     * 获取电池电量
     *
     * @return
     */
    public int getPower() {
        return mPower;
    }
}
效果图
上一篇 下一篇

猜你喜欢

热点阅读