2

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?

fdermishin
  • 3,519
  • 3
  • 24
  • 45
RedBounce
  • 213
  • 5
  • 14
  • 1
    Possible duplicate of [Imageview set color filter to gradient](https://stackoverflow.com/questions/37775675/imageview-set-color-filter-to-gradient) – Manohar Apr 06 '18 at 11:24
  • Does the duplicate question answer your question? If not, then post an expected output image. – Abhi Apr 06 '18 at 11:52

1 Answers1

1

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;
    }