怎么在Android中利用ViewAnimation实现一个动画加载界面-古蔺大橙子建站
RELATEED CONSULTING
相关咨询
选择下列产品马上在线沟通
服务时间:8:30-17:00
你可能遇到了下面的问题
关闭右侧工具栏

新闻中心

这里有您想知道的互联网营销解决方案
怎么在Android中利用ViewAnimation实现一个动画加载界面

本篇文章给大家分享的是有关怎么在Android中利用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; 
 

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

 
 
 

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

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 
 

以上就是怎么在Android中利用View Animation实现一个动画加载界面,小编相信有部分知识点可能是我们日常工作会见到或用到的。希望你能通过这篇文章学到更多知识。更多详情敬请关注创新互联行业资讯频道。


新闻名称:怎么在Android中利用ViewAnimation实现一个动画加载界面
新闻来源:http://scgulin.cn/article/iphpde.html