补间( Tween)动画

意思:
只需要开发者设置好动画的开始与结束的关键帧 中间帧通过计算来补齐

分为4种:
①alpha 透明度
②scale 缩放
③translate 位置移动
④rotate 旋转动画
AnimationSet 多种结合
需要在res/anim 资源文件下

Tween(两者之间) 动画与 Interpolator(插值)

  • android使用Animation代表抽象动画类,它包括如下几个子类
    • AlphaAnimationAlphaAnimation :透明度改变的动画。需指定动画起、止的透明度(0~1)及持续时间。
    • ScaleAnimation大小缩放的动画。需指定动画起、止的缩放比(X、Y 轴向缩放参数)及持续时间。还应通过 pivotX、pivotY 指定缩放中心的坐标。
    • TranslateAnimation位移变化的动画。需指定动画起、止的位置(X、Y 坐标)及持续时间。
    • RotateAnimation旋转动画。需指定动画起、止的旋转角度(X、Y 轴向缩放参数)及持续时间。还应通过 pivotX、pivotY 指定旋转轴心的坐标。

  • InterPolator根据特定的算法计算出来正果动画所需动态插入帧密度和位置。它是一个接口,定义了所有Interpolator都需要实现的方法。
    • 开发者可通过实现 Interpolator 来控制动画的变化速度。

  • Android为Interpolator提供了如下几个实现类:
    • LinearInterpolator :动画以匀速改变。
    • AccelerateInterpolator:动画以匀加速改变。
    • AccelerteDecelerateIterpolator:动画的改变以匀加速开始,以匀减速结束,中间改变最快。
    • CycleInterpolator :动画循环播放特定的次数,变化速度按正统曲线改变。
    • DecelerateInterpolator :动画以匀减速改变。
    • BounceInterpolator 回弹
    • OvershootInterpolator


为了在动画资源文件中指定补间动画所使用的 Interpolator,定义动画的元素 <set.../>、<alpha.../>、<scale.../>、<translate.../>、<rotate.../> 都可指定一个 android:interpolator 属性以指定动画的变化速度。Android 系统的 R.anim 类中包含了大致常量,它们定义了不同的动画速度。如:

@android:linear_interpolator :匀速变换
@android:accelerate_interpolator :加速变换
@android:decelerate_interpolator :减速变换






1.....XMl文件实现 结合代码 res/anim

、、常用的几个属性
fillAfter:是否停止在动画结束时位置,如果是在set标签中,只有在set节点中设置才有效果
interpolator:插值器,如果是在set标签中,只有在set解ID呢设置才有效果
repeatCount:重复次数,不包括第一次,infinite(无限循环)
repratMode:重复模式,与repeatCount配合使用 :reverse 反转 restart重新启动

、alpha透明度alpha 透明度<alpha></alpha>
、scale比例缩放scale 比例缩放 <scale></scale>
、translate位置移动translate 位置移动<translate></translate>
android:duration="3000" 持续时间
android:fillAfter="true" 是否停在最后位置
android:fromYDelta="0" Y轴开始坐标,
android:toYDelta="-100%p" Y轴结束坐标,以像素为单位,如果是正数,表示向下运行,如果为负数,表示向上运行,%p表示相对父控件坐标,%表示相对于自己
android:interpolator="@android:anim/accelerate_interpolator" 匀加速运行
、rotate旋转动画rotate 旋转动画 <rotate></rotate>
、综合应用:<set></set> 辅助类 AnimationUtils.loadAnimation(context, id);

代码中应用动画
  1. 找到播放动画的控件:findViewById(R.id.flower);
  2. 通过AnimationUtils.loadAnimation(this, R.anim.anim);装载动画
  3. 开始动画:flower.startAnimation(anim);


