Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 自訂載入中對話框:為網路請求加上流暢回饋

編輯:關於 Android 程式設計

效果預覽

簡要說明

現在的 Android 應用幾乎離不開網路請求,而擁有良好互動體驗的程式,對這些耗時操作的處理尤其講究。一個得體的載入提示,能讓使用者清楚知道系統正在工作,而不是懷疑程式當掉了。

一、載入提示的版面

dialog_loading.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/dialog_view"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center">

    <ImageView
        android:id="@+id/img"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/transparent"
        android:src="@drawable/progress" />

    <TextView
        android:id="@+id/tipTextView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:text="數據加載中……" />
</LinearLayout>

這就是自訂的呈現版面,結構單純明瞭:最外層一個垂直排列的線性版面,裡面依序放 ImageView 與 TextView。

二、旋轉動畫

loading_animation.xml:

<?xml version="1.0" encoding="utf-8"?>
<set android:shareInterpolator="false" xmlns:android="https://schemas.android.com/apk/res/android">
    <rotate
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fromDegrees="0"
        android:toDegrees="+360"
        android:duration="1500"
        android:startOffset="-1"
        android:repeatMode="restart"
        android:repeatCount="-1"/>
</set>

這裡定義的是旋轉動畫的基本屬性,檔案需放在 res 底下的 anim 資料夾中(該資料夾需手動建立)。

三、自訂 CustomProgressDialog

package com.cc.customprogressdialog.util;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;

/**
 * Created by CC on 2017/2/4.
 */
public class CustomProgressDialog extends Dialog {

    Context context;
    private ImageView spaceshipImage;
    private Animation hyperspaceJumpAnimation;

    public CustomProgressDialog(Context context) {
        super(context);
        this.context = context;
    }

    public CustomProgressDialog(Context context, int theme) {
        super(context, theme);
        this.context = context;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        LayoutInflater inflater = LayoutInflater.from(context);
        View v = inflater.inflate(R.layout.dialog_loading, null);// 取得載入用 view
        LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 載入版面

        // main.xml 中的 ImageView
        spaceshipImage = (ImageView) v.findViewById(R.id.img);
        // 載入動畫
        hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation);
        // 以 ImageView 呈現動畫
        spaceshipImage.startAnimation(hyperspaceJumpAnimation);

        setCancelable(false);// 不允許以「返回鍵」取消

        setContentView(layout, new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));// 設定版面
    }
}

這個類別就是自訂的 ProgressDialog,關鍵步驟都已在註解中標明。

四、使用方式

package com.cc.customprogressdialog;

import android.os.Bundle;
import android.os.SystemClock;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import com.cc.customprogressdialog.util.CustomProgressDialog;

public class MainActivity extends AppCompatActivity {

    private Button btn;
    private CustomProgressDialog mProgressDialog;

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

        btn = (Button) findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                new AsyncTask<Void, Void, Void>() {
                    @Override
                    protected void onPreExecute() {
                        super.onPreExecute();
                        mProgressDialog = new CustomProgressDialog(MainActivity.this, R.style.loading_dialog);
                        mProgressDialog.show();
                    }

                    @Override
                    protected Void doInBackground(Void... voids) {
                        SystemClock.sleep(2000);
                        return null;
                    }

                    @Override
                    protected void onPostExecute(Void aVoid) {
                        super.onPostExecute(aVoid);
                        mProgressDialog.dismiss();
                    }
                }.execute();
            }
        });
    }
}

在主 Activity 中放一顆按鈕,點擊後啟動一個非同步任務來模擬網路耗時:任務開始前顯示對話框,任務結束後關閉它。

要注意的是,建立 CustomProgressDialog 時我傳入了一個 style。系統預設的樣式實在不合用,因此必須自行定義:

<!-- 自訂 loading dialog -->
<style name="loading_dialog" parent="android:style/Theme.Dialog">
    <item name="android:windowFrame">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">#00000000</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowContentOverlay">@null</item>
</style>

這些屬性的具體含義有興趣可自行查閱,這裡不逐一展開。

實作起來不複雜,但非常實用,希望對各位讀者有所幫助。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved