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

Android 自定義 Dialog:給彈窗加上 loading 動畫

編輯:關於 Android 程式設計

很多應用都會在等待數據時彈出一個帶有 loading 動畫的對話框,讓用戶知道系統正在忙。本篇演示兩種實現方式:一種是旋轉動畫(rotate),一種是逐幀動畫(frame animation)。

MainActivity

package cc.testview1; import android.os.Bundle; import android.app.Activity; /** * Demo描述: * 自定義Dialog,在Dialog中有動畫(旋轉動畫或者幀動畫)效果 */ public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //第一種-->rotate動畫 LoadingDialogFirst loadingDialogFirst=new LoadingDialogFirst(this,R.style.dialog); loadingDialogFirst.show(); //第二種-->frame動畫 //LoadingDialogSecond loadingDialogSecond=new LoadingDialogSecond(this,R.style.dialog); //loadingDialogSecond.show(); } }

LoadingDialogFirst(旋轉動畫)

這種做法在 show() 時加載一個 XML 定義的旋轉動畫並啟動,dismiss() 時取消動畫。

package cc.testview1; 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; public class LoadingDialogFirst extends Dialog { private ImageView mLoadingImageView; private Animation mLoadingAnimation; public LoadingDialogFirst(Context context, boolean cancelable,OnCancelListener cancelListener) { super(context, cancelable, cancelListener); } public LoadingDialogFirst(Context context, int theme) { super(context, theme); } public LoadingDialogFirst(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View loadingView=LayoutInflater.from(getContext()).inflate(R.layout.loading, null); mLoadingImageView=(ImageView) loadingView.findViewById(R.id.loadingImageView); setContentView(loadingView); } @Override public void show() { super.show(); mLoadingAnimation=AnimationUtils.loadAnimation(getContext(), R.anim.loadinganimfirst); mLoadingImageView.startAnimation(mLoadingAnimation); } @Override public void dismiss() { super.dismiss(); mLoadingAnimation.cancel(); } }

LoadingDialogSecond(逐幀動畫)

逐幀動畫有一個容易忽略的細節:必須把動畫的啟動放到 Handler 的 postDelayed 裡延遲執行,否則只會看到第一張圖片。

package cc.testview1; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.os.Handler; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; public class LoadingDialogSecond extends Dialog { private ImageView mLoadingImageView; private AnimationDrawable mLoadingAnimationDrawable; public LoadingDialogSecond(Context context, boolean cancelable,OnCancelListener cancelListener) { super(context, cancelable, cancelListener); } public LoadingDialogSecond(Context context, int theme) { super(context, theme); } public LoadingDialogSecond(Context context) { super(context); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); View loadingView=LayoutInflater.from(getContext()).inflate(R.layout.loading, null); mLoadingImageView=(ImageView) loadingView.findViewById(R.id.loadingImageView); mLoadingImageView.setImageResource(R.anim.loadinganimsecond); setContentView(loadingView); } @Override public void show() { super.show(); //注意將動畫的啟動放置在Handler中.否則只可看到第一張圖片 new Handler(){}.postDelayed(new Runnable() { @Override public void run() { mLoadingAnimationDrawable =(AnimationDrawable) mLoadingImageView.getDrawable(); mLoadingAnimationDrawable.start(); } }, 10); } @Override public void dismiss() { super.dismiss(); //結束幀動畫 mLoadingAnimationDrawable =(AnimationDrawable) mLoadingImageView.getDrawable(); mLoadingAnimationDrawable.stop(); } }

main.xml

<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" xmlns:tools="https://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" android:layout_centerHorizontal="true" android:layout_marginTop="30dip" /> </RelativeLayout>

loading.xml

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center" > <TextView android:id="@+id/loadingTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="loading..." /> <ImageView android:id="@+id/loadingImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/loadingTextView" android:src="@drawable/ic_launcher" /> </RelativeLayout>

loadinganimfirst.xml(旋轉動畫定義)

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="https://schemas.android.com/apk/res/android" > <rotate android:fromDegrees="90" android:toDegrees="-90" android:pivotX="50%" android:pivotY="50%" android:duration="4000" android:repeatCount="infinite" android:repeatMode="reverse" /> </set>

loadinganimsecond.xml(逐幀動畫定義)

<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="https://schemas.android.com/apk/res/android"> <item android:drawable="@drawable/photo1" android:duration="500" /> <item android:drawable="@drawable/photo2" android:duration="500" /> <item android:drawable="@drawable/photo3" android:duration="500" /> </animation-list>

兩種動畫各有適用場景:旋轉動畫簡單流暢,逐幀動畫則更靈活生動。根據需要挑一種即可。

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