优化
This commit is contained in:
@@ -568,11 +568,11 @@ object ReadBookConfig {
|
|||||||
1 -> {
|
1 -> {
|
||||||
val path = "bg" + File.separator + curBgStr()
|
val path = "bg" + File.separator + curBgStr()
|
||||||
val bitmap = BitmapUtils.decodeAssetsBitmap(appCtx, path, width, height)
|
val bitmap = BitmapUtils.decodeAssetsBitmap(appCtx, path, width, height)
|
||||||
BitmapDrawable(resources, bitmap?.changeSize(width, height))
|
BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height))
|
||||||
}
|
}
|
||||||
else -> {
|
else -> {
|
||||||
val bitmap = BitmapUtils.decodeBitmap(curBgStr(), width, height)
|
val bitmap = BitmapUtils.decodeBitmap(curBgStr(), width, height)
|
||||||
BitmapDrawable(resources, bitmap?.changeSize(width, height))
|
BitmapDrawable(resources, bitmap?.copyAndRecycle(width, height))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} catch (e: OutOfMemoryError) {
|
} catch (e: OutOfMemoryError) {
|
||||||
|
|||||||
@@ -210,27 +210,27 @@ object BitmapUtils {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fun Bitmap.changeSize(newWidth: Int, newHeight: Int): Bitmap {
|
/**
|
||||||
|
* 获取指定宽高的图片
|
||||||
|
*/
|
||||||
|
fun Bitmap.copyAndRecycle(newWidth: Int, newHeight: Int): Bitmap {
|
||||||
val width = this.width
|
val width = this.width
|
||||||
val height = this.height
|
val height = this.height
|
||||||
|
|
||||||
//计算压缩的比率
|
//计算压缩的比率
|
||||||
var scaleWidth = newWidth.toFloat() / width
|
val scaleWidth = newWidth.toFloat() / width
|
||||||
var scaleHeight = newHeight.toFloat() / height
|
val scaleHeight = newHeight.toFloat() / height
|
||||||
|
|
||||||
if (scaleWidth > scaleHeight) {
|
|
||||||
scaleWidth = scaleHeight
|
|
||||||
} else {
|
|
||||||
scaleHeight = scaleWidth
|
|
||||||
}
|
|
||||||
|
|
||||||
//获取想要缩放的matrix
|
//获取想要缩放的matrix
|
||||||
val matrix = Matrix()
|
val matrix = Matrix()
|
||||||
matrix.postScale(scaleWidth, scaleHeight)
|
matrix.postScale(scaleWidth, scaleHeight)
|
||||||
|
|
||||||
//获取新的bitmap
|
//获取新的bitmap
|
||||||
return Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
|
val bitmap = Bitmap.createBitmap(this, 0, 0, width, height, matrix, true)
|
||||||
|
|
||||||
|
recycle()
|
||||||
|
|
||||||
|
return bitmap
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -182,7 +182,7 @@ object QRCodeUtils {
|
|||||||
hints: Map<DecodeHintType?, Any?> = DecodeFormatManager.ALL_HINTS
|
hints: Map<DecodeHintType?, Any?> = DecodeFormatManager.ALL_HINTS
|
||||||
): Result? {
|
): Result? {
|
||||||
if (bitmap.width > reqWidth || bitmap.height > reqHeight) {
|
if (bitmap.width > reqWidth || bitmap.height > reqHeight) {
|
||||||
val bm = bitmap.changeSize(reqWidth, reqHeight)
|
val bm = bitmap.copyAndRecycle(reqWidth, reqHeight)
|
||||||
return parseCodeResult(getRGBLuminanceSource(bm), hints)
|
return parseCodeResult(getRGBLuminanceSource(bm), hints)
|
||||||
}
|
}
|
||||||
return parseCodeResult(getRGBLuminanceSource(bitmap), hints)
|
return parseCodeResult(getRGBLuminanceSource(bitmap), hints)
|
||||||
|
|||||||
Reference in New Issue
Block a user