1. 程式人生 > >使用Animation list實現網路請求過程中的載入動畫dialog

使用Animation list實現網路請求過程中的載入動畫dialog

效果圖

如果圖片侵權請聯絡更改

實現過程主要是藉助於AnimationList實現幀動畫

animation_list.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<animation-list android:oneshot="false"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/progress_loading_image_01" android:duration="150"/>
<item android:drawable="@drawable/progress_loading_image_02" android:duration="150"/> <item android:drawable="@drawable/progress_loading_image_03" android:duration="150"/> </animation-list>

alert_dialog_loading.xml檔案

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android
="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="vertical" android:padding="10dp">
<ImageView android:id="@+id/loadingIv"
android:layout_width="wrap_content" android:layout_height="wrap_content"/>
<TextView android:id="@+id/loadingTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:textSize="18sp" android:textColor="#ffffff" /> </LinearLayout>

程式碼中的配置

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

        // 設定載入過程中的顯示文字
        createAnimationDailog(this,"拼命載入中……").show();

    }

    /**
     * 載入中的動畫
     * @param context 上下文
     * @param tips 載入動畫下面的提示資訊
     * @return 將對話方塊物件直接返回
     */
    public static Dialog createAnimationDailog(final Context context, String tips) {
        Dialog dialog = new Dialog(context, R.style.dialog);
        dialog.setContentView(R.layout.alert_dialog_loading);
        dialog.setCanceledOnTouchOutside(false);
        ImageView animationView = (ImageView) dialog.findViewById(R.id.loadingIv);
        TextView textView = (TextView) dialog.findViewById(R.id.loadingTv);
        textView.setText(tips);
        animationView.setBackgroundResource(R.drawable.animation_list);
        AnimationDrawable animationDrawable = (AnimationDrawable) animationView.getBackground();
        animationDrawable.start();
        return dialog;
    }

就這麼簡單的實現了自定義的載入動畫dialog!