国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android使用View Animation實現動畫加載界面

2019-10-22 18:12:46
字體:
來源:轉載
供稿:網友

之前分別介紹了View Animation和Drawable Animation,學了就要用啊,今天給大家一個使用View Animation實現動畫加載界面的實現。

首先先看一下實現效果。

Android,View,Animation,動畫加載

下面是實現代碼

package com.example.animationloading;  import java.util.Timer; import java.util.TimerTask;  import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.animation.Animation; import android.view.animation.RotateAnimation; import android.widget.ImageView;  /**  * * @ClassName: com.example.animationloading.LoadingDialog * @Description: 動畫加載Dialog * @author zhaokaiqiang * @date 2014-10-27 下午4:42:52 *  */ public class LoadingDialog extends Dialog {   protected static final String TAG = "LoadingDialog";  // 動畫間隔  private static final int DURATION = 800;  // 前景圖片  private ImageView img_front;  // 定時器,用來不斷的播放動畫  private Timer animationTimer;  // 旋轉動畫  private RotateAnimation animationL2R;   @SuppressLint("HandlerLeak")  private Handler handler = new Handler() {    public void handleMessage(Message msg) {    img_front.setAnimation(animationL2R);    animationL2R.start();   };   };   public LoadingDialog(Context context) {   super(context, R.style.dialog);  }   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.dialog_loading);    img_front = (ImageView) findViewById(R.id.img_front);   animationTimer = new Timer();    // 從左到右的旋轉動畫,設置旋轉角度和旋轉中心   animationL2R = new RotateAnimation(0f, -90f,     Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,     0.5f);   // 設置動畫的運行時長   animationL2R.setDuration(DURATION);   // 動畫運行結束之后,保存結束之后的狀態   animationL2R.setFillAfter(true);   // 設置重復的次數   animationL2R.setRepeatCount(1);   //設置重復模式為逆運動   animationL2R.setRepeatMode(Animation.REVERSE);   // 執行間隔任務,開始間隔是0,每隔DURATION * 2執行一次   animationTimer.schedule(new TimerTask() {     @Override    public void run() {     handler.sendEmptyMessage(1);    }   }, 0, DURATION * 2);   }   @Override  protected void onStop() {   super.onStop();   animationTimer.cancel();  }  } 

當然,除了這種直接使用代碼的硬編碼方式,哦們還可以使用xml的方式,和硬編碼基本類似,把需要的屬性在xml里面定義好即可,下面的代碼實現。

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android"  android:duration="800"  android:fillAfter="true"  android:fromDegrees="0"  android:pivotX="50%"  android:pivotY="50%"  android:repeatCount="1"  android:repeatMode="reverse"  android:toDegrees="-90" >  </rotate> 

如果使用這種方式,那么,上面的代碼就要變成下面這種了。

package com.example.animationloading;  import java.util.Timer; import java.util.TimerTask;  import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView;  /**  *  * @ClassName: com.example.animationloading.LoadingDialog  * @Description: 動畫加載Dialog  * @author zhaokaiqiang  * @date 2014-10-27 下午4:42:52  *  */ public class LoadingDialog extends Dialog {   protected static final String TAG = "LoadingDialog";  // 動畫間隔  private static final int DURATION = 800;  // 前景圖片  private ImageView img_front;  // 定時器,用來不斷的播放動畫  private Timer animationTimer;   private Animation animation;   private Context context;   @SuppressLint("HandlerLeak")  private Handler handler = new Handler() {    public void handleMessage(Message msg) {    img_front.setAnimation(animation);    animation.start();   };   };   public LoadingDialog(Context context) {   super(context, R.style.dialog);   this.context = context;  }   @Override  protected void onCreate(Bundle savedInstanceState) {   super.onCreate(savedInstanceState);   setContentView(R.layout.dialog_loading);    img_front = (ImageView) findViewById(R.id.img_front);   animationTimer = new Timer();    animation = AnimationUtils.loadAnimation(context,     R.anim.anim_load_dialog);      // 執行間隔任務,開始間隔是0,每隔DURATION * 2執行一次   animationTimer.schedule(new TimerTask() {     @Override    public void run() {     handler.sendEmptyMessage(1);    }   }, 0, DURATION * 2);   }   @Override  protected void onStop() {   super.onStop();   animationTimer.cancel();  }  } 

下面是dialog的樣式

<style name="dialog" parent="android:style/Theme.Dialog">    <!-- 背景顏色及透明程度 -->   <item name="android:windowBackground">@android:color/transparent</item>   <item name="android:windowFrame">@null</item>   <item name="android:windowNoTitle">true</item>   <!-- 是否浮現在activity之上 -->   <item name="android:windowIsFloating">true</item>   <item name="android:windowContentOverlay">@null</item>  </style>

下載:項目地址

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 黑水县| 朝阳市| 竹山县| 平果县| 集贤县| 都匀市| 阿荣旗| 芜湖市| 桑日县| 电白县| 大英县| 遵义市| 湄潭县| 汉阴县| 蓝田县| 临洮县| 耿马| 阜阳市| 海宁市| 衡阳县| 盐亭县| 滕州市| 顺平县| 泾阳县| 沽源县| 讷河市| 甘孜县| 台中县| 泰来县| 宁德市| 忻城县| 吐鲁番市| 天等县| 榆林市| 永平县| 清水县| 广元市| 丹阳市| 威宁| 玉溪市| 赤壁市|