Reputation: 213
In my application I want to set dynamically gradient colors to imageView
.
I can set solid color with the following code:
imageView.setColorFilter(ContextCompat.getColor(context, R.color.grayColor),
android.graphics.PorterDuff.Mode.MULTIPLY);
But I want to set gradient color instead of solid color.
How can I do it?
Upvotes: 2
Views: 2915
Reputation: 114
You can use this method after getting bitmap
public Bitmap setGradientBackground(Bitmap originalBitmap) {
int width = originalBitmap.getWidth();
int height = originalBitmap.getHeight();
Bitmap updatedBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(updatedBitmap);
canvas.drawBitmap(originalBitmap, 0, 0, null);
Paint paint = new Paint();
LinearGradient shader = new LinearGradient(0, 0, 0, height, 0xFFF0D252, 0xFFF07305, Shader.TileMode.CLAMP);
paint.setShader(shader);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawRect(0, 0, width, height, paint);
return updatedBitmap;
}
Upvotes: 1