您的位置:首页 > 其它

【自用】Kotlin Bitmap 缩放 翻转

2017-06-07 00:00 204 查看
//使用Bitmap加Matrix来缩放
fun resizeImage(bitmap: Bitmap, width: Int, height: Int): Bitmap {
val bmpWidth = bitmap.width
val bmpHeight = bitmap.height

val scaleWidth = width.toFloat() / bmpWidth
val scaleHeight = height.toFloat() / bmpHeight

val matrix = Matrix()
matrix.postScale(scaleWidth, scaleHeight)

return Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true)
}


//使用Bitmap加Matrix来翻转
fun horverImage(bitmap: Bitmap, H: Boolean, V: Boolean): Bitmap {
val bmpWidth = bitmap.width
val bmpHeight = bitmap.height
val matrix = Matrix()

if (H) matrix.postScale(-1f, 1f)                    //水平翻转H

if (V) matrix.postScale(1f, -1f)                     //垂直翻转V

if (H && V) matrix.postScale(-1f, -1f)        //水平&垂直翻转HV

return Bitmap.createBitmap(bitmap, 0, 0, bmpWidth, bmpHeight, matrix, true)
//matrix.postRotate(-90);           //旋转-90度
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Kotlin Bitmap 缩放 翻转