編輯:關於Android編程
如圖,Android上新開的線程如想更新UI,需要重新跳到主線程中才能操作,以下是老外給出的幾種方案,大家多多學習下.<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHA+PGJyPgo8L3A+CjxwPjHNqLn9VUm/2Lz+cG9zdNK7uPZydW5uYWJsZdfTwOA6PC9wPgo8cD48cHJlIGNsYXNzPQ=="brush:java;">private void loadIcon() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(mDelay);
} catch (InterruptedException e) {
e.printStackTrace();
}
mBitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.painter);
mImageView.post(new Runnable() {
@Override
public void run() {
mImageView.setImageBitmap(mBitmap);
}
});
}
}).start();
}
2.通過調用主線程runOnUiThread執行修改
new Thread(new Runnable() { @Override public void run() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { e.printStackTrace(); } mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.painter); SimpleThreadingExample.this.runOnUiThread(new Runnable() { @Override public void run() { mImageView.setImageBitmap(mBitmap); } }); } }).start();
private class LoadIconTask implements Runnable { int resId; LoadIconTask(int resId) { this.resId = resId; } public void run() { handler.post(new Runnable() { @Override public void run() { mProgressBar.setVisibility(ProgressBar.VISIBLE); } }); mBitmap = BitmapFactory.decodeResource(getResources(), resId); // Simulating long-running operation for (int i = 1; i < 11; i++) { sleep(); final int step = i; handler.post(new Runnable() { @Override public void run() { mProgressBar.setProgress(step * 10); } }); } handler.post(new Runnable() { @Override public void run() { mImageView.setImageBitmap(mBitmap); } }); handler.post(new Runnable() { @Override public void run() { mProgressBar.setVisibility(ProgressBar.INVISIBLE); } }); } }
static class UIHandler extends Handler { WeakReferencemParent; public UIHandler(WeakReference parent) { mParent = parent; } @Override public void handleMessage(Message msg) { HandlerMessagesActivity parent = mParent.get(); if (null != parent) { switch (msg.what) { case SET_PROGRESS_BAR_VISIBILITY: { parent.getProgressBar().setVisibility((Integer) msg.obj); break; } case PROGRESS_UPDATE: { parent.getProgressBar().setProgress((Integer) msg.obj); break; } case SET_BITMAP: { parent.getImageView().setImageBitmap((Bitmap) msg.obj); break; } } } } } Handler handler = new UIHandler(new WeakReference ( this)); private class LoadIconTask implements Runnable { private final int resId; private final Handler handler; LoadIconTask(int resId, Handler handler) { this.resId = resId; this.handler = handler; } public void run() { Message msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY, ProgressBar.VISIBLE); handler.sendMessage(msg); final Bitmap tmp = BitmapFactory.decodeResource(getResources(), resId); for (int i = 1; i < 11; i++) { sleep(); msg = handler.obtainMessage(PROGRESS_UPDATE, i * 10); handler.sendMessage(msg); } msg = handler.obtainMessage(SET_BITMAP, tmp); handler.sendMessage(msg); msg = handler.obtainMessage(SET_PROGRESS_BAR_VISIBILITY, ProgressBar.INVISIBLE); handler.sendMessage(msg); } private void sleep() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { e.printStackTrace(); } } }
class LoadIconTask extends AsyncTask{ @Override protected void onPreExecute() { mProgressBar.setVisibility(ProgressBar.VISIBLE); } @Override protected Bitmap doInBackground(Integer... resId) { Bitmap tmp = BitmapFactory.decodeResource(getResources(), resId[0]); // simulating long-running operation for (int i = 1; i < 11; i++) { sleep(); publishProgress(i * 10); } return tmp; } @Override protected void onProgressUpdate(Integer... values) { mProgressBar.setProgress(values[0]); } @Override protected void onPostExecute(Bitmap result) { mProgressBar.setVisibility(ProgressBar.INVISIBLE); mImageView.setImageBitmap(result); } private void sleep() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { Log.e(TAG, e.toString()); } } }
Splash界面版本更新業務邏輯:
本章簡單講述下android實現自動撥號的功能,該功能利用了系統啟動的rild的服務來實現,因為rild的服務是殺不死的,所以利用這一點,可以使撥號失敗或網絡斷掉後自動重
在介紹本文動畫效果實現之前,先來介紹屬性動畫相關的幾個知識點。ValueAnimator與ObjectAnimator。 Interpolator插值器與TypeEval
在4.5.6節介紹過一個<include>標簽,該標簽可以在布局文件中引用另外一個布局文件,並可以覆蓋被引用布局文件根節點所有與布局相關的屬性,也就是以and