I want to use the Picasso Image library for Android to crop an image – not resize it.
To be more specific, I want to load an image and then take a rectangular piece of it with coordinates (x,y)
, width w
, and height h
:
Original Image
------------------------------
| |
| (x,y) |
| ------------ |
| | | |
| | Cropped | |
| | Image | h |
| | | |
| | | |
| ------------ |
| w |
------------------------------
Then, I want to load this rectangular piece of the original image into an ImageView
. How do I do that?
You can use Picasso's transform function for that. It allows you to pass your own method to make the required changes to the Bitmap
drawable:
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