1
0
mirror of https://github.com/wasabeef/glide-transformations.git synced 2025-10-04 18:03:21 +08:00

add CropType(Top, Center, Bottom) for CropTransformation

This commit is contained in:
wasabeef
2015-04-23 12:40:15 +09:00
parent e3c0c8d597
commit c0ab4282d9
3 changed files with 46 additions and 5 deletions

View File

@@ -31,6 +31,8 @@ public class CropTransformation implements Transformation<Bitmap> {
private int mWidth;
private int mHeight;
private CropType mCropType = CropType.CENTER;
public CropTransformation(BitmapPool pool) {
mBitmapPool = pool;
}
@@ -41,6 +43,13 @@ public class CropTransformation implements Transformation<Bitmap> {
mHeight = height;
}
public CropTransformation(BitmapPool pool, int width, int height, CropType cropType) {
mBitmapPool = pool;
mWidth = width;
mHeight = height;
mCropType = cropType;
}
@Override
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
Bitmap source = resource.get();
@@ -65,7 +74,7 @@ public class CropTransformation implements Transformation<Bitmap> {
float scaledWidth = scale * source.getWidth();
float scaledHeight = scale * source.getHeight();
float left = (mWidth - scaledWidth) / 2;
float top = (mHeight - scaledHeight) / 2;
float top = getTop(scaledHeight);
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
Canvas canvas = new Canvas(bitmap);
@@ -76,6 +85,26 @@ public class CropTransformation implements Transformation<Bitmap> {
@Override
public String getId() {
return "CropTransformation(width=" + mWidth + ", height=" + mHeight + ")";
return "CropTransformation(width=" + mWidth + ", height=" + mHeight + ", cropType="
+ mCropType + ")";
}
private float getTop(float scaledHeight) {
switch (mCropType) {
case TOP:
return 0;
case CENTER:
return (mHeight - scaledHeight) / 2;
case BOTTOM:
return mHeight - scaledHeight;
default:
return 0;
}
}
public enum CropType {
TOP,
CENTER,
BOTTOM
}
}