android中的自定义进度条就像gif一样

时间:2015-09-22 04:13:19

标签: android progress-bar android-animation

如何在没有图像的android中创建这种类型的进度条?我还需要显示渐变效果。 http://goldesel.to/img/loading.gif

1 个答案:

答案 0 :(得分:5)

  

进度对话框,显示进度指示器和可选的文本消息或视图。只能同时使用短信或视图。

创建asyncTask类:ShowCustomProgressBarAsyncTask

public class ShowCustomProgressBarAsyncTask extends AsyncTask < Void,Integer,Void > {
    int myProgress;
    @Override
    protected void onPostExecute(Void result) {
        textview.setText("Finish work with custom ProgressBar");
        button1.setEnabled(true);
        progressBar2.setVisibility(View.INVISIBLE);
    }
    @Override
    protected void onPreExecute() {
        button1.setEnabled(false);
        textview.setText("Start work with custom ProgressBar");
        myProgress = 0;
        progressBar.setSecondaryProgress(0);
    }
    @Override
    protected Void doInBackground(Void...params) {
        while (myProgress < 100) {
            myProgress++;
            publishProgress(myProgress);
            SystemClock.sleep(100);
        }
        return null;
    }@Override
    protected void onProgressUpdate(Integer...values) {
        progressBar.setProgress(values[0]);
        progressBar.setSecondaryProgress(values[0] + 5);
    }
}

在文件res / drawable / custom_progress_bar_horizo​​ntal.xml

<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />

            <gradient
                android:angle="270"
                android:centerColor="#ffdddddd"
                android:centerY="0.50"
                android:endColor="#ffffffff"
                android:startColor="#ffffffff" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="90"
                    android:endColor="#771997e1"
                    android:startColor="#770e75af" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />

                <gradient
                    android:angle="90"
                    android:endColor="#ff1997e1"
                    android:startColor="#ff0e75af" />
            </shape>
        </clip>
    </item>

</layer-list>

输出:

enter image description here

有关更多信息,请参阅this