在Android屏幕上显示加载覆盖



当我的应用程序尝试登录服务器时,我希望在屏幕上显示一个覆盖,显示一个加载的小股票代码,甚至可能显示一些文本。我的登录屏幕是一个垂直的线性布局。

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

也许为时已晚,但我想有些人可能会觉得它很有用。

活动:

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>

我喜欢Kostya But的答案中的方法。

在此基础上,这里有几个想法可以让相同的覆盖层在您的应用程序中轻松地可重复使用

考虑将覆盖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);
        }
    });
}

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

我在相对布局中有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>

可以使用ProgressDialog创建在应用程序上带有消息的微调器。虽然它没有达到图片中的确切效果,但这是一个很好的方式来表明应用程序正在工作。

我制作了一个库(还没有很好的文档,在减轻一些工作压力后的几天内完成)来进行这种进度对话框。我以非常可重复使用的方式实现了它,这就是为什么你只需要配置一次,然后只需调用一行代码就可以在应用程序中的任何位置隐藏显示它。配置-

 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();

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

  1. 将屏幕划分为两个视图:内容和进度栏
  2. 当您想调用ProgressBar时,您可以将可见性更改为VISIBLE,并将以下属性添加到content-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>

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

  • 创建AsyncTask的子类
  • 使用AsyncTask执行后台工作
  • 调用onPreExecute()初始化任务
  • 使用带有setIndeterminate(true)的进度条来启用不确定模式
  • 调用onProgressUpdate()来设置进度条的动画,让用户知道有些工作正在进行
  • 通过特定值
  • 调用doInBackground()并在此处进行后台工作
  • 捕获InterruptedException对象以查找后台的末尾操作
  • 调用onPostExecute()表示操作结束,并显示结果

Android的不确定ProgressDialog教程

在安卓应用程序中加载资源时启动屏幕

最新更新