1
0
mirror of https://github.com/wasabeef/glide-transformations.git synced 2025-06-08 23:44:04 +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

@ -22,7 +22,9 @@ public class MainActivity extends ActionBarActivity {
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
List<Type> dataSet = new ArrayList<>();
dataSet.add(Type.Crop);
dataSet.add(Type.CropTop);
dataSet.add(Type.CropCenter);
dataSet.add(Type.CropBottom);
dataSet.add(Type.CropSquare);
dataSet.add(Type.CropCircle);
dataSet.add(Type.ColorFilter);

View File

@ -45,7 +45,9 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
private BitmapPool mPool;
enum Type {
Crop,
CropTop,
CropCenter,
CropBottom,
CropSquare,
CropCircle,
ColorFilter,
@ -81,9 +83,17 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
Transformation<Bitmap> transformation = null;
switch (mDataSet.get(position)) {
case Crop:
case CropTop:
transformation =
new CropTransformation(mPool, 300, 100, CropTransformation.CropType.TOP);
break;
case CropCenter:
transformation = new CropTransformation(mPool, 300, 100);
break;
case CropBottom:
transformation =
new CropTransformation(mPool, 300, 100, CropTransformation.CropType.BOTTOM);
break;
case CropSquare:
transformation = new CropSquareTransformation(mPool);
break;

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