/*** @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-100public 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"})@Overrideprotected 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.DrawBatteryViewandroid:id="@+id/battery_view"android:layout_width="wrap_content"android:layout_height="wrap_content"/>

Android 自定义View绘制电池图标相关推荐

  1. android 自定义View绘制电池电量(电池内带数字显示)

    最新公司需要一个电池内带数字的显示电池电量需求,百度了一下.参考下面这篇文章写的Android自定义View之电池电量显示. 增加了里面电池电量数字显示,还有就是一个屏幕适配.不管屏幕分辨率基本都能适 ...

  2. android自定义弧度按钮,Android 自定义View 绘制六边形设置按钮

    今天逛酷安的时候,发现酷安的设置按钮(截图的右上角),是一个六边形 + 中心圆的图标,所以又是一个自定义View练习对象了.画圆很简单,知道半径即可,而重点就在画出六边形. 酷安截图.png 最终效果 ...

  3. Android自定义View绘制闹钟

    Android自定义View绘制闹钟 本文简单实现了一个闹钟,扩展View,Canvas绘制 效果如下: 代码如下: package com.gaofeng.mobile.clock_demo;imp ...

  4. Android自定义View绘制流程

    Android视图层次结构简介 在介绍View绘制流程之前,咱们先简单介绍一下Android视图层次结构以及DecorView,因为View的绘制流程的入口和DecorView有着密切的联系. 我们平 ...

  5. Android自定义View之电池电量显示

    自定义简单的电池电量显示.话不多说,直接上代码 package com.kimascend.thermometer.customview; import android.content.Context ...

  6. android画a4矩形,Android自定义View绘制原理:画多大?画在哪?画什么?(三)

    View绘制就好比画画,抛开Android概念,如果要画一张图,首先会想到哪几个基本问题: 画多大? 画在哪? 怎么画? Android绘制系统也是按照这个思路对View进行绘制,上面这些问题的答案分 ...

  7. Android自定义View绘制闪闪发光的文字

    如何实现类似网页效果中闪闪发光的文字,通过自定义View可以实现这一炫酷效果 1.自定义View public class FlickTextView extends TextView {privat ...

  8. Android 自定义View绘制的基本开发流程 Android自定义View(二)

    1 View绘制的过程 View的测量--onMeasure() View的位置确定--onLayout() View的绘制--onDraw() 2 View的测量--onMeasure() Andr ...

  9. Android 自定义View 绘制五角星

    背景 之前写过的App里有评分的功能,而显示评分一般使用系统的RatingBar再加自定义,一切都很完美,但是产品提了一个需求,例如4.6.4.7.5.8分,不要显示为4个星星加一个半星(4.5分), ...

最新文章

  1. Java时间处理类SimpleDateFormat的parse和format方法的正确使用
  2. java ee 分页_【JavaEE】JavaEE分页实践
  3. C++实现插值查找(附完整源码)
  4. C语言简单题-找最大的字符串
  5. 无人机、IoT 设备都有漏洞?专访以色列老牌安全公司 Check Point|拟合
  6. chrome里面的一些小技巧
  7. php会话控制区别和流程,PHP会话控制:cookie和session区别与用法深入理解_后端开发...
  8. SpringBoot四大核心之actuator——程序监控器
  9. 数据库外连接和内连接详解
  10. 文件系统在NVMe SSD上的性能表现分析
  11. 【Python】【Flask】Flask 后台发送html页面多种方法
  12. MySQL incompatible with sql_mode=only_full_group_by 问题解决
  13. 解压 .solitairetheme8 文件
  14. AC日记——【模板】二分图匹配 洛谷 P3386
  15. java获取中文拼音,java实现根据汉字获取拼音
  16. 5G NR SSB概述
  17. MySQL有哪些“饮鸩止渴”提高性能的方法?
  18. mod函数在vb中怎么用?
  19. 红帽的PAAS平台openshift
  20. 轮回 第二章 冷傲天

热门文章

  1. Python · 助力冬奥:turtle绘制奥运五环
  2. 【密码学篇】虚拟专用网技术原理与应用(商密)
  3. 【机器学习之逻辑回归】sklearn+python逻辑回归详解
  4. 算法题:SOJ1092: 欧几里得算法
  5. extract($_POST, EXTR_SKIP)讲解
  6. SSMS安装失败及解决
  7. echarts 时间曲线图_web前端显示设备实时温度,ECharts实现温度折线图,实时动态温度曲线图生成...
  8. 4g 控矿驱动 迪兰rx574_主流显卡之争 574/1065战怪猎:世界
  9. Win11系统打开电脑磁盘显示磁盘错误无法打开怎么办?
  10. Mac安装VirtualBox虚拟机-大数据学习准备