I have a image with sharp edges.
the tile_mode.xml
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/background"
android:tileMode="repeat">
</bitmap>
the back.xml
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/tile_mode" />
<item>
<shape>
<solid/>
<stroke android:width="1dip" android:color="#225786" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
layout.xml
<LinearLayout
android:id="@+id/frame1"
android:background="@drawable/back"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
I am setting the image as a background to this layout and drawing a border to it but the problem is the image is square with sharp edges and the border which I am drawing in the xml is rounded corners. So how to make the image also with rounded corners?
Pass original bitmap to the following function and you will get a rounded bitmap as result :) . Hope this helps someone.
public Bitmap getRoundedBitmap(Bitmap bitmap) {
Bitmap resultBitmap;
int originalWidth = bitmap.getWidth();
int originalHeight = bitmap.getHeight();
float r;
if (originalWidth > originalHeight) {
resultBitmap = Bitmap.createBitmap(originalHeight, originalHeight,
Bitmap.Config.ARGB_8888);
r = originalHeight / 2;
} else {
resultBitmap = Bitmap.createBitmap(originalWidth, originalWidth,
Bitmap.Config.ARGB_8888);
r = originalWidth / 2;
}
Canvas canvas = new Canvas(resultBitmap);
final Paint paint = new Paint();
final Rect rect = new Rect(ConstsCore.ZERO_INT_VALUE,
ConstsCore.ZERO_INT_VALUE, originalWidth, originalHeight);
paint.setAntiAlias(true);
canvas.drawARGB(ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE,
ConstsCore.ZERO_INT_VALUE, ConstsCore.ZERO_INT_VALUE);
canvas.drawCircle(r, r, r, paint);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
canvas.drawBitmap(bitmap, rect, rect, paint);
return resultBitmap;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With