您当前的位置: 首页 >  ide

蓝不蓝编程

暂无认证

  • 0浏览

    0关注

    706博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

安卓通过Glide实现图片旋转

蓝不蓝编程 发布时间:2020-04-16 09:18:36 ,浏览量:0

实现方法
  1. 添加依赖
dependencies {
    ...
    implementation 'com.github.bumptech.glide:glide:4.11.0'
    kapt 'com.github.bumptech.glide:compiler:4.11.0'
}
  1. 旋转方法
/**
 * 将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)
    }
}
关注
打赏
1639405877
查看更多评论
立即登录/注册

微信扫码登录

0.0383s