在加载东西时在ImageView中使用"动画圆圈"

Waz*_*_Be 212 geometry android loading android-asynctask

我目前正在我的应用程序中使用listview,可能需要一秒钟才能显示.

我目前所做的是使用listview的@ id/android:empty属性来创建"加载"文本.

 <TextView android:id="@id/android:empty"
           android:layout_width="match_parent"
           android:layout_height="match_parent"
           android:background="#FF0000"
           android:text="Loading..."/>
Run Code Online (Sandbox Code Playgroud)

现在,我想用加载对话框中使用的动画圆代替此文本,我想你们都知道我的意思:

编辑:我不想要对话.我想在我的布局中展示它.

http://flexfwd.com/DesktopModules/ATI_Base/resources/images/loading_blue_circle.gif

非常感谢您的帮助!

use*_*613 416

只需将此xml块放入活动布局文件中:

<RelativeLayout
    android:id="@+id/loadingPanel"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center" >

    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true" />
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

完成加载后,请调用以下一行:

findViewById(R.id.loadingPanel).setVisibility(View.GONE);
Run Code Online (Sandbox Code Playgroud)

结果(它也旋转):

在此输入图像描述

  • 砸了+1按钮 (13认同)
  • @Gustavo,这就是他想要的,展示一个"不确定的进展动画",所以我认为它解决了这个问题.; - ] (3认同)

Kur*_*rru 142

您可以使用以下xml执行此操作

<RelativeLayout
    style="@style/GenericProgressBackground"
    android:id="@+id/loadingPanel"
    >
    <ProgressBar
        style="@style/GenericProgressIndicator"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

有了这种风格

<style name="GenericProgressBackground" parent="android:Theme">
    <item name="android:layout_width">fill_parent</item>    
    <item name="android:layout_height">fill_parent</item>
    <item name="android:background">#DD111111</item>    
    <item name="android:gravity">center</item>  
</style>
<style name="GenericProgressIndicator" parent="@android:style/Widget.ProgressBar.Small">
    <item name="android:layout_width">wrap_content</item>
    <item name="android:layout_height">wrap_content</item>
    <item name="android:indeterminate">true</item> 
</style>
Run Code Online (Sandbox Code Playgroud)

要使用此功能,您必须通过将可见性值设置为GONE来隐藏UI元素,并且无论何时加载数据,setVisibility(View.VISIBLE)都要调用所有视图以恢复它们.别忘了打电话findViewById(R.id.loadingPanel).setVisiblity(View.GONE)来隐藏加载动画.

如果您没有加载事件/功能但只是希望加载面板在x秒后消失,请使用Handle触发隐藏/显示.

  • 很好的答案.通过谷歌搜索找到,并解决了我的问题.谢谢! (4认同)

use*_*209 10

这通常称为"不确定进度条"或"不确定进度对话框".

将它与ThreadHandler相结合,可以得到你想要的.有很多关于如何通过Google或在此处进行此操作的示例.我强烈建议花时间学习如何使用这种类组合来执行这样的任务.它在许多类型的应用程序中都非常有用,可以让您深入了解线程和处理程序如何协同工作.

我会告诉你这是如何工作的:

加载事件启动对话框:

//maybe in onCreate
showDialog(MY_LOADING_DIALOG);
fooThread = new FooThread(handler);
fooThread.start();
Run Code Online (Sandbox Code Playgroud)

现在线程完成了工作:

private class FooThread extends Thread {
    Handler mHandler;

    FooThread(Handler h) {
        mHandler = h;
    }

    public void run() { 
        //Do all my work here....you might need a loop for this

        Message msg = mHandler.obtainMessage();
        Bundle b = new Bundle();                
        b.putInt("state", 1);   
        msg.setData(b);
        mHandler.sendMessage(msg);
    }
}
Run Code Online (Sandbox Code Playgroud)

最后在线程完成后从线程返回状态:

final Handler handler = new Handler() {
    public void handleMessage(Message msg) {
        int state = msg.getData().getInt("state");
        if (state == 1){
            dismissDialog(MY_LOADING_DIALOG);
            removeDialog(MY_LOADING_DIALOG);
        }
    }
};
Run Code Online (Sandbox Code Playgroud)

  • 你的答案看起来真的很好,但我想把它插入我的布局...... Venkatesh的答案似乎更适合我的使用.我会尝试发送反馈.非常感谢你的时间! (2认同)