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

所属分类: 软件编程 / Android 阅读数: 43
收藏 0 赞 0 分享

之前分别介绍了View Animation和Drawable Animation,学了就要用啊,今天给大家一个使用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>

下载:项目地址

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

更多精彩内容其他人还在看

Android中加入名片扫描功能实例代码

这篇文章主要介绍了Android中加入名片扫描功能实例代码的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android仿微信发表说说实现拍照、多图上传功能

这篇文章主要为大家详细介绍了Android仿微信发表说说实现拍照、多图上传功能,使用Retrofit2.0技术,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

设置Android系统永不锁屏永不休眠的方法

在进行Android系统开发的时候,有些特定的情况需要设置系统永不锁屏,永不休眠。本篇文章给大家介绍Android 永不锁屏,开机不锁屏,删除设置中休眠时间选项,需要的朋友一起学习吧
收藏 0 赞 0 分享

Android Retrofit 2.0框架上传图片解决方案

这篇文章主要介绍了Android Retrofit 2.0框架上传一张与多张图片解决方案,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android自定义等待对话框

这篇文章主要为大家详细介绍了Android自定义等待对话框的实现方法,感兴趣的小伙伴们可以参考一下
收藏 0 赞 0 分享

Android中Window添加View的底层原理

这篇文章主要介绍了Android中Window添加View的底层原理,需要的朋友可以参考下
收藏 0 赞 0 分享

Android调用系统默认浏览器访问的方法

这篇文章主要介绍了Android调用系统默认浏览器访问的方法的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享

Android开发退出程序的方法汇总

Android程序有很多Activity,比如说主窗口A,调用了子窗口B,子窗口B又调用子窗口C,back返回子窗口B后,在B中如何关闭整个Android应用程序呢? 下面脚本之家小编就给大家介绍android开发退出程序的几种方法,感兴趣的朋友参考下吧
收藏 0 赞 0 分享

Android程序开发中单选按钮(RadioGroup)的使用详解

在android程序开发中,无论是单选按钮还是多选按钮都非常的常见,接下来通过本文给大家介绍Android程序开发中单选按钮(RadioGroup)的使用,需要的朋友参考下吧
收藏 0 赞 0 分享

Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中

这篇文章主要介绍了Android实现仿网易今日头条等自定义频道listview 或者grideview等item上移到另一个view中 的相关资料,需要的朋友可以参考下
收藏 0 赞 0 分享
查看更多