android 动画间隔时间,Android使用View Animation实现动画加载界面

之前分别介绍了View Animation和Drawable Animation,学了就要用啊,今天给大家一个使用View Animation实现动画加载界面的实现。

首先先看一下实现效果。

87ff2fde84ca2572b2a80b9bca414922.gif

下面是实现代码

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里面定义好即可,下面的代码实现。

android:duration="800"

android:fillAfter="true"

android:fromDegrees="0"

android:pivotX="50%"

android:pivotY="50%"

android:repeatCount="1"

android:repeatMode="reverse"

android:toDegrees="-90" >

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

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的样式

@android:color/transparent

@null

true

true

@null

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值