Android终极大招之全面取代drawble文件实现View圆角背景样式的新方案

简介

最近一直忙于音视频SDK的开发,遇到很多问题,简单来说,就是怎么让别人接入SDK时越简单越好。相信大多数Android开发都会遇到一个场景,给TextView或Button添加背景颜色,修改圆角,描边等需求。一看到这样的实现效果,自然就是创建drawble文件,设置相关属性shap,color,radius等。然后将drawble文件设置给对应的view即可实现想要的效果。但是作为一个SDK,如果不同的app都需要修改自己独特的颜色,圆角等需求,如何以最小的改动来满足不同app的需求呢?

效果

本文介绍一种使用代码的形式来替代drawble文件实现view的背景色,圆角等效果。话不多说,先看效果。

代码实现

代码实现这些效果主要用到 ShapeDrawable,GradientDrawable,以及 Shape 的实现类。下面介绍常用的6种效果:

  1. 圆形
复制代码
//实心圆
val drawable = ShapeDrawable(OvalShape())
drawable.paint.color = Color.RED
mBinding.tvOval.background = drawable
  1. 左上角部分圆角矩形
复制代码
 //上半边圆角矩形
        val externalRound = floatArrayOf(10f,10f,0f,0f,0f,0f,0f,0f)
        val shapeDrawable = ShapeDrawable(RoundRectShape(externalRound, RectF(0f,0f,0f,0f), floatArrayOf(0f,0f,0f,0f,0f,0f,0f,0f)))
        shapeDrawable.paint.color = Color.BLUE
        mBinding.tvOval2.background = shapeDrawable
  1. 带描边的圆角空心矩形
复制代码
        val roundIn = dp2px(this, 30)
        val externalRounds = floatArrayOf(roundIn, roundIn, roundIn, roundIn, roundIn, roundIn, roundIn, roundIn)
        val outlineRound = floatArrayOf(roundIn, roundIn, roundIn, roundIn, roundIn, roundIn, roundIn, roundIn)
        val rectF = RectF(dp2px(this, 2), dp2px(this, 2), dp2px(this, 2), dp2px(this, 2))
        val drawable1 = ShapeDrawable(RoundRectShape(externalRounds, rectF, outlineRound))
        drawable1.paint.color = Color.RED
        mBinding.tvOval3.background = drawable1
  1. 带描边的矩形
复制代码
 val gradientDrawable = GradientDrawable()
        gradientDrawable.setStroke(dp2px(this, 2).toInt(), Color.RED)
        gradientDrawable.setColor(Color.YELLOW)
        gradientDrawable.shape = GradientDrawable.RECTANGLE
        mBinding.tvOval4.background = gradientDrawable
  1. 带描边圆角的矩形
复制代码
        val gradientDrawable1 = GradientDrawable()
        gradientDrawable1.setStroke(dp2px(this, 2).toInt(), Color.RED)
        gradientDrawable1.setColor(Color.BLUE)
        gradientDrawable1.shape = GradientDrawable.RECTANGLE
        gradientDrawable1.cornerRadius = roundIn
        mBinding.tvOval5.background = gradientDrawable1
  1. 带描边的圆形
复制代码
        val gradientDrawable2 = GradientDrawable()
        gradientDrawable2.setStroke(dp2px(this, 2).toInt(), Color.RED)
        gradientDrawable2.setColor(Color.GREEN)
        gradientDrawable2.shape = GradientDrawable.OVAL
        mBinding.tvOval6.background = gradientDrawable2

以上是实现的6种效果,基本上drawble能实现的效果,通过代码也可以达到同样的效果。更多效果需要读者自己动手实现。

Android 学习笔录

Android 性能优化篇:https://qr18.cn/FVlo89
Android Framework底层原理篇:https://qr18.cn/AQpN4J
Android 车载篇:https://qr18.cn/F05ZCM
Android 逆向安全学习笔记:https://qr18.cn/CQ5TcL
Android 音视频篇:https://qr18.cn/Ei3VPD
Jetpack全家桶篇(内含Compose):https://qr18.cn/A0gajp
OkHttp 源码解析笔记:https://qr18.cn/Cw0pBD
Kotlin 篇:https://qr18.cn/CdjtAF
Gradle 篇:https://qr18.cn/DzrmMB
Flutter 篇:https://qr18.cn/DIvKma
Android 八大知识体:https://qr18.cn/CyxarU
Android 核心笔记:https://qr21.cn/CaZQLo
Android 往年面试题锦:https://qr18.cn/CKV8OZ
2023年最新Android 面试题集:https://qr18.cn/CgxrRy
Android 车载开发岗位面试习题:https://qr18.cn/FTlyCJ
音视频面试题锦:https://qr18.cn/AcV6Ap

相关推荐
Pu_Nine_910 小时前
IntersectionObserver 详解:封装 Vue 指令实现图片懒加载
前端·javascript·vue.js·性能优化
云起SAAS11 小时前
抖音小游戏源码 - 消消乐 | 含激励广告+成就系统 | 开箱即用商业级消除游戏模板
android·游戏·广告联盟·看激励广告联盟流量主·抖音小游戏源码 - 消消乐
爱喝水的鱼丶12 小时前
SAP-ABAP:数据类型与数据对象(8篇) 第七篇:进阶优化篇——基于类型与对象特征的性能优化技巧
运维·数据库·学习·性能优化·sap·abap·开发交流
大貔貅喝啤酒12 小时前
基于Windows下载安装Android Studio 3.3.2版本教程(2026详细图文版)
android·java·windows·android studio
程序员码歌12 小时前
OpenSpec 到 Superpowers:AI 编码从说清到做对
android·前端·人工智能
2501_9151063212 小时前
深入解析无源码iOS加固原理与方案,保护应用安全
android·安全·ios·小程序·uni-app·cocoa·iphone
jiayong2314 小时前
前端面试题库 - 工程化与性能优化篇
前端·面试·性能优化
计算机安禾16 小时前
【c++面向对象编程】第35篇:构造函数与异常:如何避免资源泄露?
开发语言·javascript·c++·算法·性能优化
黄林晴16 小时前
重磅官宣:Android UI 开发正式进入 Compose-first 时代
android·google io
Kapaseker16 小时前
搞懂变换!精通 Compose 绘制(二)
android·kotlin