Reputation: 2720
I would like to round the corner of image with 5px to show on imageview with Picasso. I have created simple class as ImageRoundCorners
in which I am using simple method to round image corners, but my code is not working, corners are not rounded.Below is my code :
file = new File(APP.DIR_APP + APP.IMAGE + "/ok.jpg");
if (file.isFile() && file.exists()) {
Uri uri = Uri.fromFile(file);
Picasso.with(this).load(uri).transform(new ImageRoundCorners()).into(fiv_image_view);
}
and ImageRoundCorners
class:
import com.squareup.picasso.Transformation;
public class ImageRoundCorners implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
Bitmap output = Bitmap.createBitmap(source.getWidth(), source
.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, source.getWidth(), source.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 50;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(source, rect, rect, paint);
return output;
}
@Override
public String key() {
return "RoundImage";
}
}
what is the problem in this code and how can i resolve that?
I am getting this error:
java.lang.IllegalStateException: Transformation RoundImage mutated input Bitmap but failed to recycle the original.
Upvotes: 2
Views: 7643
Reputation: 8574
The error message is pretty clear. You simply forgot to recycle the original Bitmap.
....
canvas.drawBitmap(source, rect, rect, paint);
source.recycle();
return output;
Just one line missing from your code! (I'm amazed at all these answers telling you to do all sorts of unrelated, uprooting solutions.)
Upvotes: 3
Reputation: 269
Picasso.with(MainActivity.this)
.load(url)
.transform(new PRoundedCornersTransformation(30, 0, PRoundedCornerTransformation.CornerType.ALL))
.into(imageView);
Upvotes: 0
Reputation: 198
You can use following library for it. https://github.com/lopspower/CircularImageView
<com.mikhaellopez.circularimageview.CircularImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/image"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"/>
Upvotes: 0
Reputation: 2877
You can use RoundedCornerTansformation from picasso like this ::
final int radius = 5;
final int margin = 5;
final Transformation transformation = new RoundedCornersTransformation(radius,margin);
Picasso.with(activity).load(uri).transform(transformation).into(fiv_image_view);
Upvotes: 1
Reputation: 704
step 1 compile 'de.hdodenhof:circleimageview:1.2.1'
in your Build.Gradle(Module:App)
step 2 In XML File
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/imageView"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_centerVertical="true"
android:src="@drawable/chetankambale" />
step 3 in Activity
// get image from drawble with rounded corner converted image
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), R.drawable.yogeshborhade);
Bitmap circularBitmap = ImageConverter.getRoundedCornerBitmap(bitmap, 100);
// set rounded corner image to imageview
ImageView circularImageView = (ImageView) layout.findViewById(R.id.imageView);
circularImageView.setImageBitmap(circularBitmap);
Upvotes: 0
Reputation: 23384
You can use this image view for rounded corners
https://github.com/siyamed/android-shape-imageview
<com.github.siyamed.shapeimageview.RoundedImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/neo"
app:siRadius="6dp"
app:siBorderWidth="6dp"
app:siBorderColor="@color/darkgray"
app:siSquare="true"/>
result
Upvotes: 2