通过位图解码提高BaseAdapter性能

时间:2018-06-24 21:37:57

标签: android performance gridview baseadapter

我正在尝试开发一个应用程序,供用户拍摄照片并将其保存在图库中。该图像的路径以字符串形式保存在数据库中。我有一个使用适配器并显示图像的GridView(通过使用Bitmap.decodeFile和BitMap.Options对其进行解码以使其与ImageView大小匹配)。

问题在于,适配器进行的解码需要花费很长时间,并且我的UI出现滞后,“严重”滞后约为3秒。我需要一些帮助来优化它。

我正在Kotlin中进行操作,我的OptimiseImage类如下(接着是Adapter类的getView方法):

    class OptimiseImage {
    companion object {
        fun getBitmap (iView: ImageView, imagePath: String):Bitmap {
            // Get the dimensions of the View
            val targetW = iView.getWidth()
            val targetH = iView.getHeight()

            // Get the dimensions of the bitmap
            val bmOptions = BitmapFactory.Options() // Returns Option Object
            bmOptions.inJustDecodeBounds = true // So it does not run out of memory if image is big
            BitmapFactory.decodeFile(imagePath, bmOptions) // But can still query the size of the image
            val photoW = bmOptions.outWidth
            val photoH = bmOptions.outHeight

            // Determine how much to scale down the image
            // Return the lowest out of the two based on photo orientation
            val scaleFactor = Math.min(photoW / targetW, photoH / targetH)

            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false
            bmOptions.inSampleSize = scaleFactor

            return BitmapFactory.decodeFile(imagePath, bmOptions)
        }

        fun getBitmap(height:Int, width:Int, imagePath: String):Bitmap {
            // Get the dimensions of the View
            val targetW = width
            val targetH = height

            // Get the dimensions of the bitmap
            val bmOptions = BitmapFactory.Options() // Returns Option Object
            bmOptions.inJustDecodeBounds = true // So it does not run out of memory if image is big
            BitmapFactory.decodeFile(imagePath, bmOptions) // But can still query the size of the image
            val photoW = bmOptions.outWidth
            val photoH = bmOptions.outHeight

            // Determine how much to scale down the image
            // Return the lowest out of the two based on photo orientation
            val scaleFactor = Math.min(photoW / targetW, photoH / targetH)

            // Decode the image file into a Bitmap sized to fill the View
            bmOptions.inJustDecodeBounds = false
            bmOptions.inSampleSize = scaleFactor

            return BitmapFactory.decodeFile(imagePath, bmOptions)
        }
    }
}

任何我的getView方法如下:

   override fun getView(i: Int, convertView: View?, viewGroup: ViewGroup): View {
    Log.d(TAG,"In getView")
    var cView = convertView
    val (_, name, company, _, imagePath) = mSitesData[i]

    if (convertView == null) {
        val layoutInflater = LayoutInflater.from(context)
        cView = layoutInflater.inflate(R.layout.sites_grid_layout, null)
    }
    val iView = cView!!.findViewById(R.id.imageview_cover_art) as ImageView
    val siteName = cView.findViewById(R.id.site_name) as TextView
    val siteCompany = cView.findViewById(R.id.company_name) as TextView

    if (imagePath.equals("")){
        iView.setImageResource(R.drawable.camera_item)
    } else {
        val image = OptimiseImage.getBitmap(165,165,imagePath) //How to avoid this slowing down the UI?
        iView.setImageBitmap(image)
    }
    Log.d(TAG,"Out of GetView")
    siteName.text = name
    siteCompany.text = company
    return cView
}

0 个答案:

没有答案