android下面图片更新是需要启动多个子线程来进行的,而android下面是并不是线程安全的,所以thread这里是用不了的,只能用runnable接口。

废话不多说了 直接上代码。

1、下载线程  继承runnable接口

public class DownloadImage implements Runnable {

 

private ImageView p_w_picpathView;

private String p_w_picpathUrl;

private Bitmap bitmap;

 

        //构造的时候传入要更新的ImageView ,同时传入图片的URL

public DownloadImage(ImageView p_w_picpathView, String p_w_picpathUrl) {

super();

this.p_w_picpathView = p_w_picpathView;

this.p_w_picpathUrl = p_w_picpathUrl;

}

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(p_w_picpathUrl);

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(){

p_w_picpathView.setImageBitmap(bitmap);

}

}

2、主程序

ImageView p_w_picpathView = (ImageView)findViewById(R.id.p_w_picpath);

String p_w_picpathUrl = "http://www.qqzhi.com/show/UploadPic/2010-5/2010521102357899.jpg";

 new Thread(new DownloadImage(p_w_picpathView, p_w_picpathUrl)).start();

这些添加在oncreate()里面就实现了图片的更新了  

 

3、配置文件AndroidManifest


获取访问网络权限

 

4、布局文件

android:id="@+id/p_w_picpath"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_x = "100px"

android:layout_y = "100px"

android:src="@drawable/icon"

/>

 

 

OK,要实现的功能就完全实现了,你可以定义N个变量(就是你需要更新的图片),目前我测试一次更新20幅消耗时间1s。

希望能对大家有所帮助,有兴趣可以一起讨论!

 

 

 

更多相关文章

  1. Android蓝牙通信功能开发
  2. Android(安卓)Activity之间传递图片(Bitmap)的方法
  3. Android中WebView获取网页中标题 ,内容, 图片的方法
  4. android 延时 不用另起线程或timertask
  5. Android面试题整理(一)
  6. Android(安卓)中如何关闭线程
  7. android 4.0 以上平台选择图片报错Attempted to access a cursor
  8. android 下改变默认的checkbox的 选中 和被选中 图片
  9. android内部培训视频_第五节(1)_OA实战之登录界面

随机推荐

  1. 是否有一个类似于Python的“枚举”函数的
  2. Python之错误异常和文件处理
  3. 在java中调用python方法
  4. Python知识点整理(基础篇)
  5. python 之 logger日志 字典配置文件
  6. python import的一些问题
  7. Python使用pandas对数据进行差分运算
  8. Python pyexcelerate库将多个工作表写入
  9. python 中 unicode原样转成str, unicode-
  10. python2和python3的区别