【问题标题】:It's possible change the Android Activity Transition Effect to a loading animation?可以将Android Activity Transition Effect 更改为加载动画吗?
【发布时间】:2013-05-27 02:32:45
【问题描述】:

我想创建一个“加载”屏幕,直到 Activity 的布局完全加载,有什么办法吗?

或者为当前主题定义一个自定义的默认背景?

【问题讨论】:

  • 你可以尝试添加一个启动画面来保持简单..

标签: android android-activity loading transition effect


【解决方案1】:

使用两个视图设计您的活动。

-Loading layout.
-Second layout. 

现在在 xml 中将第二个布局可见性设置为“消失”或“不可见”

现在在您的第二个布局加载后..(使用异步任务,它将在后台进行加载。我使用伪代码):

  class Task extends AsyncTask<Void,Void,Void>{
  doInBackGround(){
   Load your content
  }
  onPostExecute(){
  set the visibility here;
  }

将加载布局设置为“不可见”,将第二个布局设置为可见。

您可以在视图之间添加动画以使其看起来更好。

【讨论】:

    【解决方案2】:

    您不应该阻止您的 UI 线程。在我看来,您正在这样做并且您可能会遇到 ANR(应用程序未响应异常)。如果您正在对 UI 线程进行更新并且这些更新很繁重且需要采取行动,那么您必须使用工作线程。工作线程异步更新 UI 线程而不阻塞它。

    例子:

    class workerThread extends Thread {
            public void run() {
                        final View play = getActivity()
                                .findViewById(R.id.play);
                        play.post(new Runnable() {
                            public void run() {
                            //put all your time taking tasks inside this post runnable.Here you may implement updateprogressbar etc
                                play.performClick();
                            }
                        });
                    }   
            }
        workerThread = new workerThread();
        workerThread.start();
    

    您也可以考虑使用AsyncTask,它自己处理大部分样板代码。

    您可以随意更改背景,但您的应用程序将不稳定。如果您坚持这样做,只需在您的视图中创建一个 ProgressDialog 并根据需要进行更新:

    Example:
    mProgressDialog = new ProgressDialog(getActivity());
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        mProgressDialog.setTitle(R.string.progress_dialog_loading);
        mProgressDialog.setCancelable(true);
        mProgressDialog
                .setOnCancelListener(new DialogInterface.OnCancelListener() {
                    public void onCancel(DialogInterface dialog) {
                        mLoadingKeepGoing = false;
                    }
                });
    
        mProgressDialog.show();
    

    线程和过程是相当复杂的话题,你需要多学习和练习。以下是我为您找到的一些资源:

    1.Processes and threads for android doc

    2.Worker thread blog post

    3.Another blog post

    4.On stackoverflow

    【讨论】:

    • 但是,我可以更改当前主题样式的默认背景吗?
    • 改变样式需要你activity.recreate()你的活动,你当前的活动需要重新创建,我确定你不希望它发生。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-24
    • 1970-01-01
    • 1970-01-01
    • 2013-01-25
    • 1970-01-01
    • 2023-02-16
    相关资源
    最近更新 更多