使用View Animation实现动画加载界面
2015-12-14 22:06
281 查看
转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992
之前的两篇文章,分别介绍了View Animation和Drawable Animation,学了就要用啊,今天给大家一个使用View Animation实现动画加载界面的实现。
首先先看一下实现效果。
![](http://img.blog.csdn.net/20141027135613606?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvemhhb2thaXFpYW5nMTk5Mg==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/SouthEast)
下面是实现代码
[java] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
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里面定义好即可,下面的代码实现。
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<?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>
如果使用这种方式,那么,上面的代码就要变成下面这种了。
[java] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
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的样式
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
![](https://code.csdn.net/assets/ico_fork.svg)
<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>
github的项目地址:https://github.com/ZhaoKaiQiang/LoadingDialog
之前的两篇文章,分别介绍了View Animation和Drawable Animation,学了就要用啊,今天给大家一个使用View Animation实现动画加载界面的实现。
首先先看一下实现效果。
下面是实现代码
[java] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
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里面定义好即可,下面的代码实现。
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
<?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>
如果使用这种方式,那么,上面的代码就要变成下面这种了。
[java] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
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的样式
[html] view
plaincopy
![](https://code.csdn.net/assets/CODE_ico.png)
<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>
github的项目地址:https://github.com/ZhaoKaiQiang/LoadingDialog
相关文章推荐
- iOS RAC下的MVVM
- 广播接收器
- 《一个操作系统的实现》学习笔记(一) bochs源码安装及配置
- prompt()、 isNaN()函数简介
- Android:Fragment 初步之旅 随记一
- Kurento应用开发指南(以Kurento 5.0为模板) 之四:示例教程 一对一视频呼叫
- 关于dip、dpi和分辨率的一些事
- gtest的安装和使用
- 界面布局动态调整
- Android学习笔记——SwipeRefreshLayout 官方下拉刷新控件介绍
- iOS 观察者模式
- Kurento应用开发指南(以Kurento 5.0为模板) 之三:示例教程 一对多的视频呼叫
- 八大排序算法(图文)
- 学生成绩管理系统
- sizeof 运算符
- iOS_开发中遇到的那些问题_5
- 九度oj 1380 lucky number + 二进制
- Java多线程-新特征-锁(下)
- Dropping and creating tables in read only tablespaces?!
- for循环练习