Optimal use of BitmapFactory.Options.inSampleSize for speed

前端 未结 4 1535
野的像风
野的像风 2021-02-05 05:15

Thanks to Schermvlieger for asking this question on anddev.org,

I\'m just copying his question to SO as nobody replied on th

4条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-05 06:01

    Hi try out calculating the inSampleSize using this logic

    private fun calculateInSampleSize(options: BitmapFactory.Options, reqWidth: Int, reqHeight: Int): Int {
        val (height: Int, width: Int) = options.run { outHeight to outWidth }
        var inSampleSize = 1
    
        if (height > reqHeight || width > reqWidth) {
            val halfHeight: Int = height / 2
            val halfWidth: Int = width / 2
    
            // Calculate the largest inSampleSize value that is a power of 2 and keeps both
            // height and width larger than the requested height and width.
            while (halfHeight / inSampleSize >= reqHeight && halfWidth / inSampleSize >= reqWidth) {
                inSampleSize *= 2
            }
        }
    
        return inSampleSize
    }
    

提交回复
热议问题