[英]Android: How to crop an image using Picasso?
我想使用 Android 的毕加索图像库来裁剪图像——而不是调整它的大小。
更具体地说,我想加载一个图像,然后用坐标(x,y)
、宽度w
和高度h
取一个矩形:
Original Image
------------------------------
| |
| (x,y) |
| ------------ |
| | | |
| | Cropped | |
| | Image | h |
| | | |
| | | |
| ------------ |
| w |
------------------------------
然后,我想将这个矩形的原始图像加载到ImageView
中。 我怎么做?
您可以为此使用毕加索的变换 function。 它允许您通过自己的方法对Bitmap
可绘制对象进行所需的更改:
Picasso.with(getContext())
.load(R.drawable.sample) // the image you want to load
.transform(new Transformation() {
@Override
public Bitmap transform(Bitmap source) {
Bitmap result = Bitmap.createBitmap(source,x,y,w,h); // the actual cropping
source.recycle(); // recycle the source bitmap to avoid memory problems
return result;
}
@Override
public String key() {
return x+","+y+","+w+","+h; // some id unique for the transformation you do
}
})
.into(findViewById(R.id.yourImageView); // load the cropped image into your image view
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.