- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个带有自定义项的 ListView,例如这个:
灰色方 block 是一个 ImageView。填充 ListView 的数据来自游标形式的数据库。但是图像并没有直接存储在数据库中,而是在SDCard中,数据库只保存了对它们的String引用。
一开始,我通过重写的 CursorAdapter 的 bindView() 回调方法将图像解码为位图:
Bitmap bmp = BitmapFactory.decodeFile(imageLocation);
holder.imageHolder.setImageBitmap(bmp);
但是 ListView 滚动非常慢。所以我阅读了 Executor 框架并实现了它,用以下代码替换了以前的代码:
ImageView imageView = holder.imageHolder;
asyncImageLoader.DisplayImage(imageLocation, imageView);
并创建 AsyncImageLoader 类。它在其构造函数中创建了一个线程池,最多有 5 个工作线程来处理发送到工作队列的 Runnable。然后,当我从自定义 CursorAdapter 调用 DisplayImage() 方法时,它会检查位置字符串是否包含 url。如果是,则将 ImageLoader Runnable 发送到线程池的工作队列。如果该位置包含“N/A”,则将默认图像设置为 ImageView。
当可用的工作线程处理 ImageLoader Runnable 时,SDCard 中的图像被解码为位图,并将 ImageDisplayer Runnable 发送到主线程的消息队列,以在 UI 中显示图像:
public class AsyncImageLoader {
ExecutorService executorService;
Handler handler = new Handler();
public AsyncImageLoader() {
this.executorService = Executors.newFixedThreadPool(5);
}
public void DisplayImage(String location, ImageView imageView) {
if(!location.matches("N/A")) {
queueImageDecoding(location, imageView);
} else {
imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.drawable.not_available));
}
}
private void queueImageDecoding(String location, ImageView imageView) {
executorService.execute(new ImageLoader(location, imageView));
}
class ImageLoader implements Runnable {
private String location;
private ImageView imageView;
public ImageLoader(String location, ImageView imageView) {
this.location = location;
this.imageView = imageView;
}
@Override
public void run() {
Bitmap bmp = BitmapFactory.decodeFile(location);
handler.post(new ImageDisplayer(bmp, imageView));
}
}
class ImageDisplayer implements Runnable {
private Bitmap bitmap;
private ImageView imageView;
public ImageDisplayer(Bitmap bitmap, ImageView imageView) {
this.bitmap = bitmap;
this.imageView = imageView;
}
@Override
public void run() {
if(bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
}
}
问题是我仍然在滚动 Laggy。如果我去掉 ImageLoader.run() 方法中的代码,滚动就完美了。该代码不应该在工作线程中处理吗?我在这里缺少什么?
更新
由于 ListView 中的 View 在滚动发生时被重用,从工作线程返回的位图在单个 ImageView 中设置了多次。所以可能的解决方案是:
我正在使用 Future 对象取消任务。它存储在标记到自定义 CursorAdapter 内的项目 View 的容器中:
public class MyCustomAdapter extends CursorAdapter {
...
public AsyncImageLoader asyncImageLoader;
private static class ViewHolder {
ImageView imageHolder;
TextView text1Holder;
TextView text2Holder;
TextView text3Holder;
Button buttonHolder;
Future<?> futureHolder;
}
public MyCustomAdapter(Context context, Cursor c, int flags) {
...
this.asyncImageLoader = new AsyncImageLoader();
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
...
return view;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
ViewHolder holder = (ViewHolder)view.getTag();
String location = ...;
ImageView imageView = holder.imageHolder;
if(holder.futureHolder == null) {
holder.futureHolder = asyncImageLoader.DisplayImage(location, imageView);
} else {
if(!holder.futureHolder.isDone())
holder.futureHolder.cancel(true);
holder.futureHolder = asyncImageLoader.DisplayImage(location, imageView);
}
...
}
}
每次重用项目 View 时,我都会检查持有者的 future 对象是否为 Done()。如果不是,我使用 Future.cancel(true) 取消任务。但是现在,问题是任务完成得太快而无法取消。如果我让工作线程休眠,比如说 1 秒,那么任务会持续足够长的时间来取消,ListView 滚动效果会更好。但我必须等待 1 秒钟才能显示图像,我不想这样。
public class AsyncImageLoader {
....
public Future<?> DisplayImage(String location, ImageView imageView) {
if(!location.matches("N/A")) {
return executorService.submit(new ImageLoader(location, imageView));
} else {
imageView.setImageDrawable(imageView.getContext().getResources().getDrawable(R.drawable.not_available));
return null;
}
}
class ImageLoader implements Runnable {
private String location;
private ImageView imageView;
public ImageLoader(String location, ImageView imageView) {
this.location = location;
this.imageView = imageView;
}
@Override
public void run() {
boolean interrupted = false;
try {
if(!Thread.currentThread().isInterrupted()) {
Thread.sleep(1000);
Bitmap bmp = BitmapFactory.decodeFile(location);
handler.post(new ImageDisplayer(bmp, imageView));
}
} catch (InterruptedException consumed) {
interrupted = true;
} finally {
if(interrupted)
Thread.currentThread().interrupt();
}
}
}
...
}
第二种解决方案是让任务完成,但在 ListView 项目已被重用时阻止设置旧位图。但我不知道该怎么做。有什么建议吗?
最佳答案
好的,最初我从网络服务获取图像,并将它们存储在 SDCard 中。从我下载的示例中,我相信该服务会返回所有具有相同尺寸的图像。错误的!其中一些比预期的要大,并且在 ImageView 中设置时会导致延迟。我只需要缩小它们。 Load a Scaled Bitmap Version into Memory
关于android - 带有 ImageView 和 Executor Framework 的滞后 Listview,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23050212/
我有一个简单的应用程序,它读取数据库,然后经过一些操作将结果写入另一个数据库。 第一行代码使用给用户的消息和屏幕日志更新 ui,然后全部包装在带有 using 和其他 try/catch 的 try/
我有一个名为activity的表,其中有一个memberId和一个时间戳。我想找出在给定的月份中有多少成员执行了一项 Activity (即-在 Activity 表中有记录),但在过去12个月中,谁
我有前三列数据。第一个列表示 id 在前一天做了某件事。我试图通过添加一个新变量“new”来从 dat 转到 dat2,该变量执行三件事: 将 yest 的值复制到前一天。但日子并不总是连续的。因此,
我有一个简单的应用程序,它读取数据库,然后经过一些操作将结果写入另一个数据库。 第一行代码使用给用户的消息和屏幕日志更新 ui,然后全部包装在带有 using 和其他 try/catch 的 try/
我有 data.frame,它显示了股票的当前出价和要价以及我当时的信号。 time bid_price ask_price signal 10:10:01.000500
我无法让网站正常运行。它有许多移动背景并使用 css-invert 过滤器。 请看这里: http://epicstudios.de/blackwhite/ 我的问题是,即使是普通计算机也无法处理移动
我创建了一个矩形对象网格并将它们添加到一个 Pane 中。每个矩形都有一个连接到它的鼠标事件监听器,它由 MouseEvent.Entered 触发器触发。当用户将鼠标移到矩形上时,处理程序只是更改矩
感觉我的笔记本电脑不允许控制台应用程序以一定的速度运行,因为我也尝试过其他应用程序,并且它们也随机滞后。我的机器不老,也不应该这样做,它具有i7-4720HQ CPU @ 2.60GHz(8 CPUs
我现在正面临这个问题。当我的页面加载 (DOM) 时,我调用一个返回 1880 张图像的函数,这些图像存储在 Steam 服务器中。 这些图像在回调之后被添加到我的 DOM 中,该回调返回我的数组响应
我正在尝试创建一个每两秒执行一次函数的应用程序。为了实现这一点,我使用 Timer.scheduledTimer 函数。问题是该函数没有按照应有的那样每两秒执行一次。通常应用程序开始时的间隔是 2 秒
我得到了这个 gps 接收器方法,它将一些数据存储到数据库中。 // GPS private void addGPSListener() { globalconstant.db
我有一个 UISwitch,它可以在切换值时更改其上方 UILabel 的文本。每隔一段时间(大约 2% 的时间)文本不会改变。标签的文本被保存到文本文件中,因此我需要准确性。由于这个问题是间歇性的,
我有一个包含用户帖子的表格 View 。每个帖子都有图片、用户名和帖子本身。刷新控件的操作是使用来自 Parse 的数据重新加载表。除了拉动刷新时的极度延迟外,一切都完美无缺。不知道是因为每个单元格里
我有一个“详细信息”页面,其中显示俱乐部的信息。该页面是一个 UIViewController,由按钮和标签组成,以实现这种外观(就像分组的小表格)。当我在设备上加载此页面时,它比我的应用程序中的任何
我有 ActionSheet 的代码,它可以连接的东西有点慢? @IBAction func showAction(_ sender: UIButton) { let actionSheetC
我的桌面应用程序滞后。我认为 java.awt.image.BufferStrategy 中有问题。 private void render() { BufferStrategy bs
你好,我有一个包含多个页面的 viewpager(使用 fragment 状态寻呼机),以及一些 png 作为这些页面的背景。我已经遵循了在 Ui 中显示位图 (http://developer.an
我在 WPF 窗体上有一个 richtextbox 控件。它有 SpellChecking.IsEnabled 设置为 true 并且 VerticalScrollBarVisibility 设置为
在我的 android 应用程序中,我将数据存储在本地 SQLite 数据库中。在这个数据库的大小小于 8-9 MB 之前,一切都很顺利;然而,一旦数据库大小约为 9 MB,它就会继续在 logcat
我正在开发一个简单的 Android 应用程序,它只有一个 Activity ,一个 WebView。它在我的手机(Android 7.1.2 Nougat 版本)上运行良好,但我收到许多用户的投诉,
我是一名优秀的程序员,十分优秀!