简体   繁体   中英

Android: Flashing image to screen while other View content is loading

I've read a few posts on the topic, . Here is my strategy; I have a drawing app that shows the user-created drawings in a ListView. Once the user selects a drawing, the DrawingEdit Activity is fired up, with the drawing being loaded in onCreate, roughly as follows:

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

    // set the layout for the activity
setContentView(R.layout.drawing_view);

    // do potentially expensive loading of drawing here
    loadDrawing();
}

Where loadDrawing() is loading the individual shapes of the user's drawing from a database. Now, for 95% of the drawings, and for the 'typical' use case, this works fine, as the Activity loads quickly with little to no delay. However, for very very complex drawings, the delay can be up to two seconds. To help resolve this issue, I've tried an AsyncTask, which works great for the 5% of drawings that are excessively large, but for the other 95% seems like overkill, and actually makes the loading a little slower due to having to fire up the AsyncTask. It's not the worst solution, but I'm looking at alternatives.

Now, in addition to the drawing data that I have stored in the database, I also have a static PNG version of the drawing that I can make use of... so the solution that I'd like to use is along the lines of:

  1. Load a temporary 'splash' screen view at the beginning of onCreate(), using the static drawing as a placeholder that the user can immediately review while the drawing is loading.
  2. Start the sometimes expensive loadDrawing() routine.
  3. Upon completion switch the View and show the secondary view with the fully interactive drawing canvas that includes the shapes from loadDrawing() .

My problem is that if I model the above solution roughly as below, the 'splash' layout is never displayed, and there is still the same delay until loadDrawing() is complete and then the final layout is displayed. Any info on what is happening here? Should I move the loadDrawing() to onResume so that the initial splash UI has a chance to load before loadDrawing() is triggered?

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

    // set the layout for the activity
setContentView(R.layout.splash_view);

    // do potentially expensive loading of drawing here
    if (loadDrawing()) {
      // set the layout for the activity
  setContentView(R.layout.drawing_view);
    }
}

If loadDrawing is a method within your Activity, it will block the UI thread until it is complete. In short, the UI isn't displayed until after the startup steps (create/start/resume) have finished. It wouldn't matter if you put loadDrawing in onResume as it will still block the UI creation.

You need to use some sort of background asynchronous thread to get this to work - this is why AsyncTask is useful to allow the UI to be drawn/manipulated while something else needs to happen at the same time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM