android异步更新UI
2011-02-24 11:50
232 查看
android下面图片更新是需要启动多个子线程来进行的,而android下面是并不是线程安全的,所以thread这里是用不了的,只能用runnable接口。
废话不多说了 直接上代码。
1、下载线程 继承runnable接口
public class DownloadImage implements Runnable {
private ImageView imageView;
private String imageUrl;
private Bitmap bitmap;
//构造的时候传入要更新的ImageView ,同时传入图片的URL
public DownloadImage(ImageView imageView, String imageUrl) {
super();
this.imageView = imageView;
this.imageUrl = imageUrl;
}
public Handler handler = new Handler();
Runnable updateResults = new Runnable() {
@Override
public void run() {
updateUI();
}
};
public void run() {
HttpGet httpRequest = null;
URL url;
try {
url = new URL(imageUrl);
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bitmap = BitmapFactory.decodeStream(instream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(updateResults);
}
public void updateUI(){
imageView.setImageBitmap(bitmap);
}
}
2、主程序
ImageView imageView = (ImageView)findViewById(R.id.image);
String imageUrl = "http://www.qqzhi.com/show/UploadPic/2010-5/2010521102357899.jpg";
new Thread(new DownloadImage(imageView, imageUrl)).start();
这些添加在oncreate()里面就实现了图片的更新了
3、配置文件AndroidManifest
<uses-permission android:name="android.permission.INTERNET"/>
获取访问网络权限
4、布局文件
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x = "100px"
android:layout_y = "100px"
android:src=\'#\'" /icon"
/>
OK,要实现的功能就完全实现了,你可以定义N个变量(就是你需要更新的图片),目前我测试一次更新20幅消耗时间1s。
希望能对大家有所帮助,有兴趣可以一起讨论!
本文出自 “alloxa的博客” 博客,请务必保留此出处http://alloxa.blog.51cto.com/1427286/499424
废话不多说了 直接上代码。
1、下载线程 继承runnable接口
public class DownloadImage implements Runnable {
private ImageView imageView;
private String imageUrl;
private Bitmap bitmap;
//构造的时候传入要更新的ImageView ,同时传入图片的URL
public DownloadImage(ImageView imageView, String imageUrl) {
super();
this.imageView = imageView;
this.imageUrl = imageUrl;
}
public Handler handler = new Handler();
Runnable updateResults = new Runnable() {
@Override
public void run() {
updateUI();
}
};
public void run() {
HttpGet httpRequest = null;
URL url;
try {
url = new URL(imageUrl);
httpRequest = new HttpGet(url.toURI());
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bitmap = BitmapFactory.decodeStream(instream);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
handler.post(updateResults);
}
public void updateUI(){
imageView.setImageBitmap(bitmap);
}
}
2、主程序
ImageView imageView = (ImageView)findViewById(R.id.image);
String imageUrl = "http://www.qqzhi.com/show/UploadPic/2010-5/2010521102357899.jpg";
new Thread(new DownloadImage(imageView, imageUrl)).start();
这些添加在oncreate()里面就实现了图片的更新了
3、配置文件AndroidManifest
<uses-permission android:name="android.permission.INTERNET"/>
获取访问网络权限
4、布局文件
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x = "100px"
android:layout_y = "100px"
android:src=\'#\'" /icon"
/>
OK,要实现的功能就完全实现了,你可以定义N个变量(就是你需要更新的图片),目前我测试一次更新20幅消耗时间1s。
希望能对大家有所帮助,有兴趣可以一起讨论!
本文出自 “alloxa的博客” 博客,请务必保留此出处http://alloxa.blog.51cto.com/1427286/499424
相关文章推荐
- Android AsyncTask(异步耗时 更新UI)
- android进行异步更新UI的四种方式
- Android异步更新UI的方式之使用runOnUiThread(action)方法
- Android 通过广播来异步更新UI
- Android异步更新UI的四种方式
- Android 异步更新UI----AsyncTask
- 详解android异步更新UI的几种方法
- Android异步更新UI的四种方式
- 使用RxAndroid配合MVP模式实现异步网络请求,更新ui
- Handler原理与使用,以及Android中异步更新的UI的方式
- Android开发——异步更新UI的四种方式
- Android 异步更新UI----handler+thread
- [Android UI开发] Android进行异步更新UI的四种方式
- Android-初识Handler-子线程异步更新UI
- Android异步更新UI的几种方法
- Android 异步更新UI-线程池-Future-Handler实例分析
- android进行异步更新UI的四种方式
- Android 通过广播来异步更新UI
- android进行异步更新UI的四种方式
- Android 异步更新UI----handler+thread