Crop image as square with picasso

▼魔方 西西 提交于 2019-12-02 00:40:41

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.

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">
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!