Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rotate image using glide library? (Like in picasso)

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.

like image 579
Ravi Gadipudi Avatar asked Jan 15 '16 12:01

Ravi Gadipudi


2 Answers

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

like image 177
Camino2007 Avatar answered Nov 05 '22 16:11

Camino2007


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()

like image 25
jonny Avatar answered Nov 05 '22 17:11

jonny