更新UI的方法
是更新UI的方法不是线程安全的,即只能在单线程中完成UI的更新,不能使用多线程。因此,只能在主线程(UI线程)中进行UI更新。下面介绍几种方法:
1,使用Activity.runOnUIThread(Runnable)方法:
这种方法比较简单,看字面意思就是说在线程中更新UI,就把更新的代码抛给主线程做就行
public class MainActivity extends Activity {
private ImageView im ;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView)findViewById(R.id.iv);
myThread th = new myThread();
th.start();
}
public class myThread extends Thread{
@Override
public void run() {
super.run();
try {
URL url = new URL("http://amuse.nen.com.cn/imagelist/11/11/z9ei83iq7k8b.jpg");
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
urlcon.connect();
InputStream is = urlcon.getInputStream();
int length = (int) urlcon.getContentLength();
if (length != -1) {
byte[] imgData = new byte[length];
byte[] temp=new byte[512];
int readLen=0;
int destPos=0;
while((readLen=is.read(temp))>0){
System.arraycopy(temp, 0, imgData, destPos, readLen);
destPos+=readLen;
}
bitmap=BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
}
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
im.setImageBitmap(bitmap);
}
});
urlcon.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
2,View.post(Runnable )方法:
public class MainActivity extends Activity {
private ImageView im ;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView)findViewById(R.id.iv);
myThread th = new myThread();
th.start();
}
public class myThread extends Thread{
@Override
public void run() {
super.run();
try {
URL url = new URL("http://amuse.nen.com.cn/imagelist/11/11/z9ei83iq7k8b.jpg");
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
urlcon.connect();
InputStream is = urlcon.getInputStream();
int length = (int) urlcon.getContentLength();
if (length != -1) {
byte[] imgData = new byte[length];
byte[] temp=new byte[512];
int readLen=0;
int destPos=0;
while((readLen=is.read(temp))>0){
System.arraycopy(temp, 0, imgData, destPos, readLen);
destPos+=readLen;
}
bitmap=BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
}
im.post(new Runnable() {
@Override
public void run() {
im.setImageBitmap(bitmap);
}
});
urlcon.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
3,Handler方法:
public class MainActivity extends Activity {
private ImageView im ;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView)findViewById(R.id.iv);
myThread th = new myThread();
th.start();
}
Handler myHander = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
super.handleMessage(msg);
im.setImageBitmap(bitmap);
}
};
public class myThread extends Thread{
@Override
public void run() {
super.run();
try {
URL url = new URL("http://amuse.nen.com.cn/imagelist/11/11/z9ei83iq7k8b.jpg");
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
urlcon.connect();
InputStream is = urlcon.getInputStream();
int length = (int) urlcon.getContentLength();
if (length != -1) {
byte[] imgData = new byte[length];
byte[] temp=new byte[512];
int readLen=0;
int destPos=0;
while((readLen=is.read(temp))>0){
System.arraycopy(temp, 0, imgData, destPos, readLen);
destPos+=readLen;
}
bitmap=BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
}
myHander.sendEmptyMessage(1);
urlcon.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
4.AsyncTask方法:
public class MainActivity extends Activity {
private ImageView im ;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
im = (ImageView)findViewById(R.id.iv);
new DownloadTask().execute("http://amuse.nen.com.cn/imagelist/11/11/z9ei83iq7k8b.jpg");
}
class DownloadTask extends AsyncTask<String, Void, Bitmap>{
@Override
protected Bitmap doInBackground(String... urls) {
// TODO Auto-generated method stub
Bitmap bmp = null;
bmp = this.loadImageFromNetwork(urls[0]);
return bmp;
}
@Override
protected void onPostExecute(Bitmap result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
im.setImageBitmap(result);
}
private Bitmap loadImageFromNetwork(String str) {
try {
URL url = new URL(str);
HttpURLConnection urlcon = (HttpURLConnection)url.openConnection();
urlcon.connect();
InputStream is = urlcon.getInputStream();
int length = (int) urlcon.getContentLength();
if (length != -1) {
byte[] imgData = new byte[length];
byte[] temp=new byte[512];
int readLen=0;
int destPos=0;
while((readLen=is.read(temp))>0){
System.arraycopy(temp, 0, imgData, destPos, readLen);
destPos+=readLen;
}
bitmap=BitmapFactory.decodeByteArray(imgData, 0, imgData.length);
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bitmap;
}
}
}