mirror of
https://github.com/wasabeef/glide-transformations.git
synced 2025-06-09 07:54:04 +08:00
add CropType(Top, Center, Bottom) for CropTransformation
This commit is contained in:
parent
e3c0c8d597
commit
c0ab4282d9
@ -22,7 +22,9 @@ public class MainActivity extends ActionBarActivity {
|
|||||||
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
|
recyclerView.setLayoutManager(new GridLayoutManager(this, 2));
|
||||||
|
|
||||||
List<Type> dataSet = new ArrayList<>();
|
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.CropSquare);
|
||||||
dataSet.add(Type.CropCircle);
|
dataSet.add(Type.CropCircle);
|
||||||
dataSet.add(Type.ColorFilter);
|
dataSet.add(Type.ColorFilter);
|
||||||
|
@ -45,7 +45,9 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
|
|||||||
private BitmapPool mPool;
|
private BitmapPool mPool;
|
||||||
|
|
||||||
enum Type {
|
enum Type {
|
||||||
Crop,
|
CropTop,
|
||||||
|
CropCenter,
|
||||||
|
CropBottom,
|
||||||
CropSquare,
|
CropSquare,
|
||||||
CropCircle,
|
CropCircle,
|
||||||
ColorFilter,
|
ColorFilter,
|
||||||
@ -81,9 +83,17 @@ public class MainAdapter extends RecyclerView.Adapter<MainAdapter.ViewHolder> {
|
|||||||
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
|
public void onBindViewHolder(MainAdapter.ViewHolder holder, int position) {
|
||||||
Transformation<Bitmap> transformation = null;
|
Transformation<Bitmap> transformation = null;
|
||||||
switch (mDataSet.get(position)) {
|
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);
|
transformation = new CropTransformation(mPool, 300, 100);
|
||||||
break;
|
break;
|
||||||
|
case CropBottom:
|
||||||
|
transformation =
|
||||||
|
new CropTransformation(mPool, 300, 100, CropTransformation.CropType.BOTTOM);
|
||||||
|
break;
|
||||||
case CropSquare:
|
case CropSquare:
|
||||||
transformation = new CropSquareTransformation(mPool);
|
transformation = new CropSquareTransformation(mPool);
|
||||||
break;
|
break;
|
||||||
|
@ -31,6 +31,8 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
private int mWidth;
|
private int mWidth;
|
||||||
private int mHeight;
|
private int mHeight;
|
||||||
|
|
||||||
|
private CropType mCropType = CropType.CENTER;
|
||||||
|
|
||||||
public CropTransformation(BitmapPool pool) {
|
public CropTransformation(BitmapPool pool) {
|
||||||
mBitmapPool = pool;
|
mBitmapPool = pool;
|
||||||
}
|
}
|
||||||
@ -41,6 +43,13 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
mHeight = height;
|
mHeight = height;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CropTransformation(BitmapPool pool, int width, int height, CropType cropType) {
|
||||||
|
mBitmapPool = pool;
|
||||||
|
mWidth = width;
|
||||||
|
mHeight = height;
|
||||||
|
mCropType = cropType;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
public Resource<Bitmap> transform(Resource<Bitmap> resource, int outWidth, int outHeight) {
|
||||||
Bitmap source = resource.get();
|
Bitmap source = resource.get();
|
||||||
@ -65,7 +74,7 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
float scaledWidth = scale * source.getWidth();
|
float scaledWidth = scale * source.getWidth();
|
||||||
float scaledHeight = scale * source.getHeight();
|
float scaledHeight = scale * source.getHeight();
|
||||||
float left = (mWidth - scaledWidth) / 2;
|
float left = (mWidth - scaledWidth) / 2;
|
||||||
float top = (mHeight - scaledHeight) / 2;
|
float top = getTop(scaledHeight);
|
||||||
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
|
RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);
|
||||||
|
|
||||||
Canvas canvas = new Canvas(bitmap);
|
Canvas canvas = new Canvas(bitmap);
|
||||||
@ -76,6 +85,26 @@ public class CropTransformation implements Transformation<Bitmap> {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String getId() {
|
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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user