简体   繁体   中英

Android round image with picasso

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.

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

在此处输入图片说明

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.)

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);

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);

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"/>
Picasso.with(MainActivity.this)
    .load(url)
    .transform(new PRoundedCornersTransformation(30, 0, PRoundedCornerTransformation.CornerType.ALL))
    .into(imageView);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM