Android 自定义View绘制电池图标

/**
 * @anthor GrainRain
 * @funcation 自定义View绘制电池
 * @date 2019/8/27
 */
public class DrawBatteryView extends View{

    private Paint batteryPaint = new Paint();    //电池画笔
    private Paint electricQuantityRedPaint = new Paint();     //红色电量画笔
    private Paint electricQuantityGreenPaint = new Paint();     //绿色电量画笔
    private int StrokeWidth = 10;               //笔画宽度
    private int ElectricQuantity = 0;          //电量

    //电池尺寸
    private int left = 0;
    private int top = 0;
    private int right = 60;
    private int bottom = 25;

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

    public DrawBatteryView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public DrawBatteryView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    //外部调用的接口 设置电池尺寸
    public void setBatterySize(int size){
        this.right = right + (int)(size * 1.5);
        this.bottom = bottom + (int)(size * 0.625);

        //更新调用onDraw重新绘制
        invalidate();
    }

    //外部调用的接口 设置电量 0-100
    public void setElectricQuantity(int electric){
        if(electric >= 0 && electric <= 100) {
            this.ElectricQuantity = electric;
        } else if(electric < 0) {
            this.ElectricQuantity = 0;
        } else if(electric > 100) {
            this.ElectricQuantity = 100;
        }

        //更新调用onDraw重新绘制
        invalidate();
    }

    @SuppressLint({"ResourceAsColor", "DrawAllocation"})
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        batteryPaint.setColor(Color.GRAY);
//        batteryPaint.setStyle(Paint.Style.STROKE);   //设置空心
        batteryPaint.setStrokeWidth(StrokeWidth);

        electricQuantityRedPaint.setColor(Color.RED);
        electricQuantityRedPaint.setStrokeWidth(StrokeWidth);

        electricQuantityGreenPaint.setColor(Color.GREEN);
        electricQuantityGreenPaint.setStrokeWidth(StrokeWidth);

        //电池头部位置
        int BatteryHeadLeft = right;
        int BatteryHeadTop = top+(bottom-top)/4;
        int BatteryHeadRight = right+((right-left)/15);
        int BatteryHeadBottom = top+((bottom-top)/4*3);

        //电量位置
        int ElectricQuantityLeft = left+2;
        int ElectricQuantityTop = top+2;
        int ElectricQuantityRight = (int)(((right-left)/100.0f)*ElectricQuantity)-2;
        int ElectricQuantityBottom = bottom-2;

        //电量为0时绘制感叹号
        int ExclamationMarkHeadLeft = (int)(((right-left)/100.0f)*10);
        int ExclamationMarkHeadTop = top+(bottom-top)/4;
        int ExclamationMarkHeadRight = (int)(((right-left)/100.0f)*20);
        int ExclamationMarkHeadBottom = top+((bottom-top)/4*3);
        int ExclamationMarkBodyLeft = (int)(((right-left)/100.0f)*30);
        int ExclamationMarkBodyTop = top+(bottom-top)/4;
        int ExclamationMarkBodyRight = (int)(((right-left)/100.0f)*85);
        int ExclamationMarkBodyBottom = top+((bottom-top)/4*3);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            canvas.drawRoundRect(left, top, right, bottom,0,0, batteryPaint);
            canvas.drawRoundRect(BatteryHeadLeft, BatteryHeadTop, BatteryHeadRight, BatteryHeadBottom,0,0, batteryPaint);

            //电量小于20的话使用红色画笔 否则使用绿色画笔
            if(ElectricQuantity > 0 && ElectricQuantity < 20) {
                canvas.drawRoundRect(ElectricQuantityLeft, ElectricQuantityTop, ElectricQuantityRight, ElectricQuantityBottom,0,0, electricQuantityRedPaint);
            } else if(ElectricQuantity >= 20) {
                canvas.drawRoundRect(ElectricQuantityLeft, ElectricQuantityTop, ElectricQuantityRight, ElectricQuantityBottom,0,0, electricQuantityGreenPaint);
            } else if(ElectricQuantity == 0) {
                canvas.drawRoundRect(ExclamationMarkHeadLeft, ExclamationMarkHeadTop, ExclamationMarkHeadRight, ExclamationMarkHeadBottom,0,0, electricQuantityRedPaint);
                canvas.drawRoundRect(ExclamationMarkHeadLeft, ExclamationMarkHeadTop, ExclamationMarkHeadRight, ExclamationMarkHeadBottom,0,0, electricQuantityRedPaint);
            }

        } else {
            canvas.drawRoundRect(new RectF(left, top, right, bottom), 0, 0, batteryPaint);
            canvas.drawRoundRect(new RectF(BatteryHeadLeft, BatteryHeadTop, BatteryHeadRight, BatteryHeadBottom), 0, 0, batteryPaint);

            //电量小于20的话使用红色画笔 否则使用绿色画笔
            if(ElectricQuantity > 0 && ElectricQuantity < 20) {
                canvas.drawRoundRect(new RectF(ElectricQuantityLeft, ElectricQuantityTop, ElectricQuantityRight, ElectricQuantityBottom), 0, 0, electricQuantityRedPaint);
            } else if(ElectricQuantity >= 20) {
                canvas.drawRoundRect(new RectF(ElectricQuantityLeft, ElectricQuantityTop, ElectricQuantityRight, ElectricQuantityBottom), 0, 0, electricQuantityGreenPaint);
            } else if(ElectricQuantity == 0) {
                canvas.drawRoundRect(new RectF(ExclamationMarkHeadLeft, ExclamationMarkHeadTop, ExclamationMarkHeadRight, ExclamationMarkHeadBottom), 0, 0, electricQuantityRedPaint);
                canvas.drawRoundRect(new RectF(ExclamationMarkBodyLeft, ExclamationMarkBodyTop, ExclamationMarkBodyRight, ExclamationMarkBodyBottom), 0, 0, electricQuantityRedPaint);
            }
        }
    }
}

使用

DrawBatteryView batteryView = findViewById(R.id.battery_view);
//设置电池图标尺寸
batteryView.setBatterySize(50);
//设置电池电量
batteryView.setElectricQuantity(50);

<com.example.grain.test.DrawBatteryView
    android:id="@+id/battery_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值