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

Android 手把手實作:打造一個透明背景的進度對話框

編輯:關於 Android 程式設計

先來看看我們要做出來的「透明進度對話框」長什麼樣子——它沒有傳統對話框的邊框與背景,只有一個持續旋轉的加載動畫,乾淨又低調。

實作步驟

1、建立專案
用 Eclipse 建立一個新的 Android 專案,建議使用 Android 2.2 或以上版本。

2、建立版面配置 activity_main.xml
在 /res/layout 資料夾建立線性版面配置,主要用來加入一個文字標籤和一個按鈕:

複製程式碼 程式碼如下:
activity_main.xml
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:padding="8dp"
        android:textSize="20sp"
        android:text="Transparent Progress Indicator" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Check it out!"
        android:layout_marginTop="40dp"
        android:layout_gravity="center"
        android:id="@+id/the_button" />

</LinearLayout>

3、在 styles.xml 中定義透明對話框樣式
在 /res/values 中打開 styles.xml,加入透明對話框的樣式。請務必指定 parent 屬性,否則運行時會出問題:

複製程式碼 程式碼如下:
styles.xml
<resources>

    <style name="AppBaseTheme" parent="android:Theme.Light">
    </style>

    <style name="AppTheme" parent="AppBaseTheme">
    </style>

    <!--  Transparent dialog -->
    <style name="TransparentProgressDialog" parent="@android:Theme.Dialog">
        <item name="android:windowFrame">@null</item>
        <item name="android:windowBackground">@android:color/transparent</item>
        <item name="android:windowIsFloating">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowTitleStyle">@null</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:background">@android:color/transparent</item>
    </style>

</resources>

4、加入旋轉動畫圖片
在 /res 中放入一張會動態旋轉的加載圖片(如 spinner)。

5、實作 MainActivity.java

複製程式碼 程式碼如下:
MainActivity.java
package com.authorwjf.transparentprogressdialog;

import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.LinearInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnClickListener {

 private TransparentProgressDialog pd;
 private Handler h;
 private Runnable r;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  h = new Handler();
  pd = new TransparentProgressDialog(this, R.drawable.spinner);
  r =new Runnable() {
   @Override
   public void run() {
    if (pd.isShowing()) {
     pd.dismiss();
    }
   }
  };
  findViewById(R.id.the_button).setOnClickListener(this);
 }

 @Override
 public void onClick(View v) {
  pd.show();
  h.postDelayed(r,5000);
 }

 @Override
 protected void onDestroy() {
  h.removeCallbacks(r);
  if (pd.isShowing() ) {
   pd.dismiss();
  }
  super.onDestroy();
 }

}

6、實作透明動畫的核心程式碼

複製程式碼 程式碼如下:
private class TransparentProgressDialog extends Dialog {

 private ImageView iv;

 public TransparentProgressDialog(Context context, int resourceIdOfImage) {
  super(context, R.style.TransparentProgressDialog);
  WindowManager.LayoutParams wlmp = getWindow().getAttributes();
  wlmp.gravity = Gravity.CENTER_HORIZONTAL;
  getWindow().setAttributes(wlmp);
  setTitle(null);
  setCancelable(false);
  setOnCancelListener(null);
  LinearLayout layout = new LinearLayout(context);
  layout.setOrientation(LinearLayout.VERTICAL);
  LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
  iv = new ImageView(context);
  iv.setImageResource(resourceIdOfImage);
  layout.addView(iv, params);
  addContentView(layout, params);
 }

 @Override
 public void show() {
  super.show();
  RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
  anim.setInterpolator(new LinearInterpolator());
  anim.setRepeatCount(Animation.INFINITE);
  anim.setDuration(3000);
  iv.setAnimation(anim);
  iv.startAnimation(anim);
 }
}

關鍵在於:自定義 Dialog 沿用 TransparentProgressDialog 樣式,去掉視窗邊框與背景;show() 時為 ImageView 掛上一個無限循環的 RotateAnimation,就能呈現持續旋轉的加載效果。點擊按鈕後弹出,5 秒後自動關閉。最終效果如下:

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