问题
I am trying to rotate the image using glide library. Previously, was able to do with Picasso (due to an issue, I moved to glide). Now I am missing rotate functionality in glide. I tried using transformations but didn't work.
// Code used
public class MyTransformation extends BitmapTransformation {
private float rotate = 0f;
public MyTransformation(Context context, float rotate) {
super(context);
this.rotate = rotate;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform,
int outWidth, int outHeight) {
return rotateBitmap(toTransform, rotate);
}
@Override
public String getId() {
return "com.example.helpers.MyTransformation";
}
public static Bitmap rotateBitmap(Bitmap source, float angle)
{
Matrix matrix = new Matrix();
matrix.postRotate(angle);
return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}
}
// glide
Glide.with(context)
.load(link)
.asBitmap()
.transform(new MyTransformation(context, 90))
.into(imageView);
Thanks in advance.
回答1:
Maybe you found a solution already by yourself, if not, maybe this could help you. I use this snippet for rotate images which I get from the camera.
public MyTransformation(Context context, int orientation) {
super(context);
mOrientation = orientation;
}
@Override
protected Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
int exifOrientationDegrees = getExifOrientationDegrees(mOrientation);
return TransformationUtils.rotateImageExif(toTransform, pool, exifOrientationDegrees);
}
private int getExifOrientationDegrees(int orientation) {
int exifInt;
switch (orientation) {
case 90:
exifInt = ExifInterface.ORIENTATION_ROTATE_90;
break;
// other cases
default:
exifInt = ExifInterface.ORIENTATION_NORMAL;
break;
}
return exifInt;
}
and how to use it:
Glide.with(mContext)
.load(//your url)
.asBitmap()
.centerCrop()
.transform(new MyTransformation(mContext, 90))
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.into(//your view);
for more cases or exif int values, check the public constants in android.media.ExifInterface
回答2:
One way I found was this.
Glide.with(mCtx)
.load(uploadImage.getImageUrl())
.into(holder.imageView);
holder.imageView.setRotation(uploadImage.getmRotation());
I hope you understand.
Just take the file that you put inside the .into(x)
method
and then write x.setRotaion()
来源:https://stackoverflow.com/questions/34811415/how-to-rotate-image-using-glide-library-like-in-picasso