Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set gradient on TextView color with angle?

Following code will set gradient on textview (not background, but text itself). But I need to change angle of this gradient, how to do it?

Shader textShader = new LinearGradient(0, 0, 0, textView.getPaint().getTextSize(),
        new int[]{context.getResources().getColor(R.color.color1), context.getResources().getColor(R.color.color2)},
        new float[]{0, 1}, Shader.TileMode.CLAMP);
textView.getPaint().setShader(textShader);

Thank you in advance.

like image 326
Michalsx Avatar asked Oct 30 '22 12:10

Michalsx


1 Answers

final TextView myTextView = findViewById(R.id.my_text_view);

myTextView.post(new Runnable() 
    {

         @Override

            public void run() {

                int length = textView.getMeasuredWidth();

            float angle = 45; // specify angle in degrees here

            Shader textShader = new LinearGradient(0, 0, (int) (Math.sin(Math.PI * angle / 180) * length), 
                                    (int) (Math.cos(Math.PI * angle / 180) * length),

                                        new int[]{Color.BLUE, Color.GREEN, Color.GREEN, Color.BLUE, Color.RED, Color.GREEN, Color.BLUE, Color.RED},

    null, 
        Shader.TileMode.CLAMP);

        myTextView.getPaint().setShader(textShader);

            textView.invalidate();

            }

    });

result

like image 190
Chaudhari Sachin Avatar answered Nov 14 '22 15:11

Chaudhari Sachin