简体   繁体   English

Android:在加载其他View内容时将图像闪烁到屏幕

[英]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. 我有一个绘图应用程序,可以在ListView中显示用户创建的绘图。 Once the user selects a drawing, the DrawingEdit Activity is fired up, with the drawing being loaded in onCreate, roughly as follows: 用户选择绘图后,将启动DrawingEdit活动,并将绘图加载到onCreate中,大致如下:

@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. loadDrawing()从数据库加载用户图形的各个形状。 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. 现在,对于95%的工程图和“典型”用例,此方法都可以正常工作,因为Activity加载很快,几乎没有延迟。 However, for very very complex drawings, the delay can be up to two seconds. 但是,对于非常复杂的图形,延迟可能长达2秒。 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. 为了帮助解决此问题,我尝试了一个AsyncTask,它对于5%的过大图纸非常有效,但对于另外95%的图纸来说似乎过头了,实际上由于不得不开火,加载速度稍慢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: 现在,除了我存储在数据库中的图形数据之外,我还可以使用图形的静态PNG版本...因此,我要使用的解决方案是:

  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. 使用静态图形作为占位符,在onCreate()的开头加载临时的“初始”屏幕视图,用户可以在加载图形时立即对其进行查看。
  2. Start the sometimes expensive loadDrawing() routine. 启动有时昂贵的loadDrawing()例程。
  3. Upon completion switch the View and show the secondary view with the fully interactive drawing canvas that includes the shapes from loadDrawing() . 完成后,切换视图并显示具有完全交互式绘图画布的辅助视图,其中包括来自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. 我的问题是,如果我大致按如下所示对上述解决方案进行建模,则“ splash”布局将永远不会显示,并且在loadDrawing()完成并显示最终布局之前仍然存在相同的延迟。 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? 我应该将loadDrawing()移到onResume上,以便在启动loadDrawing()之前有机会加载初始启动界面吗?

@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. 如果loadDrawing是您Activity中的一个方法,它将阻塞UI线程直到完成。 In short, the UI isn't displayed until after the startup steps (create/start/resume) have finished. 简而言之,直到完成启动步骤(创建/启动/继续)后,才会显示UI。 It wouldn't matter if you put loadDrawing in onResume as it will still block the UI creation. 是否将loadDrawing放在onResume中并不重要,因为它仍然会阻止UI的创建。

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. 您需要使用某种后台异步线程来使其正常工作-这就是为什么AsyncTask有助于在需要同时进行其他操作的同时绘制/操作UI的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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