Search code examples
androidbitmapmasking

create a mask from bitmap


I have an image as bitmap e.g enter image description here

I want to create mask programmatically from that bitmap like this

enter image description here

I searched online but did not find any solution.


Solution

  • I found a solution to my problem i solved my problem by tinting my bitmap using PorterDuffColorFilter in a following way.

        public Bitmap tintBitmap(Bitmap bitmap, int color) {
              Paint paint = new Paint();
              paint.setColorFilter(new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_IN));
              Bitmap bitmapResult = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
              Canvas canvas = new Canvas(bitmapResult);
              canvas.drawBitmap(bitmap, 0, 0, paint);
              return bitmapResult;
         }