Android自定义View之继承TextView绘制背景
作者:Brioal 时间:2021-11-05 11:16:06
本文实例为大家分享了TextView绘制背景的方法,供大家参考,具体内容如下
效果:
实现流程:
1.初始化:对画笔进行设置
mPaintIn = new Paint();
mPaintIn.setAntiAlias(true);
mPaintIn.setDither(true);
mPaintIn.setStyle(Paint.Style.FILL);
mPaintIn.setColor(getResources().getColor(R.color.colorPrimary));
mPaintOut = new Paint();
mPaintOut.setAntiAlias(true);
mPaintOut.setDither(true);
mPaintOut.setStyle(Paint.Style.FILL);
mPaintOut.setColor(getResources().getColor(R.color.colorAccent));
2.绘制外框,内框,文字
获取组件宽高
int width = getMeasureWidth();
int height = getMeasureHeight();
绘制
@Override
protected void onDraw(Canvas canvas) {
//绘制背景,在绘制文字之前绘制
canvas.drawRect(new Rect(0, 0, getMeasuredWidth(), getMeasuredHeight()), mPaintIn);
canvas.drawRect(new Rect(10, 10, getMeasuredWidth()-10, getMeasuredHeight()-10), mPaintOut);
super.onDraw(canvas);
}
标签:Android,自定义,View,TextView,背景
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android 13新功能及适配工作详解
2022-04-13 11:05:33
Java操作Excel的示例详解
2021-07-08 00:56:56
![](https://img.aspxhome.com/file/2023/2/125662_0s.png)
spring boot RestTemplate 发送get请求的踩坑及解决
2022-01-19 16:08:09
![](https://img.aspxhome.com/file/2023/2/69662_0s.png)
改进c# 代码的五个技巧(二)
2023-01-09 08:57:56
![](https://img.aspxhome.com/file/2023/2/104222_0s.png)
C#实现贪吃蛇小游戏
2022-09-28 17:17:59
![](https://img.aspxhome.com/file/2023/6/80116_0s.jpg)
Java全面细致讲解Wrapper的使用
2023-02-14 00:49:24
![](https://img.aspxhome.com/file/2023/8/63788_0s.png)
C# WebApi 异常处理解决方案
2021-06-14 16:52:08
![](https://img.aspxhome.com/file/2023/1/77261_0s.png)
关于fastjson的@JSONField注解的一些问题(详解)
2021-11-23 09:09:34
Spring占位符Placeholder的实现原理解析
2023-03-14 18:14:00
![](https://img.aspxhome.com/file/2023/5/66075_0s.png)
java实现双色球机选号码生成器
2022-10-05 07:41:33
使用Files.walkFileTree遍历目录文件
2021-09-27 06:12:40
一文详解Java线程的6种状态与生命周期
2022-02-08 08:44:30
![](https://img.aspxhome.com/file/2023/5/122955_0s.jpg)
Android权限如何禁止以及友好提示用户开通必要权限详解
2023-10-09 04:29:41
![](https://img.aspxhome.com/file/2023/7/88097_0s.jpg)
Android Dialog对话框实例代码讲解
2021-12-18 06:34:50
android里TextView加下划线的几种方法总结
2022-03-25 10:42:18
C#身份证验证小例子
2022-06-22 05:22:01
Android 游戏开发中绘制游戏触摸轨迹的曲线图
2023-02-26 08:50:30
Java面向对象实现汽车租赁系统
2023-05-20 07:03:06
![](https://img.aspxhome.com/file/2023/8/64428_0s.jpg)
C# 解析 Excel 并且生成 Csv 文件代码分析
2021-11-09 18:04:04
ListView实现聊天列表之处理不同数据项
2022-07-01 01:06:58
![](https://img.aspxhome.com/file/2023/4/138764_0s.jpg)