AsyncTaskActivity.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 |
import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.AsyncTask; import android.os.Bundle; import android.util.Log; 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 AsyncTaskActivity extends Activity { private final static String TAG = "ThreadingAsyncTask"; private ImageView mImageView; private ProgressBar mProgressBar; private int mDelay = 500; @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 LoadIconTask().execute(R.drawable.painter); } }); final Button otherButton = (Button) findViewById(R.id.otherButton); otherButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(AsyncTaskActivity.this, "I'm Working", Toast.LENGTH_SHORT).show(); } }); } class LoadIconTask extends AsyncTask<Integer, Integer, Bitmap> { @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()); } } } } |
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="300dp" android:scaleType="centerInside" android:contentDescription="@string/image_desc_string"> </ImageView> <ProgressBar android:id="@+id/progressBar" style="@android:style/Widget.ProgressBar.Horizontal" android:layout_width="fill_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> |