在android中加载位图/可绘制以查看异步的方法
我的活动必须在可见之前完成很多事情。在加载视图的过程中,我必须加载位图/可绘制对象,这会消耗一些时间并阻止当时可以执行的其他操作。所以我决定将其移至另一个线程。我使用过异步任务,但这不是加载内容的可靠方法。在某些情况下,任务会在我的视图显示后 1-2 秒开始!是否有另一种加载位图/可绘制的方法,不会消耗太多时间并允许将其分配给 ImageView?
I've got activity that has to do many things before it is visible. In proccess of loading view I have to load up bitmap/drawable that consume some time and prevents from other actions that could be done in that time. So i decide to move it to another thread. I've used Async Task, but this is not solid way of loading things. There are situations where task starts 1-2 sec after my view is shown! Is there anothe way to load bitmap/drawable that will not consume so much time and will allow to assign it to ImageView?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在执行任务之前,您可能仍然阻塞 UI 线程。使用记录器来验证任务何时执行。
如果任务确实需要花费大量时间来执行,那么您正在加载的位图可能太大,在这种情况下,您应该使用
对正在加载的图像进行下采样>inSampleSize
选项与BitmapFactory
。It is likely that you are still blocking your UI thread before executing the task. Use a logger to verify when your task is being executed.
If the task does take up a lot of time to execute then the bitmaps you are loading may be too large, in which case you should look at the down-sampling the images you are loading using the
inSampleSize
option withBitmapFactory
.AsyncTask
从线程池运行任务,该线程池可能已完全使用,因此它可能必须将任务保留在队列中,直到线程释放以运行新任务。尽管我怀疑这就是问题的原因。您也许应该考虑在要显示的实际视图之前显示进度指示器,以确保所有任务在用户看到它们之前都已完成。
编辑
你也可以使用task.get();运行所有其他任务后等待位图加载器完成后再显示任何内容...但请尝试保持 UI 响应!
AsyncTask
runs tasks from a thread pool which might already be fully in use so it might have to keep the tasks in queue until a thread frees up to run new task. Though I doubt this is the cause of the problem.You should maybe consider displaying a progress indicator before the actual view you want to show to make sure all tasks are done before the user sees them.
Edit
you could also use task.get(); after running all the other tasks to wait for the bitmap loader to complete before showing anything... but do try to keep the UI responding!