39

I know there are lots of link available to make ImageView Round Corner. But I'm Using Picasso Library for Image Loading.. I refer the link to get result. But the Problem is that I'm Using it in ListView and for the LIstView's first item ImageView its working perfectly fine but for the remaining once transformation is not working.

Akshay
  • 5,453
  • 7
  • 38
  • 58
  • Have you tried this transformation? https://gist.github.com/aprock/6213395 Usage: Picasso.with(context).load(url).transform(new RoundedTransformation()).into(imageView) – Roi Divon Jun 08 '15 at 08:45
  • @RoiDivon I just used link you provided, But it also giving the the same effect.. – Akshay Jun 08 '15 at 08:58

7 Answers7

57

I am using this transformation: https://gist.github.com/julianshen/5829333

Picasso.with(activity).load(url).transform(new CircleTransform()).into(imageView);
dasar
  • 5,131
  • 3
  • 22
  • 35
  • 4
    Superb!!! Thanks a lot...I just replace `canvas.drawCircle(r, r, r, paint);` with `canvas.drawRoundRect(new RectF(0, 0, source.getWidth(), source.getHeight()), r, r, paint);` to get round corner rect – Akshay Jun 08 '15 at 11:45
  • 2
    You forgot to say that the r needs to be changed for this to work, otherwise it makes the circle from RoundRect because the radius is too big. – stevyhacker Feb 06 '16 at 13:25
  • 1
    And both forgot to say that you need to change r for 0 and size.getHeight() for it to work (in my case). This is the final code: `canvas.drawRoundRect(new RectF(0, 0, source.getWidth(), source.getHeight()), 0, source.getHeight(), paint);` – Pablo Quemé Jan 04 '18 at 21:34
36

You can use RoundedCornersTransformation class of picasso-transformations library.

Example :

final int radius = 5;
final int margin = 5;
final Transformation transformation = new RoundedCornersTransformation(radius, margin);
Picasso.with(activity).load(url).transform(transformation).into(imageView);
Stéphane B.
  • 3,080
  • 2
  • 27
  • 35
  • 16
    if we set `android:scaleType="centerCrop"` to the `ImageView` and image is zoomed to fill out the view, then your method doesn't take any effect because rounded corners are out of view – Choletski Sep 21 '16 at 08:17
  • @Choletski use also CropTransformation(int width, int height) before RoundedCornersTransformation – NickUnuchek Sep 18 '17 at 13:24
  • 1
    @Choletski, probably `centerCrop()` may help: `Picasso.with(getContext()).load(uri).centerCrop().resize(width, height).transform(transformation).into(imageView);`. – CoolMind Feb 14 '18 at 16:36
29

You can use this class to make rounded corners rectangle image view with Picasso, use it like this

 Picasso.with(activity).load(url).transform(new RoundedCornersTransform(this)).into(imageView);

Here is the class RoundedCornersTransform.

package com.demo.picasso;

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;

import com.squareup.picasso.Transformation;


