在Android屏幕上显示加载叠加层

时间:2013-08-02 15:48:22

标签: android progress-bar overlay

我希望在屏幕上显示一个叠加层,当我的应用尝试登录服务器时,该叠加层会显示一些加载自动收报机,甚至可能显示一些文字。我的登录屏幕都在垂直线性布局中。

我想要达到的效果是这样的:http://docs.xamarin.com/recipes/ios/standard_controls/popovers/display_a_loading_message

7 个答案:

答案 0 :(得分:88)

可能为时已晚,但我想有人可能会发现它很有用。

的活动:

public class MainActivity extends Activity implements View.OnClickListener {

    String myLog = "myLog";

    AlphaAnimation inAnimation;
    AlphaAnimation outAnimation;

    FrameLayout progressBarHolder;
    Button button;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        progressBarHolder = (FrameLayout) findViewById(R.id.progressBarHolder);

        button.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button:
                new MyTask().execute();
                break;
        }

    }

    private class MyTask extends AsyncTask <Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            button.setEnabled(false);
            inAnimation = new AlphaAnimation(0f, 1f);
            inAnimation.setDuration(200);
            progressBarHolder.setAnimation(inAnimation);
            progressBarHolder.setVisibility(View.VISIBLE);
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);
            outAnimation = new AlphaAnimation(1f, 0f);
            outAnimation.setDuration(200);
            progressBarHolder.setAnimation(outAnimation);
            progressBarHolder.setVisibility(View.GONE);
            button.setEnabled(true);
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                for (int i = 0; i < 5; i++) {
                    Log.d(myLog, "Emulating some task.. Step " + i);
                    TimeUnit.SECONDS.sleep(1);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}

布局xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start doing stuff"
        android:id="@+id/button"
        android:layout_below="@+id/textView"
        android:layout_centerHorizontal="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="Do Some Stuff"
        android:id="@+id/textView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

    <FrameLayout
        android:id="@+id/progressBarHolder"
        android:animateLayoutChanges="true"
        android:visibility="gone"
        android:alpha="0.4"
        android:background="#000000"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:indeterminate="true"
            android:layout_gravity="center" />
    </FrameLayout>
</RelativeLayout>

答案 1 :(得分:55)

我喜欢Kostya But's answer中的方法。

在此基础上,这里有一些想法可以在您的应用中轻松重复使用

考虑将叠加层FrameLayout放在单独的布局文件中,例如res/layout/include_progress_overlay

<FrameLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/progress_overlay"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:alpha="0.4"
    android:animateLayoutChanges="true"
    android:background="@android:color/black"
    android:clickable="true"
    android:visibility="gone">

    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:indeterminate="true"/>

</FrameLayout>

(我在叠加层FrameLayout中添加的一件事是android:clickable="true"。因此,当显示叠加层时,它会阻止点击进入其下方的UI元素。至少在我的典型用例中,这就是我想要的。 )

然后在需要的地方加入:

<!-- Progress bar overlay; shown while login is in progress -->
<include layout="@layout/include_progress_overlay"/>

在代码中:

View progressOverlay;
[...]

progressOverlay = findViewById(R.id.progress_overlay);
[...]

// Show progress overlay (with animation): 
AndroidUtils.animateView(progressOverlay, View.VISIBLE, 0.4f, 200);
[...]

// Hide it (with animation): 
AndroidUtils.animateView(progressOverlay, View.GONE, 0, 200); 

将动画代码提取到util方法中:

/**
 * @param view         View to animate
 * @param toVisibility Visibility at the end of animation
 * @param toAlpha      Alpha at the end of animation
 * @param duration     Animation duration in ms
 */
public static void animateView(final View view, final int toVisibility, float toAlpha, int duration) {
    boolean show = toVisibility == View.VISIBLE;
    if (show) {
        view.setAlpha(0);
    }
    view.setVisibility(View.VISIBLE);
    view.animate()
            .setDuration(duration)
            .alpha(show ? toAlpha : 0)
            .setListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
            view.setVisibility(toVisibility);
        }
    });
}

(此处使用view.animate(),在API 12中添加,而不是AlphaAnimation。)

答案 2 :(得分:5)

我在相对布局中有ProgressBar,我分别隐藏或显示它。是的活动可以是透明的。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:id="@+id/hsvBackgroundContainer"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
    </LinearLayout>


    <ProgressBar
        android:id="@+id/pbProgess"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

答案 3 :(得分:3)

可以使用ProgressDialog创建在应用程序上显示消息的微调器。虽然它没有达到图片中的确切效果,但这是展示应用程序正常工作的好方法。

答案 4 :(得分:1)

我制作了一个library文档尚不完善,在减轻工作压力后的几天内完成 )来进行这种进度对话框 。我采用了非常可重用的方式,这就是为什么您只需要配置一次并在应用程序中的任何地方隐藏它们即可,只需调用一行代码即可。 配置-

 LoadingPopup.getInstance(this)
                .customLoading()
               .setCustomViewID(R.layout.yourProgressLayout,R.color.yourProgressBackgroundColor)
                .doIntentionalDelay()
                .setDelayDurationInMillSec(5000)
                .setBackgroundOpacity(70)/*How much transparent you want your background*/
                .build();

用于显示进度-

LoadingPopup.showLoadingPopUp();

用于隐藏进度-

LoadingPopup.hideLoadingPopUp();

答案 5 :(得分:0)

我有相同的问题,我尝试了解决方案,但不是最好的UI,所以我执行了以下步骤。

  1. 将屏幕分为2个视图:Content和ProgressBar。
  2. 当您要调用ProgressBar时,请将可见性更改为VISIBLE,并将以下属性添加到内容id = content而不是progressBar容器中。

content.background =“#000000” content.alpha =“ 0.4”

<FrameLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            tools:context=".MainActivity">

            <Button
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/textView"
                android:layout_centerHorizontal="true"
                android:text="Start doing stuff" />

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_centerHorizontal="true"
                android:text="Do Some Stuff"
                android:textAppearance="?android:attr/textAppearanceLarge" />

        </RelativeLayout>

        <ProgressBar
            style="?android:attr/progressBarStyleLarge"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:indeterminate="true"
            android:visibility="gone" />

    </FrameLayout>

答案 6 :(得分:-2)

您必须使用AsyncTask等线程概念进行后台操作。使用此功能,您可以隐藏UI部分中的实际工作。操作完成后,AsyncTask将被取消分配。

  • 创建AsyncTask的子类
  • 使用AsyncTask执行后台工作
  • 调用onPreExecute()初始化任务
  • 使用带有setIndeterminate(true)的进度条启用 不确定模式
  • 调用onPressressUpdate()为您的进度条设置动画以让用户 知道正在做一些工作
  • 使用incrementProgressBy()通过a增加进度条内容 具体价值
  • 调用doInBackground()并在此处完成后台工作
  • 捕获InterruptedException对象以查找背景结束 操作
  • 调用onPostExecute()表示操作结束并显示 结果

Android's indeterminate ProgressDialog tutorial

Splash screen while loading resources in android app