Issue
I would like to set my ImageView to a SweepGradient.
Here's what I tried:
protected void onCreate(@Nullable Bundle savedInstanceState) {
ImageView colorPicker = findViewById(R.id.color_picker);
colorPicker.setImageDrawable(CreateColorPickerDrawable());
}
private PaintDrawable CreateColorPickerDrawable()
{
int[] colors = {0xFFFF0000, 0xFF00FF00, 0xFF0000FF};
PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setCornerRadius(getResources().getDimension(R.dimen.corner_radius));
SweepGradient sweepGradient = new SweepGradient(50, 50, colors, null);
paintDrawable.getPaint().setShader(sweepGradient);
return paintDrawable;
}
But no gradient appears.
I've also seen this: Imageview set color filter to gradient
But I'm thinking there's got to be a simpler solution than that (plus it requires a bitmap src and I just want my ImageView to be a rectangle w/ rounded corners [which can be done easily w/ the PaintDrawable]).
If anyone has any guidance/advice it would be much appreciated! Ty!
Solution
I was being a derp. I just had to switch the ImageView to a normal View and then I could replace the background drawable to my paintDrawable.
protected void onCreate(@Nullable Bundle savedInstanceState) {
View colorPicker = findViewById(R.id.color_picker);
colorPicker.setBackground(CreateColorPickerDrawable());
}
private PaintDrawable CreateColorPickerDrawable() {
int[] colors = {0xFFFF0000, 0xFF00FF00, 0xFF0000FF};
PaintDrawable paintDrawable = new PaintDrawable();
paintDrawable.setCornerRadius(getResources().getDimension(R.dimen.corner_radius));
SweepGradient sweepGradient = new SweepGradient(50, 50, colors, null);
paintDrawable.getPaint().setShader(sweepGradient);
return paintDrawable;
}
Answered By - Beks_Omega
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.