public class RoundedCornersTransform implements Transformation {
@Override
public Bitmap transform(Bitmap source) {
    int size = Math.min(source.getWidth(), source.getHeight());

    int x = (source.getWidth() - size) / 2;
    int y = (source.getHeight() - size) / 2;

    Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
    if (squaredBitmap != source) {
        source.recycle();
    }

    Bitmap bitmap = Bitmap.createBitmap(size, size, source.getConfig());

    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    BitmapShader shader = new BitmapShader(squaredBitmap, BitmapShader.TileMode.CLAMP, BitmapShader.TileMode.CLAMP);
    paint.setShader(shader);
    paint.setAntiAlias(true);

    float r = size / 8f;
    canvas.drawRoundRect(new RectF(0, 0, source.getWidth(), source.getHeight()), r, r, paint);
    squaredBitmap.recycle();
    return bitmap;
}

@Override
public String key() {
    return "rounded_corners";
  }
}
stevyhacker
  • 1,646
  • 1
  • 19
  • 29
  • This is short n perfect solution in all posts about rounded corner transform on SO. Thanks a lot :) – Atul May 02 '16 at 11:48
  • 3
    How do I use this @stevyhacker to make only the top two corners rounded... I cannot understand what part I am suppose to change? – Lion789 Oct 19 '16 at 22:50
  • 3
    @Lion789: In the [picasso-transformations library](https://github.com/wasabeef/picasso-transformations/blob/master/transformations/src/main/java/jp/wasabeef/picasso/transformations/RoundedCornersTransformation.java) mentionded below, it is possible to exactly set the corners that you want rounded, e.g: Picasso .with(mContext) .load(mUrl) .resize(200, 200) .centerCrop() .transform( new RoundedCornersTransformation(2, 0, RoundedCornersTransformation.CornerType.LEFT) ) .into(mImageView); – ʕ ᵔᴥᵔ ʔ Jan 09 '17 at 08:33
  • @Lion789 I got all the rounded corners with the code in [this answer](http://stackoverflow.com/a/42380519/2819864). – RominaV Feb 22 '17 at 00:35
10

i used RoundedCornersTransformationclass of picasso-transformationslibrary. I had custom adapter with view holder pattern in my listview. I added below dependency in my build.gradle:

dependencies {
        compile 'jp.wasabeef:picasso-transformations:2.1.0'
} 

And in my customArrayAdapter.java,i added:

Picasso.with(getContext()).load(path).transform(new RoundedCornersTransformation(10,10)).resize(175,300).into(viewHolder.ivImage);
This would both resize and give rounded corners to you images.

Anirudh Sharma
  • 7,808
  • 13
  • 36
  • 40
DISHA
  • 141
  • 1
  • 3
6

Like said here. You can use MaskTransformationclass of picasso-transformations library.

Example :

final Transformation transformation = new MaskTransformation(getContext(), R.drawable.rounded_convers_transformation);
Picasso.with(activity).load(url).transform(transformation).into(imageView);

res/drawable/rounded_convers_transformation.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <corners android:radius="5dp"/>
    <solid android:color="@color/black"/>
</shape>

UPDATE: But notice that you should also .resize(w,h) the image, because if image will be large the round will not be determinatable

NickUnuchek
  • 8,369
  • 9
  • 74
  • 111
  • Nice solution. Also note that before you apply this transformation `ImageView` must be visible (in other case no transformation will be applied). And yes, you can write `Picasso.with(getContext()).load(uri).centerCrop().resize(width, height).transform(transformation).into(imageView);` for cropped images. – CoolMind Feb 21 '18 at 07:33
3

Following @stevyhacker's answer and this related one, I came up with this:

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.squareup.picasso.Transformation;


public class RoundedCornersTransform implements Transformation {
    private static Bitmap createRoundedRectBitmap(Bitmap bitmap,
                                                  float topLeftCorner, float topRightCorner,
                                                  float bottomRightCorner, float bottomLeftCorner) {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = Color.WHITE;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        Path path = new Path();
        float[] radii = new float[]{
                topLeftCorner, bottomLeftCorner,
                topRightCorner, topRightCorner,
                bottomRightCorner, bottomRightCorner,
                bottomLeftCorner, bottomLeftCorner
        };

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        path.addRoundRect(rectF, radii, Path.Direction.CW);
        canvas.drawPath(path, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
        return output;
    }

    @Override
    public Bitmap transform(Bitmap source) {
        int size = Math.min(source.getWidth(), source.getHeight());

        int x = (source.getWidth() - size) / 2;
        int y = (source.getHeight() - size) / 2;

        Bitmap squaredBitmap = Bitmap.createBitmap(source, x, y, size, size);
        if (squaredBitmap != source) {
            source.recycle();
        }

        float r = size / 4f;

        Bitmap roundedBitmap = createRoundedRectBitmap(squaredBitmap, r, r, r, r);

        squaredBitmap.recycle();

        return roundedBitmap;
    }

    @Override
    public String key() {
        return "rounded_corners";
    }
}

Use it like this:

Picasso.with(context).load(url).transform(new RoundedCornersTransform()).into(imageView);

Probably needs some enhancements though, so watch out!

Community
  • 1
  • 1
RominaV
  • 3,085
  • 1
  • 26
  • 52
2

Kotlin version of RoundCornerTransform based on @stevyhacker 's answer.

  • Support rectangle instead of square image.
  • No extra 3rd party library needed.

=============================================

import android.graphics.*
import com.squareup.picasso.Transformation

class RoundCornersTransform(private val radiusInPx: Float) : Transformation {

    override fun transform(source: Bitmap): Bitmap {
        val bitmap = Bitmap.createBitmap(source.width, source.height, source.config)
        val canvas = Canvas(bitmap)
        val paint = Paint(Paint.ANTI_ALIAS_FLAG or Paint.DITHER_FLAG)
        val shader = BitmapShader(source, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP)
        paint.shader = shader
        val rect = RectF(0.0f, 0.0f, source.width.toFloat(), source.height.toFloat())
        canvas.drawRoundRect(rect, radiusInPx, radiusInPx, paint)
        source.recycle()

        return bitmap
    }

    override fun key(): String {
        return "round_corners"
    }

}

Usage:

    Picasso.get()
        .load(imageUrl)
        .memoryPolicy(MemoryPolicy.NO_CACHE)
        .placeholder(R.drawable.image_placeholder)
        .transform(RoundCornersTransform(32.0f))
        .into(imageView, callback)
Chandler
  • 1,964
  • 1
  • 15
  • 24