0

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

Babbara
  • 448
  • 1
  • 6
  • 21
  • 1
    Does this answer your question? [Android shape border with gradient](https://stackoverflow.com/questions/20870853/android-shape-border-with-gradient) – Junaid Khalid Mar 28 '22 at 09:15
  • @JunaidKhalid unfortunately not, I need to modify the method, not the xml – Babbara Mar 28 '22 at 09:38
  • Use a concentric [`SweepGradient`](https://developer.android.com/reference/android/graphics/SweepGradient) instead. – Mike M. Mar 28 '22 at 12:01
  • 1
    @MikeM. I updated the question with the implementation of a SweepGradient, it didn't worked. – Babbara Mar 28 '22 at 13:19
  • Works for me with four colors: https://i.stack.imgur.com/t0V83.jpg. Are you sure yours is concentric? – Mike M. Mar 28 '22 at 13:34
  • Yes, I also tried using 0.5f as positions but nothing changed. – Babbara Mar 28 '22 at 14:44
  • Quick example in a `ShapeDrawable`, which seems to be what your second block is in: https://drive.google.com/file/d/10xIq4oUkairP1MtV1xLJVZgC77caNYh7/view?usp=sharing. Looks like: https://i.stack.imgur.com/reTZr.png. – Mike M. Mar 28 '22 at 15:13
  • @MikeM. tried again using you example and now it uses 3 colors instead of 6. https://imgur.com/a/MHQbwyO – Babbara Mar 29 '22 at 07:37
  • Did you mean to post some code, or something? I mean, the example I gave works automatically with as many colors as you put in the array; 3, 4, 6, 20, whatever. There's nothing there that's limiting it to 3 or any other number of colors. – Mike M. Mar 30 '22 at 00:04

0 Answers0