更新UI 2种方法

来源:互联网 发布:现代诗集推荐 知乎 编辑:程序博客网 时间:2024/05/16 06:51

一、当前更新Activity 

//      handler声明private Handler mHandler = new Handler() { public void handleMessage(Message msg) { switch (msg.what) { case 1: imag_mlzx.setScaleType(ScaleType.CENTER_CROP); imag_mlzx.setImageBitmap(bitmap); break;                 }       }; };
二、oncreat()方法中实现调用

 @Override public void run() { try { bitmap = PrcFromUrl .getBitmap("http://employee.pzfw.net/Image/lunbo/1.jpg"); Message message = new Message(); message.what = 1; mHandler.sendMessage(message); } catch (IOException e) { e.printStackTrace(); }}}).start();

这是最基本的   给个图片链接就能更新Ui  


第二种:创建一个工具类才用Bitmap 进行更新


public class PrcFromUrl {
public static Bitmap getBitmap(String path) throws IOException {


URL fileUrl = null;
Bitmap bitmap = null;


try {
fileUrl = new URL(path);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) fileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
}





0 0
原创粉丝点击