HandlerRunnableActivity.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 |
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; 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 HandlerRunnableActivity extends Activity { private ImageView mImageView; private ProgressBar mProgressBar; private Bitmap mBitmap; private int mDelay = 500; private final Handler handler = new Handler(); @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)).start(); } }); final Button otherButton = (Button) findViewById(R.id.otherButton); otherButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(HandlerRunnableActivity.this, "I'm Working", Toast.LENGTH_SHORT).show(); } }); } 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); } }); } } private void sleep() { try { Thread.sleep(mDelay); } catch (InterruptedException e) { e.printStackTrace(); } } } |
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> |