Crop image as square with picasso

我的梦境 提交于 2019-12-02 04:06:26

问题


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

I need following: cat one and cat two


回答1:


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.




回答2:


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">


来源:https://stackoverflow.com/questions/43281758/crop-image-as-square-with-picasso

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