Search code examples
androidtextviewtexturesshadowtextcolor

How to remove texture applied to textpaint and apply hex color?


I applied texture effect to my textpaint using below code

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),textureRid);

    Shader shader = new BitmapShader(bitmap, Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

    textPaint.setShader(shader);

And this texture effect is working fine. But when I tried to apply normal hex color to textpaint, there is no change and the texture effect remains. This below code was working fine, when no texture effect is applied.

textPaint.setColor(color);

Now what I understand is, we need to remove the shader we applied or nullify it. So I tried to pass null value to setshader, it didn't work.

So after lots of research I could able to apply normal color to textpaint even after texture is applied using below code.

    Shader textShader=new LinearGradient(0, 0, 0, 25, new     int[]{color,color}, new float[]{0, 1}, Shader.TileMode.CLAMP);

    textPaint.setShader(textShader);

But am not sure, it is the correct method to apply textcolor to textpaint, after we set shader to textpaint or there is a better method to do it. And also I couldn't able to change the shadow color of the textpaint, when I tried to apply shadow after texture effect is applied. The shadow color remains same as the text color.

    textPaint.clearShadowLayer();
    textPaint.setShadowLayer(shadowvalue , shadowvalue, shadowvalue, color);

If anyone could give your expertise, it would be very helpful. Thanks in advance.


Solution

  • Before applying normal hex color to textpaint for which you applied texture effect before by using setShader, we must pass null to Paint#setShader as @L. Swifter suggested in comment.

    textPaint.setShader(null);
    

    Happy coding!