HandlerMessagesActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 |
import java.lang.ref.WeakReference; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.ProgressBar; import android.widget.Toast; public class HandlerMessagesActivity extends Activity { private final static int SET_PROGRESS_BAR_VISIBILITY = 0; private final static int PROGRESS_UPDATE = 1; private final static int SET_BITMAP = 2; private ImageView mImageView; private ProgressBar mProgressBar; private int mDelay = 500; static class UIHandler extends Handler { WeakReference<HandlerMessagesActivity> mParent; public UIHandler(WeakReference<HandlerMessagesActivity> 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<HandlerMessagesActivity>( this)); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); mImageView = (ImageView) findViewById(R.id.imageView); mProgressBar = (ProgressBar) findViewById(R.id.progressBar); final Button button = (Button) findViewById(R.id.loadButton); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { new Thread(new LoadIconTask(R.drawable.painter, handler)) .start(); } }); final Button otherButton = (Button) findViewById(R.id.otherButton); otherButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(HandlerMessagesActivity.this, "I'm Working", Toast.LENGTH_SHORT).show(); } }); } 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(); } } } public ImageView getImageView() { return mImageView; } public ProgressBar getProgressBar() { return mProgressBar; } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="250dp" android:contentDescription="@string/image_desc_string" android:scaleType="centerInside" > </ImageView> <ProgressBar android:id="@+id/progressBar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="match_parent" android:layout_height="wrap_content" android:maxHeight="5dip" android:minHeight="5dip" android:visibility="invisible" > </ProgressBar> <Button android:id="@+id/loadButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/load_icon_string" android:textSize="24sp" > </Button> <Button android:id="@+id/otherButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/other_button_string" android:textSize="24sp" > </Button> </LinearLayout> |