Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

774
Visualizações
How to set gradient as text color as well as add stroke around it in TextView Android?

I want to set gradient as textcolor and also at the same time I want text to have a solid stroke around it as well in a TextView. So far what I have achieved is text can only show gradient or the stroke, not both at the same time.

I've created a custom class with extended TextView and I am using the following methods:

Using this to draw stroke:

paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(_strokeWidth);
setTextColor(_strokeColor);

Which gives me this result:

enter image description here

Using following code to add gradient:

Shader textShader = new LinearGradient(0f, 0f, getWidth(), getTextSize(), gradientColorsArray, null, Shader.TileMode.CLAMP);
                paint.setShader(textShader);

And It gives me the following result:

enter image description here

The problem is when I combine the above two methods, The stroke is drawn but the color of the stroke is the same as the gradient I am giving to paint object.

Following is the result I want to achieve. It would be great if somebody can guide me on how to achieve desired results.

enter image description here

over 4 years ago · Santiago Trujillo
4 Respostas
Responde à pergunta

0

So after waiting for more than 4 days and lots of research, I am finally able to succeed to achieve the desired output.

The mistake I was making is while drawing a stroke on a paint object, I am setting the stroke color as textcolor. What I did this time was I created a LinearGradient() object and gave it to paint.shader while setting the paintStyle(Paint.Style.Stroke).

Paint paint = this.getPaint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5f);
        paint.setShader(new LinearGradient(0f, 0f, getTextSize(), getTextSize(), mOutlineColor, mOutlineColor, Shader.TileMode.CLAMP));

And after setting the stroke in onDraw() method of my CustomTextView class, I called the super.onDraw(canvas)

Then I create a new LinearGradient() object for the gradient colors as follow:

 Paint paint = this.getPaint();
        paint.setStyle(Paint.Style.FILL);
        Shader linearShader = new LinearGradient(0f, 0f, getWidth(), getTextSize(), colors, null,
                Shader.TileMode.CLAMP);
        paint.setShader(linearShader);

Finally calling the super.onDraw(canvas) again and this gives my textview a stroke as well as gradient as textColor.

over 4 years ago · Santiago Trujillo Relatório

0

Hola Internet para todos

about 4 years ago · Manuel saravia Relatório

0

Hola a tos amigos

about 4 years ago · Manuel saravia Relatório

0

Saludos para todos los del grupo

about 4 years ago · Manuel saravia Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda