Android’s ultimate move: a new solution to completely replace drawble files to implement View rounded corner background styles

Introduction

Recently I have been busy developing the audio and video SDK and encountered many problems. To put it simply, how to make it easier for others to access the SDK, the better. I believe that most Android developers will encounter a scenario where they need to add background color to TextView or Button, modify rounded corners, strokes, etc. Once you see such an implementation effect, it is natural to create a drawble file and set related attributes such as shape, color, radius, etc. Then set the drawble file to the corresponding view to achieve the desired effect. But as an SDK, if different apps need to modify their own unique colors, rounded corners, etc., how can we meet the needs of different apps with minimal changes?

Effect

This article introduces a way to use code to replace the drawble file to achieve the background color, rounded corners and other effects of the view. Without further ado, let’s look at the effects first.

Code

The code to achieve these effects mainly uses ShapeDrawable, GradientDrawable, and Shape implementation classes. Here are 6 commonly used effects:

  1. round

//实心圆
val drawable = ShapeDrawable(OvalShape())
drawable.paint.color = Color.RED
mBinding.tvOval.background = drawable
  1. Partially rounded rectangle with upper left corner

 //上半边圆角矩形
        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. Rounded hollow rectangle with stroke

        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. Rectangle with stroke

 val gradientDrawable = GradientDrawable()
        gradientDrawable.setStroke(dp2px(this, 2).toInt(), Color.RED)
        gradientDrawable.setColor(Color.YELLOW)
        gradientDrawable.shape = GradientDrawable.RECTANGLE
        mBinding.tvOval4.background = gradientDrawable
  1. Rectangle with stroked rounded corners

        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. circle with stroke

        val gradientDrawable2 = GradientDrawable()
        gradientDrawable2.setStroke(dp2px(this, 2).toInt(), Color.RED)
        gradientDrawable2.setColor(Color.GREEN)
        gradientDrawable2.shape = GradientDrawable.OVAL
        mBinding.tvOval6.background = gradientDrawable2

The above are the 6 effects achieved. Basically, the effects that drawble can achieve can also achieve the same effect through code. More effects need to be realized by readers themselves.

Study notes

Android performance optimization: Android Framework underlying principles: Android vehicle version: Android reverse security Study notes: Android audio and video articles: Gradle articles: Jetpack family bucket articles (including Compose): a> Latest Android interview questions in 2023: Android interview questions from previous years: Android core notes: Android’s eight major bodies of knowledge: Flutter article: Kotlin article: OkHttp source code analysis notes: Audio and video interview questions: Hongmeng (OpenHarmony) development learning chapter: https://qr18.cn/FVlo89
https://qr18.cn/AQpN4J
https://qr18.cn/F05ZCM
https://qr18.cn/CQ5TcL
https://qr18.cn/Ei3VPD
https://qr18.cn/DzrmMB
https://qr18.cn/A0gajp
https://qr18.cn/Cw0pBD
https://qr18.cn/CdjtAF
https://qr18.cn/DIvKma
https://qr18.cn/CyxarU
https://qr21.cn/CaZQLo
https://qr18.cn/CKV8OZ
https://qr18.cn/CgxrRy
https://qr18.cn/FTlyCJ
https://qr18.cn/AcV6Ap
https://qr21.cn/FV7h05

Guess you like

Origin blog.csdn.net/maniuT/article/details/134603498