1 year ago

#343929

test-img

Babbara

LinearGradient as button border in Android

I'm developing a class to color my button drawer. I have a gradient class with the 6 colors I wanna use:

public class Gradients {

final int [] colors;

public Gradients(Context context) {

    this.colors = new int[]{
            context.getResources().getColor(R.color.color_gradient_3),
            context.getResources().getColor(R.color.color_gradient_4),
            context.getResources().getColor(R.color.color_gradient_5),
            context.getResources().getColor(R.color.color_gradient_2),
            context.getResources().getColor(R.color.color_gradient_0),
            context.getResources().getColor(R.color.color_gradient_1)
    };
  }
}

And in another class I have my method which colors the border:

  init {
     val gradients = Gradients(context)
     shaderHeight = context.resources.getDimensionPixelSize(R.dimen.micro_margin)
     shaderFactory = object : ShaderFactory() {
         override fun resize(width: Int, height: Int): Shader {
             return LinearGradient(
                 0f, 0f, width.toFloat(), (height - shaderHeight).toFloat(),
                 gradients.colors, null, TileMode.MIRROR)
         }
     }
     shaderBorderPaint.style = Style.STROKE
     shaderBorderPaint.strokeWidth = 6f
 }

The problem is that it mirrors the color instead of using all of them. The results is this:

enter image description here

The result I want is this, where all the colors are linearly placed, without repeting themselves:

enter image description here

How can I achieve it?

EDIT: This is the result using a SweepGradient, it only shows 2 colors:

enter image description here

java

android

xml

android-layout

linear-gradients

0 Answers

Your Answer

Accepted video resources