您的位置:首页 > 产品设计 > UI/UE

更新UI 2种方法

2016-06-01 20:05 337 查看
一、当前更新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;
}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息