【问题标题】:How to rotate image using glide library? (Like in picasso)如何使用滑动库旋转图像? (就像毕加索一样)
【发布时间】:2016-01-15 12:46:19
【问题描述】:

我正在尝试使用 glide 库来旋转图像。以前,可以和毕加索一起做(由于一个问题,我搬到了滑翔)。现在我在滑行中缺少旋转功能。我尝试使用转换,但没有成功。

// 使用的代码

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.with(context)
                .load(link)
                .asBitmap()
                .transform(new MyTransformation(context, 90))
                .into(imageView);

提前致谢。

【问题讨论】:

    标签: android android-glide


    【解决方案1】:

    也许您自己已经找到了解决方案,如果没有,也许这可以帮助您。我使用这个 sn-p 来旋转从相机获取的图像。

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

    以及如何使用它:

       Glide.with(mContext)
                .load(//your url)
                .asBitmap()
                .centerCrop()
                .transform(new MyTransformation(mContext, 90))
                .diskCacheStrategy(DiskCacheStrategy.RESULT)
                .into(//your view);
    

    更多情况或 exif int 值,请查看 android.media.ExifInterface 中的公共常量

    【讨论】:

    • 谢谢。我会测试一下。
    【解决方案2】:

    我找到的一种方法是这样的。

    Glide.with(mCtx)
                .load(uploadImage.getImageUrl())
                .into(holder.imageView);
        holder.imageView.setRotation(uploadImage.getmRotation());
    

    希望你能理解。 只需将您放入 .into(x) 方法中的文件即可 然后写x.setRotaion()

    【讨论】:

    • 设置图像后旋转的好主意
    • 这对我来说是一个更好的解决方案。因为直接在图像上使用变换而不是 90 度的倍数,图像会改变它的大小。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-09-03
    • 2021-07-26
    • 1970-01-01
    • 2015-11-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多