最近在项目中有用Glide去显示图片,其中有去实现图片旋转和当大缩小的功能,这里分享一下
图片的旋转:
首先通过一个类继承实现BitmapTransformation
public class RotateTransformation extends BitmapTransformation {
private float rotateRotationAngle = 0f;
public RotateTransformation(float rotateRotationAngle) {
this.rotateRotationAngle = rotateRotationAngle;
}
@Override
protected Bitmap transform(@NonNull BitmapPool pool, @NonNull Bitmap toTransform, int outWidth, int outHeight) {
Matrix matrix = new Matrix();
matrix.postRotate(rotateRotationAngle);
return Bitmap.createBitmap(toTransform, 0, 0, toTransform.getWidth(), toTransform.getHeight(), matrix, true);
}
@Override
public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
}
}
然后通过glide去实现旋转功能:
float rotateRotationAngle = 90f;
/**
* 旋转图片
*/
private void RorateImage() {
((PhotoPlayActivity) getActivity()).rotatePhoto(copeBitmap, rotateRotationAngle);
rotateRotationAngle += 90f;
if (rotateRotationAngle > 360f) {
rotateRotationAngle = 90f;
}
}
public void rotatePhoto(Bitmap bitmap, float rotateRotationAngle) {
Glide.with(this).load(bitmap)
.apply(RequestOptions.bitmapTransform(new RotateTransformation(rotateRotationAngle)))
.into(imageView);
}
图片的方法和缩小:
public void zoomPhoto(Bitmap bitmap, int weight, int height) {
Glide.with(getApplicationContext()).asBitmap().load(resource)
.apply(RequestOptions.overrideOf(weight, height).centerCrop())
.into(imageView);
}
});
}
private int mDeviceWidth;//设备原始宽
private int mDeviceHeight;//设备原始高
private int mZoomInWidth; //缩放后的高
private int mZoomInHeight;//缩放后的宽
double x = 0.8;缩小倍数
double y = 1.25; 放大倍数
protected void initData() {
mDeviceWidth = Utils.getDeviceWidth(MmpApplication.getApplication());//通过工具类去得到设备的宽和高,这里工具类就不展示了
mDeviceHeight = Utils.getDeviceHeight(MmpApplication.getApplication());
mZoomInHeight = mDeviceHeight;
mZoomInWidth = mDeviceWidth;
}
/**
* 缩小图片
*/
private void ZoomInImage() {
mZoomInWidth = (int) (mZoomInWidth * x);
mZoomInHeight = (int) (mZoomInHeight * x);
DebugLog.i("mZoomInWidth:" + mZoomInWidth + "mZoomInHeight:" + mZoomInHeight);
if (mZoomInWidth <= mDeviceWidth && mZoomInHeight <= mDeviceHeight) {
((PhotoPlayActivity) getActivity()).zoomPhoto(Bitmap, mZoomInWidth, mZoomInHeight);
}
}
/**
* 放大照片
*/
private void ZoomOutImage() {
mZoomInHeight = (int) (mZoomInHeight * y);
mZoomInWidth = (int) (mZoomInWidth * y);
DebugLog.i("mZoomInWidth:" + mZoomInWidth + "mZoomInHeight:" + mZoomInHeight);
if (mZoomInWidth <= mDeviceWidth && mZoomInHeight <= mDeviceHeight) {
((PhotoPlayActivity) getActivity()).zoomPhoto(Bitmap, mZoomInWidth, mZoomInHeight);
} else {
mZoomInHeight = (int) (mZoomInHeight * x);
mZoomInWidth = (int) (mZoomInWidth * x);
}
}
到此,就实现了图片的旋转和放大缩小功能。