android 使用View Animation实现动画加载界面

简介:

转载请注明出处:http://blog.csdn.net/zhaokaiqiang1992

今天给大家一个使用View Animation实现动画加载界面的实现。

    首先先看一下实现效果。

    


    下面是实现代码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.animationloading;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.annotation.SuppressLint;  
  7. import android.app.Dialog;  
  8. import android.content.Context;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.view.animation.Animation;  
  13. import android.view.animation.RotateAnimation;  
  14. import android.widget.ImageView;  
  15.   
  16. /** 
  17.  *  
  18. * @ClassName:  com.example.animationloading.LoadingDialog 
  19. * @Description: 动画加载Dialog 
  20. * @author zhaokaiqiang 
  21. * @date 2014-10-27 下午4:42:52 
  22. * 
  23.  */  
  24. public class LoadingDialog extends Dialog {  
  25.   
  26.     protected static final String TAG = "LoadingDialog";  
  27.     // 动画间隔  
  28.     private static final int DURATION = 800;  
  29.     // 前景图片  
  30.     private ImageView img_front;  
  31.     // 定时器,用来不断的播放动画  
  32.     private Timer animationTimer;  
  33.     // 旋转动画  
  34.     private RotateAnimation animationL2R;  
  35.   
  36.     @SuppressLint("HandlerLeak")  
  37.     private Handler handler = new Handler() {  
  38.   
  39.         public void handleMessage(Message msg) {  
  40.             img_front.setAnimation(animationL2R);  
  41.             animationL2R.start();  
  42.         };  
  43.   
  44.     };  
  45.   
  46.     public LoadingDialog(Context context) {  
  47.         super(context, R.style.dialog);  
  48.     }  
  49.   
  50.     @Override  
  51.     protected void onCreate(Bundle savedInstanceState) {  
  52.         super.onCreate(savedInstanceState);  
  53.         setContentView(R.layout.dialog_loading);  
  54.   
  55.         img_front = (ImageView) findViewById(R.id.img_front);  
  56.         animationTimer = new Timer();  
  57.   
  58.         // 从左到右的旋转动画,设置旋转角度和旋转中心  
  59.         animationL2R = new RotateAnimation(0f, -90f,  
  60.                 Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,  
  61.                 0.5f);  
  62.         // 设置动画的运行时长  
  63.         animationL2R.setDuration(DURATION);  
  64.         // 动画运行结束之后,保存结束之后的状态  
  65.         animationL2R.setFillAfter(true);  
  66.         // 设置重复的次数  
  67.         animationL2R.setRepeatCount(1);  
  68.         //设置重复模式为逆运动  
  69.         animationL2R.setRepeatMode(Animation.REVERSE);  
  70.         // 执行间隔任务,开始间隔是0,每隔DURATION * 2执行一次  
  71.         animationTimer.schedule(new TimerTask() {  
  72.   
  73.             @Override  
  74.             public void run() {  
  75.                 handler.sendEmptyMessage(1);  
  76.             }  
  77.         }, 0, DURATION * 2);  
  78.   
  79.     }  
  80.   
  81.     @Override  
  82.     protected void onStop() {  
  83.         super.onStop();  
  84.         animationTimer.cancel();  
  85.     }  
  86.   
  87. }  

    当然,除了这种直接使用代码的硬编码方式,哦们还可以使用xml的方式,和硬编码基本类似,把需要的属性在xml里面定义好即可,下面的代码实现。

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <rotate xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:duration="800"  
  4.     android:fillAfter="true"  
  5.     android:fromDegrees="0"  
  6.     android:pivotX="50%"  
  7.     android:pivotY="50%"  
  8.     android:repeatCount="1"  
  9.     android:repeatMode="reverse"  
  10.     android:toDegrees="-90" >  
  11.   
  12. </rotate>  

    如果使用这种方式,那么,上面的代码就要变成下面这种了。

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.example.animationloading;  
  2.   
  3. import java.util.Timer;  
  4. import java.util.TimerTask;  
  5.   
  6. import android.annotation.SuppressLint;  
  7. import android.app.Dialog;  
  8. import android.content.Context;  
  9. import android.os.Bundle;  
  10. import android.os.Handler;  
  11. import android.os.Message;  
  12. import android.view.animation.Animation;  
  13. import android.view.animation.AnimationUtils;  
  14. import android.widget.ImageView;  
  15.   
  16. /** 
  17.  *  
  18.  * @ClassName: com.example.animationloading.LoadingDialog 
  19.  * @Description: 动画加载Dialog 
  20.  * @author zhaokaiqiang 
  21.  * @date 2014-10-27 下午4:42:52 
  22.  *  
  23.  */  
  24. public class LoadingDialog extends Dialog {  
  25.   
  26.     protected static final String TAG = "LoadingDialog";  
  27.     // 动画间隔  
  28.     private static final int DURATION = 800;  
  29.     // 前景图片  
  30.     private ImageView img_front;  
  31.     // 定时器,用来不断的播放动画  
  32.     private Timer animationTimer;  
  33.   
  34.     private Animation animation;  
  35.   
  36.     private Context context;  
  37.   
  38.     @SuppressLint("HandlerLeak")  
  39.     private Handler handler = new Handler() {  
  40.   
  41.         public void handleMessage(Message msg) {  
  42.             img_front.setAnimation(animation);  
  43.             animation.start();  
  44.         };  
  45.   
  46.     };  
  47.   
  48.     public LoadingDialog(Context context) {  
  49.         super(context, R.style.dialog);  
  50.         this.context = context;  
  51.     }  
  52.   
  53.     @Override  
  54.     protected void onCreate(Bundle savedInstanceState) {  
  55.         super.onCreate(savedInstanceState);  
  56.         setContentView(R.layout.dialog_loading);  
  57.   
  58.         img_front = (ImageView) findViewById(R.id.img_front);  
  59.         animationTimer = new Timer();  
  60.   
  61.         animation = AnimationUtils.loadAnimation(context,  
  62.                 R.anim.anim_load_dialog);  
  63.           
  64.         // 执行间隔任务,开始间隔是0,每隔DURATION * 2执行一次  
  65.         animationTimer.schedule(new TimerTask() {  
  66.   
  67.             @Override  
  68.             public void run() {  
  69.                 handler.sendEmptyMessage(1);  
  70.             }  
  71.         }, 0, DURATION * 2);  
  72.   
  73.     }  
  74.   
  75.     @Override  
  76.     protected void onStop() {  
  77.         super.onStop();  
  78.         animationTimer.cancel();  
  79.     }  
  80.   
  81. }  

      下面是dialog的样式

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <style name="dialog" parent="android:style/Theme.Dialog">  
  2.   
  3.        <!-- 背景颜色及透明程度 -->  
  4.        <item name="android:windowBackground">@android:color/transparent</item>  
  5.        <item name="android:windowFrame">@null</item>  
  6.        <item name="android:windowNoTitle">true</item>  
  7.        <!-- 是否浮现在activity之上 -->  
  8.        <item name="android:windowIsFloating">true</item>  
  9.        <item name="android:windowContentOverlay">@null</item>  
  10.    </style>  

    github的项目地址: https://github.com/ZhaoKaiQiang/LoadingDialog
