4

How can I crop image to square using picasso library on Android?

I need following: cat one

and I also need cat two

Maifee Ul Asad
  • 3,992
  • 6
  • 38
  • 86
Andrew Evtukhov
  • 423
  • 1
  • 6
  • 17
  • This question has been answered here, maybe it can help : http://stackoverflow.com/questions/30134438/picasso-crop-to-a-view – rontho Apr 07 '17 at 15:32

2 Answers2

3

The following project provides a lot of different transformations for Picasso

https://github.com/wasabeef/picasso-transformations

The one you are interested is named CropSquareTransformation and you can apply it by using the following code

Picasso.with(mContext)
       .load(R.drawable.demo)
       .transform(transformation)
       .transform(new CropSquareTransformation())
       .into(holder.image);

You could add the dependency or copy and paste the classes you need.

Robert Estivill
  • 12,369
  • 8
  • 43
  • 64
0

Using a custom imageview:

public class SquareImageView extends android.support.v7.widget.AppCompatImageView {
    public SquareImageView(Context context) {
        super(context);
    }

    public SquareImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SquareImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        setMeasuredDimension(getMeasuredWidth(), getMeasuredWidth()); //Snap to width
    }
}

In your xml:

 <com.my.package.SquareImageView
  android:layout_width="match_parent"
  android:layout_height="wrap_content">
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110