仿抖音短视频源码安卓通过Glide实现图片旋转

实现方法
1.添加依赖

dependencies {
    ...
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    kapt 'com.github.bumptech.glide:compiler:4.11.0'
}

2旋转方法

/**
 * 将imageView里显示的图片旋转指定的角度
 * @param imageView 显示图片的imageView控件
 * @param rotateRotationAngle 旋转角度
 */
private fun rotateImage(context: Context, imageView: ImageView, rotateRotationAngle: Float) {
    Glide.with(context).load(url)
            .apply(RequestOptions.bitmapTransform(RotateTransformation(rotateRotationAngle)))
            .into(imageView)
}

/**
 * 加载指定url的图片并进行旋转
 * @param imageView 显示图片的imageView控件
 * @param url 图片url
 * @param rotateRotationAngle 旋转角度
 */
private fun rotateImage(context: Context, imageView: ImageView, url: String, rotateRotationAngle: Float) {
    Glide.with(context).load(imageView.drawable)
            .apply(RequestOptions.bitmapTransform(RotateTransformation(rotateRotationAngle)))
            .into(imageView)
}
/**
 * 图片旋转处理类
 */
class RotateTransformation(private var rotateRotationAngle: Float) : BitmapTransformation() {
    override fun updateDiskCacheKey(messageDigest: MessageDigest) {
    }

    override fun transform(pool: BitmapPool, toTransform: Bitmap, outWidth: Int, outHeight: Int): Bitmap {
        val matrix = Matrix()
        matrix.postRotate(rotateRotationAngle)
        return Bitmap.createBitmap(toTransform, 0, 0, toTransform.width, toTransform.height, matrix, true)
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值