我们知道更新UI只能在主线程中,但是在日常应用中我们除了使用封装好的AsyncTask外大多会使用Thread+Handler的手法来实现子线程从网络获取数据,然后再修改主线程上的View视图,现在我们可以使用下面5种方法来实现线程间的UI交互.
1,认识Activity.runOnUiThread(Runnable)先上代码
public class MyActivity extends Activity {
private final static String PIC_URL =
"http://www.easyicon.net/api/resizeApi.php?id=1171895&size=128";
private ImageView picIv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
picIv = (ImageView) findViewById(R.id.pic_iv);
}
/**
* 获取图片的监听事件,此方法需要和xml布局中的button的 android:onClick="doRequest"
* 属性名称一致并且为public类型不然会报一些问题
* @param view
*/
publicvoid doRequest(View view) {
thread.start();
}
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
final Bitmap bitmap = getBitmap(PIC_URL);
if