开发者

Loading bitmaps in onCreate makes activity start slow

I'm new to android programming and recently been learning to develop some applications. It wasn't much problem until I came across dealing with images. I'm trying to develop a simple application more like a eBook which shows pictures(JPEG/PNG) stored in the drawable folder.

My first approach was to use the Gallery widget, but I was unable to implement a proper zoom with that. So I'm now using a simple Imageview to display a single bitmap at a time and change or zoom the bitmap on touch events. In order to do that, I had to decode all the bitmaps into a bitmap array during activity startup so that i could use them anytime.

My application starts fine on my Galaxy Tab but every phone except that takes about 3-5 seconds to start. I just wanted to know if there's an easy way to load bitmaps during start. Forgive me if my question is asked already, but I searched stackoverflow a lot and couldn't find specific answer to my question. Here's my code. Hope anyone could help.

    package ....

    imports ....

    public class ImageViewFlipper extends Activity {

    private ViewFlipper viewFlipper;
    private Bitmap[] bMap= new Bitmap[15];
    private TextView currentpage;
    private ImageViewZoom iv;
    private imagenum=3;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    bMap[0] =BitmapFactory.decodeResource(getResources(), R.drawable.page);
    bMap[1] =BitmapFactory.decodeResource(getResources(), R.drawable.page1);
    bMap[2] =BitmapFactory.decodeResource(getResources(), R.drawable.page10);
    bMap[3] =BitmapFactory.decodeResource(getResources(), R.drawable.page11);
    bMap[4] =BitmapFactory.decodeResource(getResources(), R.drawable.page12);
    bMap[5] =BitmapFactory.decodeResource(getResources(), R.drawable.page13);
    bMap[6] =BitmapFactory.decodeResource(getResources(), R.drawable.page14);
    bMap[7] =BitmapFactory.decodeResource(getResources(), R.drawable.page2);
    bMap[8] =BitmapFactory.decodeResource(getResources(), R.drawable.page3);
    bMap[9] =BitmapFactory.decodeResource(getResources(), R.drawable.page4);
    bMap[10] =BitmapFactory.decodeResource(getResources(), R.drawable.page5);
    bMap[11] =BitmapFactory.decodeResource(getResources(), R.drawable.page开发者_如何学JAVA6);
    bMap[12] =BitmapFactory.decodeResource(getResources(), R.drawable.page7);
    bMap[13] =BitmapFactory.decodeResource(getResources(), R.drawable.page8);
    bMap[14] =BitmapFactory.decodeResource(getResources(), R.drawable.page9);

    currentpage=(TextView)findViewById(R.id.textview);
    iv = (ImageViewZoom) findViewById(R.id.zero);
    iv.setImageBitmapReset( bMap[imagenum], 0, true );
    viewFlipper = (ViewFlipper) findViewById(R.id.flipper);
    ......

the value of imagenum changes according to the onFling method, ImageViewZoom is the custom ImageView class. I hope this much information is sufficient.


I would suggest moving all the bitmap loading into an AsyncTask that you start inside onCreate. Design your activity to display something sensible before all the images are loaded. The AsyncTask can either load all the images and deliver them in onPostExecute, or else can deliver each one as it is loaded using onProgressUpdate.


Why not try loading 2 or 3 images first and then load the other images later in the background?


You can also think about Service for loading image in background. http://developer.android.com/guide/topics/fundamentals/services.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