android三大动画的基本使用

逐帧动画

1.最重要:自定义一个drawable->animation.xml

<?xml version="1.0" encoding="utf-8"?>
<!--
    根标签为animation-list,其中oneshot代表着是否只展示一遍,设置为false会不停的循环播放动画
    根标签下,通过item标签对动画中的每一个图片进行声明
    android:duration 表示展示所用的该图片的时间长度
 -->
<animation-list
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true"//一次播放还是循环播放
    >
    <item android:drawable="@drawable/icon1" android:duration="150"></item>
    <item android:drawable="@drawable/icon2" android:duration="150"></item>
    <item android:drawable="@drawable/icon3" android:duration="150"></item>
    <item android:drawable="@drawable/icon4" android:duration="150"></item>
    <item android:drawable="@drawable/icon5" android:duration="150"></item>
    <item android:drawable="@drawable/icon6" android:duration="150"></item>
</animation-list>

2.创建AnimationDrawable 对象

//XML里联系刚创建的drawable
   <ImageView android:id="@+id/animationIV"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="5px"
        android:src="@drawable/animation1"/>

//java代码
AnimationDrawable anim=(AnimationDrawable)imageview.getDrawable();

注意:
代码使用imageview.getDrawable获取AnimationDrawable 对象
layout中属性就需要使用android:src=”@drawable/animation”
代码使用imageview.getBackground获取AnimationDrawable 对象
layout中属性就需要使用android:background=”@drawable/animation”

3.启动动画

anim.start();//启动需要在某个监听器内,不能放在onCreate方法内执行,因为AnimationDrawable 类在onCreate方法前还没加载完

4.代码中添加帧可用addFrame()


补间动画

  • 移动补间动画:TranslateAnimation
TranslateAnimation animTran=new TranslateAnimation(currX,nextX,currY,nextY);
animTran.setDuration(200);
imageView.startAnimation(animTran);

ps: 补间动画只是将view绘制在目标位置,并不是将view真实移动到目标位置,所以监听器什么的响应不了

  • 透明补间动画: AlphaAnimation
Animation animation = new AlphaAnimation(fromAlpha,toAlpha);
//fromAlpha起始透明度,toAlpha目标透明度
  • 旋转补间动画:RotateAnimation
 Animation animation  = new RotateAnimation(
360,0,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 
/*
参数1:旋转的起始角度
参数2:旋转的终止角度
参数3:旋转中心的x轴取值参照方式
参数4:中心点x轴的取值
参数5:旋转中心的y轴取值参照方式
参数6:中心点y轴的取值
*/
  • 缩放补间动画:ScaleAnimation
Animation   animation = new ScaleAnimation(1f,0.2f,1f,0.2f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f); 
/*
参数1:x方向起始大小(1f表示原图大小)
参数2:x方向终止大小(0.2f表示原图的0.2倍)
参数3:y方向起始大小(1f表示原图大小)
参数4:y方向终止大小(0.2f表示原图的0.2倍)
参数5:缩放中心点x轴取值的参照方式
参数6:中心点x轴的取值(0.5f表示相对与原图的0.5倍)
参数7:缩放中心点y轴取值参照方式
参数8:中心点y轴的取值(0.5f表示相对与原图的0.5倍)
*/

XML方法定义补间动画
anim_alpha.xml

1.<?xml version="1.0" encoding="utf-8"?>  
2.<set xmlns:android="http://schemas.android.com/apk/res/android"  
3.android:fillEnabled="true"  
4.android:fillAfter="true"  
5.   >    
6.    <alpha  
7.        android:duration="2000"  
8.        android:fromAlpha="1"  
9.        android:repeatCount="1"  
10.        android:repeatMode="reverse"  
11.        android:toAlpha="0" />  
12.</set> 

java代码联系xml

1.    Animation rotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);

属性动画

比较常用的几个动画类是:ValueAnimator,ObjectAnimator和AnimatorSet,其中ObjectAnimator继承自ValueAnimator

以下举三个例子简单使用

  • 1.改变一个对象(myObject)的translationY属性,让其沿着Y轴向上平移一段距离:它的高度,改动画在默认时间完成,可
    自定义时间,还可以自定义插值器和估值算法。
ObjectAnimator.ofFloat(myObject,"translationY",-myObject.getHeight()).start();
  • 2.改变一个对象的背景色属性。下面动画让背景色在3秒内实现0xffff8080到0xff8080ff的渐变,动画会无限循环且反转
ValueAnimator colorAnim=ObjectAnimator.ofInt(myObject,"backgroundColor",/*Red*/0xffff8080,/*Blue*/0xff8080ff);
colorAnim.setDuration(3000);
colorAnim.setEvaluator(new ArgbEvaluator());
colorAnim.setRepeatCount(ValueAnimator.INFINITE);//无限
colorAnim.setRepeatMode(ValueAnimator.REVERSE);//相反
colorAnim.start();
  • 3.动画集合,5秒对View的旋转,平移,缩放和透明度都进行改变。
AnimatorSet set=new AnimatorSet();
set.playTogether(
        ObjectAnimator.ofFloat(myView,"rotationX",0,360),
        ObjectAnimator.ofFloat(myView,"rotationY",0,180),
        ObjectAnimator.ofFloat(myView,"rotation",0,360),
        ObjectAnimator.ofFloat(myView,"translationX",0,90),
        ObjectAnimator.ofFloat(myView,"translationY",0,90),
        ObjectAnimator.ofFloat(myView,"scaleX",1,1.5f),
        ObjectAnimator.ofFloat(myView,"scaleY",1,0.5f),
        ObjectAnimator.ofFloat(myView,"alpha",1,0.25f,1)
);
set.setDuration(5*1000).start();

XML实现属性动画
在animator目录创建property_animator.xml文件

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:ordering="together">
    <objectAnimator
        android:propertyName="x"
        android:duration="300"
        android:valueTo="200"
        android:valueType="intType"/>
    <objectAnimator
        android:propertyName="y"
        android:duration="300"
        android:valueTo="300"
        android:valueType="intType"/>
</set>

java代码

      AnimatorSet set1=(AnimatorSet) AnimatorInflater.loadAnimator(AttrAnimaActvity.this,R.animator.property_animator);
        set1.setTarget(myView);
        set1.start();

在实际开发中建议使用代码来实现属性动画,因为用代码实现更简单

作者:吴梓轩:原文地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值