2......Java代码实现 :
创建想要哪个控件实现的动画:如下
将控件设置动画 View.startAnimation(Animation);
  1. alpha 透明度 AlphaAnimation(AlphaAnimation alpha = new AlphaAnimation(1f, 0.2f);
    1. fromAlpha:开始透明度,
    2. toAlpha:结束透明度。 取值范围(全透明0.0f~不透明1.0f)

publicvoid alphacode(ViewcView) {
        AlphaAnimation alphaAnimation =new AlphaAnimation(1f, 0.2f);
        alphaAnimation.setDuration(3000);
        alphaAnimation.setRepeatCount(Animation.INFINITE);
        alphaAnimation.setRepeatMode(Animation.REVERSE);
        rockImage.startAnimation(alphaAnimation);
    }

  1. scale 比例缩放 ScaleAnimation(new ScaleAnimation(fromX, toX, fromY, toY, pivotXType, pivotXValue, pivotYType, pivotYValue);)
    1. // fromX// toX::开始(结束)缩放的X轴倍数。如1.0f:本身大小;如2.0f:从自己两倍开始
    2. // fromY//toY:始缩放的Y轴倍数。
  2. translate 位置移动(new TranslateAnimation(fromXType, fromXValue, toXType, toXValue, fromYType, fromYValue, toYType, toYValue))
    1. // pivotXType:X轴缩放中心点类型;可选值有:
    2. // pivotXValue:在pivotXType的基础上,X轴缩放中心的位置。如:0.5f:缩放中心就在控件的一半的位置。如果是0.0f,则会在控件本身的左边位置
      1. Animation.RELATIVE_TO_SELF相对自己--常用
      2. Animation.RELATIVE_TO_PARENT相对父窗体
      3. Animation.ABSOLUTE 绝对的---不常用
    3. // pivotYType:X轴缩放中心点类型;同上 ...
    4. // pivotYValue:在pivotYType的基础上,Y轴缩放中心的位置。
  3. rotate 旋转动画(new RotateAnimation(fromDegrees, toDegrees, pivotXType, pivotXValue, pivotYType, pivotYValue))
fromDegrees:开始旋转的角度;三点方向为0度,六点方向为90度。
toDegrees:结束旋转的角度
pivotXType:/pivotYType:参照缩放中心dian动画类型  
  Animation.RELATIVE_TO_SELF相对自己--常用
  Animation.RELATIVE_TO_PARENT相对父窗体
  Animation.ABSOLUTE 绝对的---不常用
pivotXValue/pivotYValue:参照缩放中心位置
如:0.5f:缩放中心就在控件的一半的位置。如果是0.0f,则会在控件本身的左边位置


publicvoid rotetecode(ViewcView) {
        //定义动画
        RotateAnimation rotateAnimation =new RotateAnimation(-15, 15, Animation.RELATIVE_TO_SELF, 0.5f,
                 Animation.RELATIVE_TO_SELF, -2f);
        rotateAnimation.setDuration(3000); //持续时间
        rotateAnimation.setRepeatCount(Animation.INFINITE); //执行次数
        rotateAnimation.setRepeatMode(Animation.REVERSE); //执行模式
        rockImage.setAnimation(rotateAnimation);
    }

  1. 综合应用:AnimationSet,set.addAnimation(a);

// 动画集合
AnimationSet set = new AnimationSet(true);
set.addAnimation(alpha);
set.addAnimation(rotate);
set.setFillAfter(true);

// 播放动画

flower.startAnimation(set);





补间动画侦听方法:

通过Animation.setAnimationListener的方法。传入AnimationListener的接口对象。实现接口的三个方法。

alphaAnimation.setAnimationListener(new AnimationListener() {
            @Override
            publicvoid onAnimationStart(Animationanimation) {
                // TODO Auto-generated method stub
                 Log.i("TAG","---------------onAnimationStart---");
             }
            @Override
            publicvoid onAnimationRepeat(Animationanimation) {
                // TODO Auto-generated method stub
                 Log.i("TAG","---------------onAnimationRepeat---");
             }
            @Override
            publicvoid onAnimationEnd(Animationanimation) {
                // TODO Auto-generated method stub
                 Log.i("TAG","---------------onAnimationEnd---");
             }
        });



例子:Activity跳转动画
enter_anim.xml

<?xmlversion="1.0"encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="3000">
 
   <alpha
       android:fromAlpha="1"
       android:toAlpha="0.3"/>
 
   <scale
       android:fromXScale="1"
       android:fromYScale="1"
       android:pivotX="50%"
       android:pivotY="50%"
       android:toXScale="0.2"
       android:toYScale="0.2"/>
 
</set>
 

exit_anim.xml

<?xmlversion="1.0"encoding="utf-8"?>
<setxmlns:android="http://schemas.android.com/apk/res/android"
   android:duration="3000">
 
   <alpha
       android:fromAlpha="0.2"
       android:toAlpha="1"/>
 
   <scale
       android:fromXScale="0.2"
       android:fromYScale="0.2"
       android:pivotX="50%"
       android:pivotY="50%"
       android:toXScale="1"
       android:toYScale="1"/>
 
</set>




跳转时启动动画

Intent intent =new Intent(this,ScondActivity.class);
        startActivity(intent);
       //设置动画 填写两个动画效果
        overridePendingTransition(R.anim.after, R.anim.befrom);






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值