仿抖音短视频源码安卓通过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)
    }
}

你可能感兴趣的:(技术类,android,java,计算机视觉,python,安卓)