Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Border over a bitmap with rounded corners in Android

Tags:

I used the below to make a bitmap with rounded corners. Now I want to draw a line around the bitmap.

private BitmapDrawable roundCornered(BitmapDrawable scaledBitmap, int i) {          Bitmap bitmap = scaledBitmap.getBitmap();          result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),                 Bitmap.Config.ARGB_8888);         canvas = new Canvas(result);          color = 0xff424242;         paint = new Paint();         rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());         rectF = new RectF(rect);         roundPx = i;         paint.setAntiAlias(true);         canvas.drawARGB(0, 0, 0, 0);         paint.setColor(Color.BLUE);         canvas.drawRoundRect(rectF, roundPx, roundPx, paint);          paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));         canvas.drawBitmap(bitmap, rect, rect, paint);         BitmapDrawable finalresult = new BitmapDrawable(result);         return finalresult;     } 

I got the image below, but my actual need is that I must draw a border around the image.

like image 250
Aerrow Avatar asked Jun 13 '12 10:06

Aerrow


People also ask

How do I round corners in Android?

xml file and add an attribute to that TextView, for which you want to add rounded corners. The attribute is android: background=”@drawable/rounded_corner_view”.


1 Answers

I put the following together for myself.

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips, Context context) {     Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),             Bitmap.Config.ARGB_8888);     Canvas canvas = new Canvas(output);      final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,             context.getResources().getDisplayMetrics());     final int cornerSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) cornerDips,             context.getResources().getDisplayMetrics());     final Paint paint = new Paint();     final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());     final RectF rectF = new RectF(rect);      // prepare canvas for transfer     paint.setAntiAlias(true);     paint.setColor(0xFFFFFFFF);     paint.setStyle(Paint.Style.FILL);     canvas.drawARGB(0, 0, 0, 0);     canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);      // draw bitmap     paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));     canvas.drawBitmap(bitmap, rect, rect, paint);      // draw border     paint.setColor(color);     paint.setStyle(Paint.Style.STROKE);     paint.setStrokeWidth((float) borderSizePx);     canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);      return output; } 

Credits, of course, to http://ruibm.com/?p=184

like image 200
snodnipper Avatar answered Oct 12 '22 07:10

snodnipper