我试图用Glide旋转我的图像。我认为问题是没有调用方法transform()。因此,System.out.print永远不会显示,但同时,图像在手机上正确旋转,但Android Studio仿真器(Android 3.0.0,API 26)都没有旋转。我使用的是Glide 4.7.1
我希望你能帮助我:
BitmapTransformation:
public class RotateTransformation extends BitmapTransformation {
private static final String ID = "com.example.app.aim_app.RotateTransformation";
private static final byte[] ID_BYTES = ID.getBytes(Charset.forName("UTF-8"));
private final float rotateRotationAngle;
public RotateTransformation(final float rotateRotationAngle) {
this.rotateRotationAngle = rotateRotationAngle;
}
@Override
public Bitmap transform(BitmapPool pool, Bitmap toTransform, int outWidth, int outHeight) {
if (toTransform.getWidth() == outWidth && toTransform.getHeight() == outHeight) {
System.out.print("notTransform: " + outWidth + " " + outHeight + " " + toTransform.getWidth() + " " + toTransform.getHeight());
return toTransform;
}
System.out.print("transform: " + outWidth + " " + outHeight + " " + toTransform.getWidth() + " " + toTransform.getHeight());
Matrix matrix = new Matrix();
matrix.postRotate(rotateRotationAngle);
return Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);
}
@Override
public boolean equals(Object o) {
if (o instanceof RotateTransformation) {
RotateTransformation other = (RotateTransformation) o;
return rotateRotationAngle == other.rotateRotationAngle;
}
return false;
}
@Override
public int hashCode() {
return Util.hashCode(ID.hashCode(), Util.hashCode(rotateRotationAngle));
}
@Override
public void updateDiskCacheKey(MessageDigest messageDigest) {
messageDigest.update(ID_BYTES);
byte[] radiusData = ByteBuffer.allocate(4).putFloat(rotateRotationAngle).array();
messageDigest.update(radiusData);
}
使用RotateTransformation
if (....) {
GlideApp.with(ActivityManager.getCurrentActivity()).load(resource).transform(new RotateTransformation(180)).into(imageView);
} else {
GlideApp.with(ActivityManager.getCurrentActivity()).load(resource).transform(new RotateTransformation(90)).into(imageView);
}