Android使用View Animation實(shí)現(xiàn)動(dòng)畫加載界面
之前分別介紹了View Animation和Drawable Animation,學(xué)了就要用啊,今天給大家一個(gè)使用View Animation實(shí)現(xiàn)動(dòng)畫加載界面的實(shí)現(xiàn)。
首先先看一下實(shí)現(xiàn)效果。

下面是實(shí)現(xiàn)代碼
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: 動(dòng)畫加載Dialog
* @author zhaokaiqiang
* @date 2014-10-27 下午4:42:52
*
*/
public class LoadingDialog extends Dialog {
protected static final String TAG = "LoadingDialog";
// 動(dòng)畫間隔
private static final int DURATION = 800;
// 前景圖片
private ImageView img_front;
// 定時(shí)器,用來不斷的播放動(dòng)畫
private Timer animationTimer;
// 旋轉(zhuǎn)動(dòng)畫
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();
// 從左到右的旋轉(zhuǎn)動(dòng)畫,設(shè)置旋轉(zhuǎn)角度和旋轉(zhuǎn)中心
animationL2R = new RotateAnimation(0f, -90f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// 設(shè)置動(dòng)畫的運(yùn)行時(shí)長
animationL2R.setDuration(DURATION);
// 動(dòng)畫運(yùn)行結(jié)束之后,保存結(jié)束之后的狀態(tài)
animationL2R.setFillAfter(true);
// 設(shè)置重復(fù)的次數(shù)
animationL2R.setRepeatCount(1);
//設(shè)置重復(fù)模式為逆運(yùn)動(dòng)
animationL2R.setRepeatMode(Animation.REVERSE);
// 執(zhí)行間隔任務(wù),開始間隔是0,每隔DURATION * 2執(zhí)行一次
animationTimer.schedule(new TimerTask() {
@Override
public void run() {
handler.sendEmptyMessage(1);
}
}, 0, DURATION * 2);
}
@Override
protected void onStop() {
super.onStop();
animationTimer.cancel();
}
}
當(dāng)然,除了這種直接使用代碼的硬編碼方式,哦們還可以使用xml的方式,和硬編碼基本類似,把需要的屬性在xml里面定義好即可,下面的代碼實(shí)現(xiàn)。
<?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: 動(dòng)畫加載Dialog
* @author zhaokaiqiang
* @date 2014-10-27 下午4:42:52
*
*/
public class LoadingDialog extends Dialog {
protected static final String TAG = "LoadingDialog";
// 動(dòng)畫間隔
private static final int DURATION = 800;
// 前景圖片
private ImageView img_front;
// 定時(shí)器,用來不斷的播放動(dòng)畫
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);
// 執(zhí)行間隔任務(wù),開始間隔是0,每隔DURATION * 2執(zhí)行一次
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> <!-- 是否浮現(xiàn)在activity之上 --> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
下載:項(xiàng)目地址
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Android自定義加載圈動(dòng)畫效果
- Android實(shí)現(xiàn)笑臉進(jìn)度加載動(dòng)畫
- Android實(shí)現(xiàn)仿微軟系統(tǒng)加載動(dòng)畫效果
- android實(shí)現(xiàn)加載動(dòng)畫對(duì)話框
- Android 自定義加載動(dòng)畫Dialog彈窗效果的示例代碼
- android自定義波浪加載動(dòng)畫的實(shí)現(xiàn)代碼
- Android仿視頻加載旋轉(zhuǎn)小球動(dòng)畫效果的實(shí)例代碼
- Android 使用 Path 實(shí)現(xiàn)搜索動(dòng)態(tài)加載動(dòng)畫效果
- Android使用lottie加載json動(dòng)畫的示例代碼
- Android實(shí)現(xiàn)仿iOS菊花加載圈動(dòng)畫效果
相關(guān)文章
安卓(Android)實(shí)現(xiàn)選擇時(shí)間功能
安卓開發(fā)過程中難免會(huì)碰到需要選擇日期時(shí)間的情況,當(dāng)然不可能讓用戶自己輸入日期時(shí)間,小編收集整理了一些資料,總結(jié)了一下如何實(shí)現(xiàn)android選擇時(shí)間的功能,方便后來者參考2016-08-08
Android編程調(diào)用系統(tǒng)自帶的拍照功能并返回JPG文件示例【附demo源碼下載】
這篇文章主要介紹了Android編程調(diào)用系統(tǒng)自帶的拍照功能并返回JPG文件,結(jié)合實(shí)例形式分析了Android的拍照功能調(diào)用及圖形文件操作技巧,并附帶demo源碼供讀者下載參考,需要的朋友可以參考下2017-07-07
Android?RecyclerChart其它圖表繪制示例詳解
這篇文章主要為大家介紹了Android?RecyclerChart其它圖表繪制示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android自定義加載loading view動(dòng)畫組件
這篇文章主要為大家詳細(xì)介紹了Android自定義加載loading view動(dòng)畫組件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08
Gradle屬性設(shè)置及環(huán)境變量全面教程
這篇文章主要為大家介紹了Gradle屬性設(shè)置及環(huán)境變量的全面教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄
通常手機(jī)通訊錄都會(huì)有索引欄,這篇文章主要介紹了Android自定義View實(shí)現(xiàn)字母導(dǎo)航欄,現(xiàn)在分享給大家。2016-10-10

