安卓动画之Tween动画

来源:互联网 发布:Linux进入根目录的命令 编辑:程序博客网 时间:2024/06/09 23:06
一 动画框架概述:
       在安卓的平台中有一套完整的动画框架,在安卓3.0之前,也就是API11以前,安卓一共有两种动画,就是补间动画(TweenAnimation)和逐帧动画(Fragme Aniamtion,Drawable Animation),在安卓3.0之后为增强与动画的交互出现了  属性动画(Property Animation).
       因为Tween Animation 和 Frame Animation 只能作用于View,所以它们也成为ViewAnimation
二 补间动画(Tween Animation):
         补间的动画的作用对象是View,它支持4种动画效果,分别是平移动画,缩放动画,旋转动画,透明度动画。
         补间动画的四种变换效果对应着Animation的4个子类:TranslateAnimation,ScaleAnimation,AlphaAnimation,RotateAnimation,AlphaAnimation,这4中动画既可以动过xm来定义,也可以通过代码来设置,xml的可读性稍微好一点:
         先介绍一下代码方式:
    

Alpha:

         /**

            fromAlpha:开始时刻的透明度,取值范围0~1

            toAlpha:结束时刻的透明度,取值范围0~1

              AlphaAnimation animation = new AlphaAnimation(0.0f, 1.0f);

            animation.setDuration(3000);
            animation.setFillAfter(true);//控件保持最终的位置
            iv_share.startAnimation(animation);
Trantlate:
       /**
        float fromXDelta 动画开始的点离当前View X坐标上的差值
        float toXDelta 动画结束的点离当前View X坐标上的差值
        float fromYDelta 动画开始的点离当前View Y坐标上的差值
        float toYDelta 动画开始的点离当前View Y坐标上的差值

                TranslateAnimation animation = new TranslateAnimation(0,100, 0,200);

                animation.setDuration(3000);

                animation.setFillAfter(true);

                iv_share.startAnimation(animation);

ScaleAnimation:

/**

          第一个参数fromX为动画起始时 X坐标上的伸缩尺寸  0.0表示收缩到没有

        第二个参数toX为动画结束时 X坐标上的伸缩尺寸   1.0表示正常无伸缩

        第三个参数fromY为动画起始时Y坐标上的伸缩尺寸  值小于1.0表示收缩

        第四个参数toY为动画结束时Y坐标上的伸缩尺寸   值大于1.0表示放大

                 第五个参数pivotXType为动画在X轴相对于物件位置类型

        第六个参数pivotXValue为动画相对于物件的X坐标的开始位置

        第七个参数pivotXType为动画在Y轴相对于物件位置类型

          第八个参数pivotYValue为动画相对于物件的Y坐标的开始位置

                ScaleAnimation animation = new ScaleAnimation(0.0f,1.5f,                   0.0f,1.5f,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);

                 animation.setDuration(3000);

                 animation.setFillAfter(true);

                 iv_share.startAnimation(animation);

Rotate(旋转效果):

/**

        第一个参数fromDegrees为动画起始时的旋转角度    此角度是当前为0360,设置其他值则先跳至该角度的位置再    由from - to的值负则正向转,正则反向转

        第二个参数toDegrees为动画旋转到的角度

        第三个参数pivotXType为动画在X轴相对于物件位置类型

              第四个参数pivotXValue为动画相对于物件的X坐标的开始位置 此值是以本身原始位置为原点,即如设为20%p,则向右移动父控件的20%位移,为负数则向左移

        第五个参数pivotXType为动画在Y轴相对于物件位置类型

        第六个参数pivotYValue为动画相对于物件的Y坐标的开始位置 此值是以本身原始位置为原点,即如设为20%p,则向下移动父控件的20%位移,为负数则向上移

                RotateAnimation animation = new RotateAnimation(0f, 300f, Animation.RELATIVE_TO_SELF, 0.5f,                    Animation.RELATIVE_TO_SELF, 0.5f);

                animation.setDuration(3000);

                animation.setFillAfter(true);

                iv_share.startAnimation(animation);

动画集合使用方式:

  

//设置为false每个子动画都用自己的插值器
AnimationSet animationSet = new AnimationSet(false);
animationSet.addAnimation(animation);
animationSet.setDuration(1000);
animationSet.start();;

          代码方式介绍完了,接着介绍一下XML方式:

         

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="300"
android:interpolator="@android:anim/accelerate_interpolator"
子动画支付共享插值器
android:shareInterpolator="true"
动画结束以后是否停留在原来的位置
android:fillAfter="true"


/>
<alpha
android:fromAlpha="0.0"
android:toAlpha="1.0"
android:duration="2000"
/>
<translate
android:fromXDelta="0"
android:toXDelta="500"
android:duration="2000"
/>
<scale
android:fromXScale="0.0"
android:toYScale="1.0"
android:fromYScale="0.0"
android:toXScale="1.0"
<!---缩放的节点-->
android:pivotX="9"
android:pivotY="0"
/>
<rotate
android:fromDegrees="0"
android:toDegrees="180"
android:pivotY="2"
android:pivotX="2"/>

</set>

    如何应用xml动画呢?

       Animation animation = AnimationUtils.loadAnimation(LayoutActivity.this,R.anim.anim_item);

       view.startAnimation(animation);

Tween动画的监听器:   

    

TranslateAnimation  translateAnimation = new TranslateAnimation(0,90,0,90);

translateAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
//开始执行动画时
}

@Override
public void onAnimationEnd(Animation animation) {
//停止执行动画时
}

@Override
public void onAnimationRepeat(Animation animation) {
//重复执行动画时
}
});

1 0
原创粉丝点击