Android 利用位图解码提高BaseAdapter性能
我正在尝试开发一个应用程序,用户可以在其中拍照并将其保存在图库中。该图像的路径以字符串形式保存在数据库中。我有一个GridView,它使用适配器并显示图像(通过使用Bitmap.decodeFile和Bitmap.Options对图像进行解码,使其与ImageView大小相匹配) 问题是,适配器的解码需要很长时间,我的UI会出现3秒左右的“严重”延迟。我需要一些帮助来优化它 我在Kotlin中执行此操作,我的OptimizeImage类如下所示(后面是适配器类中的getView方法): 任何my getView方法如下所示:Android 利用位图解码提高BaseAdapter性能,android,performance,gridview,baseadapter,Android,Performance,Gridview,Baseadapter,我正在尝试开发一个应用程序,用户可以在其中拍照并将其保存在图库中。该图像的路径以字符串形式保存在数据库中。我有一个GridView,它使用适配器并显示图像(通过使用Bitmap.decodeFile和Bitmap.Options对图像进行解码,使其与ImageView大小相匹配) 问题是,适配器的解码需要很长时间,我的UI会出现3秒左右的“严重”延迟。我需要一些帮助来优化它 我在Kotlin中执行此操作,我的OptimizeImage类如下所示(后面是适配器类中的getView方法): 任何my
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
}
当有像这样的库为您执行所有这些操作并且不会导致任何减速时,您手动执行这些操作有什么特别的原因吗?正如@ianhanniballake所提到的,库为您省去了参与解码的麻烦,它们经过了大量优化并且数量已知。然而,你的问题也没有错。考虑异步加载图像-这不会阻塞UI线程。但是,这将引入一些额外的复杂性,因为您只想在仍然需要的情况下“发布”结果位图(视图仍在屏幕上)。我建议使用回调进行异步加载,如果视图被滚动出视图,可以删除回调,您可能还需要某种LRU缓存,再加上一个滑动缓存。使用它,谢谢!我可以看到大多数使用Glide作为Http请求类型或直接使用位图的示例。有人能帮我找到解码位图的例子吗?我是否仍然使用适配器,或者Glide可以作为适配器使用,或者我的适配器应该使用Glide?
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
}