2013-08-02 26 views
22

我正在寻找在屏幕上显示覆盖图,该屏幕显示一个小的加载代码或可能甚至一些文本,而我的应用程序尝试登录到服务器。我的登录屏幕全部位于垂直线性布局的内部。在Android屏幕上显示加载覆盖图

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

+3

您正在寻找这个?对 ? http://stackoverflow.com/questions/2176922/how-to-create-transparent-activity-in-android –

+0

@ M-WaJeEh如何添加文字和/或图形到该活动?像你添加到一个正常的“活动”, –

+0

。它只是一个正常的“活动”,具有不同的主题。 –

回答

48

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

活动:

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> 
+2

+1代表实际代码 – JcKelley

+0

这应该被选作答案。所选答案建议关于不再使用的ProgressDialog,并且不提供代码。 –

1

我进度中相对布局和隐藏或分别显示它。而且活动可以是透明的。

<?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> 
+0

使用相对布局而不是android用于登录的标准布局是我的问题的解决方案,我无法集中进度条。谢谢! – PeterPan

-2

您必须使用AsyncTask之类的线程概念进行后台操作。使用这个你可以隐藏UI部分的实际工作。在完成操作后,AsyncTask将会被取消分配。

  • 创建的AsyncTask的一个子类
  • 使用的AsyncTask做后台工作
  • 呼叫onPreExecute()来初始化任务
  • 使用进度与setIndeterminate(真),以使 不确定模式
  • 调用onProgressUpdate()为您的进度条设置动画,让用户 知道正在完成一些工作
  • 使用incrementProgressBy()进行增量编程由 特定值
  • 调用doInBackground(SBAR内容),在这里做背景的工作
  • 抓住一个InterruptedException对象找到后台 操作
  • 呼叫onPostExecute(到2005年底)表示操作的结束并显示 结果

Android's indeterminate ProgressDialog tutorial

Splash screen while loading resources in android app

3

使用ProgressDialog可以创建带有应用程序消息的微调控件。虽然它没有达到如图所示的确切效果,但它是显示应用程序正常工作的好方法。

35

我喜欢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。)

+1

我正在使用ConstraintLayout,并且必须使用'progressOverlay.bringToFront()'来防止与其他UI元素的交互。否则,整洁的回答Jonik和Kostya。 – DJSquared