user918197
user918197

Reputation: 1139

Loading of images using multi-threading in android

I have a class that extends Activity and within it got a private class that extends View which i calling it to load the images. However, i wasn't whether i should use AsyncTask, Handler or other ways to perform the loading of images on the background and showing the progressBar at the same time while loading. For AsyncTask, the task instance must be created on the UI thread (which i presume is in the Activity class). But all the function is in the View class and have trouble using AsyncTask. Is using Handler a good choice?

public class LargeImageScroller extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
            setContentView(new SampleView(this));
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {...}

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {...}

    private static class SampleView extends View {

        public SampleView(Context context) {...}
        public boolean onTouchEvent(MotionEvent event) {...}
        protected void onDraw(Canvas canvas) {...}
        private boolean createMap(Context context) {...}
}

This is the rough skeleton of the program. createMap() is the function where i load images into a canvas so as to display the images. But if i declare a class that extends AsyncTask, the task instance can only be created in the onCreate() and not anywhere else so i have problem calling the createMap() to load the images. I was thinking of using Handler so as to load the images on a separate thread from the main thread. Can anyone tell me is there a better way of doing things?

Upvotes: 0

Views: 1867

Answers (1)

Nicholas Magnussen
Nicholas Magnussen

Reputation: 769

There's a really good post on multithreading in Android here. Should give you a few hints on what to do.

http://android-developers.blogspot.com/2010/07/multithreading-for-performance.html

Upvotes: 1

Related Questions