Android 更新UI

更新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;
			
		}
	}
	
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值