0

Clarification::
A gradient is set on textView and the eventual outcome is in blur colour like in the first picture. How might I accomplish gradient without blur in the accompanying code, like in the second picture?

Fig11.
Fig2

 val paint: TextPaint =  binding!!.tvGetStarted.getPaint()
            val width = paint.measureText("Get Started")
    
    
            val textShader: Shader = LinearGradient(
                0F, 0F, width,  binding!!.tvGetStarted.getTextSize(), intArrayOf(
                    Color.parseColor("#F97C3C"),
                    Color.parseColor("#FDB54E"),
                    Color.parseColor("#64B678"),
                    Color.parseColor("#478AEA"),
                    Color.parseColor("#8446CC")
                ), null, Shader.TileMode.CLAMP
            )
            binding!!.tvGetStarted.getPaint().setShader(textShader)

Take references from Text with gradient in Android but nothing achieved desirably

Hubby Tiwari
  • 313
  • 6
  • 14
  • 1
    It looks more translucent than blurred, yeah? Have you checked the alpha on the paint? Maybe try `paint.alpha = 255`, just to quickly test. – Mike M. Jan 22 '22 at 04:02
  • Yeah, that user states "I found out that I need to call `textView.setTextColor()` with the first color of the gradient.", which I believe is just resetting the alpha to opaque for them ('cause `#F97C3C` has implied `FF` alpha). IIRC, the primary text colors of several standard library themes have non-opaque alphas, which might be where that's coming from to begin with. – Mike M. Jan 22 '22 at 04:07
  • 1
    @MikeM. I tried and seems its better than before – Hubby Tiwari Jan 22 '22 at 04:08
  • 1
    I recently got around to testing this, and that alpha is coming from the theme color, but that test/quick-fix suggestion is not stable. If you do handle it in code, then `setTextColor()` is what you should use, though you can call it with any opaque color you like, since it's not really used. If you don't want to handle it in code, you can set the `android:textColor` attribute in the layout XML similarly. Just FYI. Cheers! – Mike M. Jan 23 '22 at 19:07
  • 1
    @MikeM.Thanks alot – Hubby Tiwari Jan 24 '22 at 02:45

1 Answers1

0

This answer - Text with gradient in Android, already show you need to call textView.setTextColor() with the first color of the gradient i.e. #F97C3C

After adding textView.setTextColor() you will get output as 2nd output.

I tried this solution and it works for me, output for solution. enter image description here

Android Geek
  • 8,956
  • 2
  • 21
  • 35