refactor(BitmapUtils.kt):minify code

pull/1786/head
Xwite 3 years ago
parent b7d1236069
commit 236aa2bf24
  1. 75
      app/src/main/java/io/legado/app/utils/BitmapUtils.kt

@ -27,7 +27,7 @@ object BitmapUtils {
* @return * @return
*/ */
@Throws(IOException::class) @Throws(IOException::class)
fun decodeBitmap(path: String, width: Int, height: Int): Bitmap { fun decodeBitmap(path: String, width: Int, height: Int? = null): Bitmap {
val fis = FileInputStream(path) val fis = FileInputStream(path)
return fis.use { return fis.use {
@ -35,44 +35,35 @@ object BitmapUtils {
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
op.inJustDecodeBounds = true op.inJustDecodeBounds = true
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
//获取比例大小 op.inSampleSize = calculateInSampleSize(op, width, height)
val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
val hRatio = ceil((op.outHeight / height).toDouble()).toInt()
//如果超出指定大小,则缩小相应的比例
if (wRatio > 1 && hRatio > 1) {
if (wRatio > hRatio) {
op.inSampleSize = wRatio
} else {
op.inSampleSize = hRatio
}
}
op.inJustDecodeBounds = false op.inJustDecodeBounds = false
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) BitmapFactory.decodeFileDescriptor(fis.fd, null, op)
} }
} }
@Throws(IOException::class) /**
fun decodeBitmap(path: String, width: Int): Bitmap { *计算 InSampleSize缺省返回1
* @param options BitmapFactory.Options,
val fis = FileInputStream(path) * @param width 想要显示的图片的宽度
* @param height 想要显示的图片的高度
return fis.use { * @return
val op = BitmapFactory.Options() */
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; private fun calculateInSampleSize(
op.inJustDecodeBounds = true options: BitmapFactory.Options,
width: Int? = null,
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) height: Int? = null
): Int {
//获取比例大小 //获取比例大小
val wRatio = ceil((op.outWidth / width).toDouble()).toInt() val wRatio = width?.let { ceil((options.outWidth / it).toDouble()).toInt() } ?: -1
val hRatio = height?.let { ceil((options.outHeight / it).toDouble()).toInt() } ?: -1
//如果超出指定大小,则缩小相应的比例 //如果超出指定大小,则缩小相应的比例
if (wRatio > 1) { return when {
op.inSampleSize = wRatio wRatio > 1 && hRatio > 1 -> max(wRatio, hRatio)
} wRatio > 1 -> wRatio
op.inJustDecodeBounds = false hRatio > 1 -> hRatio
BitmapFactory.decodeFileDescriptor(fis.fd, null, op) else -> 1
} }
} }
/** 从path中获取Bitmap图片 /** 从path中获取Bitmap图片
@ -118,17 +109,7 @@ object BitmapUtils {
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
op.inJustDecodeBounds = true op.inJustDecodeBounds = true
BitmapFactory.decodeResource(context.resources, resId, op) //获取尺寸信息 BitmapFactory.decodeResource(context.resources, resId, op) //获取尺寸信息
//获取比例大小 op.inSampleSize = calculateInSampleSize(op, width, height)
val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
val hRatio = ceil((op.outHeight / height).toDouble()).toInt()
//如果超出指定大小,则缩小相应的比例
if (wRatio > 1 && hRatio > 1) {
if (wRatio > hRatio) {
op.inSampleSize = wRatio
} else {
op.inSampleSize = hRatio
}
}
op.inJustDecodeBounds = false op.inJustDecodeBounds = false
return BitmapFactory.decodeResource(context.resources, resId, op) return BitmapFactory.decodeResource(context.resources, resId, op)
} }
@ -154,17 +135,7 @@ object BitmapUtils {
// inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight; // inJustDecodeBounds如果设置为true,仅仅返回图片实际的宽和高,宽和高是赋值给opts.outWidth,opts.outHeight;
op.inJustDecodeBounds = true op.inJustDecodeBounds = true
BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息 BitmapFactory.decodeStream(inputStream, null, op) //获取尺寸信息
//获取比例大小 op.inSampleSize = calculateInSampleSize(op, width, height)
val wRatio = ceil((op.outWidth / width).toDouble()).toInt()
val hRatio = ceil((op.outHeight / height).toDouble()).toInt()
//如果超出指定大小,则缩小相应的比例
if (wRatio > 1 && hRatio > 1) {
if (wRatio > hRatio) {
op.inSampleSize = wRatio
} else {
op.inSampleSize = hRatio
}
}
inputStream = context.assets.open(fileNameInAssets) inputStream = context.assets.open(fileNameInAssets)
op.inJustDecodeBounds = false op.inJustDecodeBounds = false
BitmapFactory.decodeStream(inputStream, null, op) BitmapFactory.decodeStream(inputStream, null, op)

Loading…
Cancel
Save