0

I'm doing circle animation with change of positions of colors in a SweepGradient and after creation of the shader I set a local matrix for it to rotate gradient.

So my question is:

Can I reuse my created SweepGradient object to change colors positions in it or I have to create it every time when I have a new state.

Second one:

And if I have to create it every single time then I suppose I must set a local matrix every single shader as well. Must I?

AlmostAI
  • 327
  • 4
  • 14
  • you have to create it every time if you are using xml drawable but I guess if you use `GradientDrawable` then you might be able to change the color in runtime, give it a try. – hfarhanahmed Feb 06 '20 at 09:26
  • @hfarhanahmed, to set offsets of colors programmatically it's required API level 29( – AlmostAI Feb 06 '20 at 12:03
  • and that's doesn't suit me – AlmostAI Feb 06 '20 at 12:04
  • No you can create a `GradientDrawable` for APIs prior to 29. Check this post https://stackoverflow.com/questions/6115715/how-do-i-programmatically-set-the-background-color-gradient-on-a-custom-title-ba – hfarhanahmed Feb 06 '20 at 12:37
  • @hfarhanahmed, I do create GradientDrawable but as I said the method [setColors()](https://developer.android.com/reference/android/graphics/drawable/GradientDrawable.html#setColors(int%5B%5D,%20float%5B%5D)) required API level 29 – AlmostAI Feb 06 '20 at 12:45
  • and the method is the only way how to set colors positions as it seems – AlmostAI Feb 06 '20 at 12:46
  • Can't you create a new instance of `GradientDrawable` whenever you want to change the color? – hfarhanahmed Feb 06 '20 at 12:55
  • @hfarhanahmed, then I don't see any constructor which would take colors offsets – AlmostAI Feb 06 '20 at 12:57
  • `GradientDrawable gradientDrawable = new GradientDrawable( GradientDrawable.Orientation.TOP_BOTTOM, new int[] {Colors.parseColor("#FFFFFF"),Colors.parseColor("#000000")});` – hfarhanahmed Feb 06 '20 at 13:03
  • @hfarhanahmed, but there are only orientation and colors here. It doesn't accept offsets like the method setColors(colors, offsets) do – AlmostAI Feb 06 '20 at 13:06
  • No, you can't set the offset, this is the best way to handle it https://stackoverflow.com/questions/14246499/gradientdrawable-setcolors-for-older-api-levels – hfarhanahmed Feb 06 '20 at 13:23
  • @hfarhanahmed, but there's no info about colors offsets in there – AlmostAI Feb 06 '20 at 13:29
  • @hfarhanahmed, can I use reflection if I have API level less than required? – AlmostAI Feb 06 '20 at 13:31
  • @hfarhanahmed otherwise what do you mean? – AlmostAI Feb 06 '20 at 13:41
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/207397/discussion-between-hfarhanahmed-and-algernone). – hfarhanahmed Feb 07 '20 at 05:12

0 Answers0