Reputation: 1011
I have one method and one bitmap
private static Bitmap do(Bitmap bit){
//Show loading until return;
return bit;
}
When i use the following code it takes about 30 seconds or more. What i want is to show a loading bar until the method completes
bitmap = do(bitmap);
How can i achieve that?
Example
What i do to resize a bitmap:
Bitmap bit;
bit = getResizedBitmap(bit,100,100);//takes 20 seconds
image.setImageBitmap(bit);
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
return resizedBitmap;
}
How can i get the same result using AsyncTask
Upvotes: 0
Views: 170
Reputation: 16398
First: You should put the method in an AsyncTask.
Second: You display the progress bar before calling the method. Then, you hide it when the method finishes.
Example:
private class MyTask extends AsyncTask<Bitmap, Integer, Boolean> {
protected Boolean doInBackground(Bitmap... bit) {
//show progress bar
//call your method
return true;
}
protected void onProgressUpdate(Integer... progress) {
setProgressPercent(progress[0]);
}
protected void onPostExecute(Boolean done) {
//hide progress bar
}
}
And you run your AsyncTask like this:
Bitmap bit;
new MyTask().execute(bit);
//Now your Bitmap object is ready.
Upvotes: 1