2010-11-19 214 views
0

我使用这个代码从互联网为什么我的android应用程序运行速度变慢?

ImageView iv = new ImageView; 
URL url = new URL(address); 
InputStream content = (InputStream)url.getContent(); 
Drawable d = Drawable.createFromStream(content , "src"); 
iv.setImageDrawable(d) 

显示severl的图像,但该应用程序变得运行缓慢的原因? 互联网连接是原因吗?或者因为我输入几个输入流来显示每个图像?

+0

您是否使用线程来加载图像?当您需要从Internet上加载某些内容时,它们很有用,因为任务可能需要很长时间。 – Javi 2010-11-19 11:30:24

回答

2

您应该使用AsyncTask或使用单独的线程异步创建InputStream和Drawable.createFromStream(...),然后在完成后使用Handler更新ImageView。 AsyncTask是可取的。

http://developer.android.com/reference/android/os/AsyncTask.html

+0

你能告诉我一个简单的例子吗? – Adham 2010-11-19 11:32:29

+0

在上面链接的API页面中有一个例子,也在Android开发人员博客http://android-developers.blogspot.com/2009/05/painless-threading.html – GeekYouUp 2010-11-19 11:36:13

+0

这是否有用? http://stackoverflow.com/questions/1905399/listitem-lazyload-asynctask-image-display-problem – Adham 2010-11-19 11:38:17

相关问题