Glide图片旋转与放大缩小

最近在项目中有用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);
    }
}

到此,就实现了图片的旋转和放大缩小功能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

假装多好123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值