Created
March 3, 2014 19:10
-
-
Save csdear/2d1435889585a5c8b58f to your computer and use it in GitHub Desktop.
Asynctask : Progress Bar
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Progress Bar asyncTask example | |
| package course.examples.Threading.ThreadingAsyncTask; | |
| 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 listener, LoadIcon Task. | |
| 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(); | |
| } | |
| }); | |
| } | |
| //LodIconTask defined as an asynctask. Arguments <params, progress, result> | |
| class LoadIconTask extends AsyncTask<Integer, Integer, Bitmap> { | |
| //onPreExecute() executed in the UI thread, and makes a ProgressBar visible on the main display. | |
| @Override | |
| protected void onPreExecute() { | |
| mProgressBar.setVisibility(ProgressBar.VISIBLE); | |
| } | |
| //doInBackground(). First argument is a integet that represents the ID for the bitmap -- resId | |
| //Purpose : Does the work of loading the bitmap. | |
| //Also periodically calls "PublishProgress" to update to progressbar. | |
| @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; | |
| } | |
| //Runs in UI thread. Receive integer passed into publish progress, and sets the progressbar to the % of work done. | |
| @Override | |
| protected void onProgressUpdate(Integer... values) { | |
| mProgressBar.setProgress(values[0]); | |
| } | |
| //Run in UI thread. Receives bitmap as a parameter. Makes bitmap visible then sets the image on the mImageView. | |
| @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()); | |
| } | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment