7

I have transparent images like shapes,letters which I'm fetch from gallery so I need to give them stroke/outline with black color, I can set border but It's set to whole bitmap like left,right,top and bottom.

Same thing we can do with photoshop is giving outerstroke to image but I want to achieve that in android.

I tried this and this It's give border, But what I want to do is like below sample image

Original Image without stroke

I want like this --> With stroke

Does this possible in android?

a.dev
  • 207
  • 2
  • 11

1 Answers1

3

I have an temporary solution like this

int strokeWidth = 8;
Bitmap originalBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.flower_icon);
Bitmap newStrokedBitmap = Bitmap.createBitmap(originalBitmap.getWidth() + 2 * strokeWidth, originalBitmap.getHeight() + 2 * strokeWidth, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(newStrokedBitmap);
float scaleX = newStrokedBitmap.getWidth() / originalBitmap.getWidth();
float scaleY = newStrokedBitmap.getHeight() / originalBitmap.getHeight();
Matrix matrix = new Matrix();
matrix.setScale(scaleX, scaleY);
canvas.drawBitmap(originalBitmap, matrix, null);
canvas.drawColor(Color.WHITE, PorterDuff.Mode.SRC_ATOP); //Color.WHITE is stroke color
canvas.drawBitmap(originalBitmap, strokeWidth, strokeWidth, null);

Firstly create a new bitmap with stroke size on left, right, bottom and top.

Secondly a little bit scale bitmap and draw scaled bitmap on newly created bitmap canvas.

Draw a color (your stroke color) with PorterDuff mode SRC_ATOP override original bitmap position with stroke color.

Finally draw your original bitmap on newly create bitmap canvas.

Mehmet Güngören
  • 2,383
  • 1
  • 9
  • 16