我有一个包含未知数量自定义项目的 ListView ,在滚动期间 - 在 getView 内)我检查本地是否存在与 aConvertView 相关的图像并将其设置为 ListView 项目(aConvertView):
// out of getView(...)
Bitmap mBitmap;
BitmapFactory.Options mOptions = new BitmapFactory.Options();
mOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;
// Inside getView(...)
File file = new File(mContext.getFilesDir(), picture_file_name);
if(file.exists())
{
**// Which one has less impact on the UI thread?**
mBitmap = BitmapFactory.decodeFile(file.getPath(), mOptions);
aImageView.setImageBitmap(mBitmap);
// OR
aImageView.setImageURI(Uri.fromFile(file));
}
当然,我有滚动延迟的已知问题,并且我想知道在滚动期间设置项目的 ImageView 的最佳方法/方法。
setImageBitmap 对 UI 线程的影响是否比 setImageURI 更小?还是两者都对 UI 起作用并导致延迟。
使用 new File 、 File.exists() 是否会对 UI 线程产生影响?
不用说,我为 ListView 项目使用了支架,并为 ListView 使用了以下参数:
android:smoothScrollbar="true"
android:scrollingCache="false"
android:animationCache="false"
最佳答案
两者都在 UI 线程上运行。我想说第一个可能比第二个快一点。第二个实际上取决于 Uri 资源的来源(例如 uri 可能指向甚至未存储在手机上的远程文件)。
我认为这里的问题是是否应该在 UI 线程上加载位图,答案是否定的,尤其是在 ListView 中。 Google 在 Developers.android.com 上发布了一个关于从 UI 线程加载位图的精彩教程:http://developer.android.com/training/displaying-bitmaps/process-bitmap.html
关于Android: ListView 滚动期间 setImageBitmap 与 setImageURI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29522659/