相关文章
|
3月前
|
Android开发 容器
Android UI设计: 什么是View和ViewGroup?
Android UI设计: 什么是View和ViewGroup?
36 0
|
4月前
|
XML API Android开发
Android 自定义View 之 圆环进度条
Android 自定义View 之 圆环进度条
|
2月前
|
Android开发 数据安全/隐私保护
【Android Studio】简单的QQ登录界面
【Android Studio】简单的QQ登录界面
|
3月前
|
XML 开发工具 Android开发
Android动画效果-更新中
Android动画效果-更新中
59 1
|
4月前
|
XML API Android开发
Android 自定义View 之 Dialog弹窗
Android 自定义View 之 Dialog弹窗
|
4月前
|
XML API Android开发
Android 自定义View 之 饼状进度条
Android 自定义View 之 饼状进度条
|
4月前
|
XML API Android开发
Android 自定义View 之 简易输入框
Android 自定义View 之 简易输入框
|
4月前
|
XML API Android开发
Android 自定义View 之 计时文字
Android 自定义View 之 计时文字
|
4月前
|
XML Android开发 数据格式
Android 自定义View 之 Mac地址输入框(下)
Android 自定义View 之 Mac地址输入框(下)
|
4月前
|
XML Java Android开发
Android 自定义View 之 Mac地址输入框(上)
Android 自定义View 之 Mac地址输入框(上)