Android 补间动画解析(一)

最近想给App加一点特效,无奈对动画的一些属性不熟悉,基本是一路百度下来的QAQ,好吧。这里做一下总结,省得下次又一路百度。。如果有什么遗漏的地方,请看到的同学说一下~!

Tweened Animations的分类 ### 

1. Alpha:淡入淡出动画
2. Scale:缩放动画
3. Rotate:旋转动画
4. Translate:移动效果
5. AnimationSet:用于播放一组动画

1.Alpha (淡入淡出)

   <!--淡入淡出动画-->
    <alpha
        android:duration="3000"
        android:fromAlpha="0"
        android:toAlpha="1" />

    <!--  duration:动画显示到结束的时间
          fromAlpha:动画开始时的透明度
          toAlpha:动画结束时的透明度-->

java代码实现上面的xml AlphaAnimation

 public static void showView(View v) {
        AlphaAnimation alphaAnimation = new AlphaAnimation(0, 1);//(开始透明度,结束透明度)
        alphaAnimation.setDuration(3000);//执行时间
        v.startAnimation(alphaAnimation);
    } 

2.Scale (缩放)

   <?xml version="1.0" encoding="utf-8"?>
    <set xmlns:android="http://schemas.android.com/apk/res/android"
    android:fillAfter="true"> <!--fillAfter 当这个属性为true是,动画结束后会保存当前状态-->
    <!--旋转动画-->
    <scale
        android:duration="3000"
        android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="90%"
        android:pivotY="50%"
        android:toXScale="2.0"
        android:toYScale="2.0" />
    <!--
     duration:动画执行时间
     fromXScale:动画开始时的x的大小,上面相当于View宽的 0%
     fromYScale: 动画开始时的y的大小,上面相当于View长的 0%
     pivotX:动画从那里开始显示 上面 90%,相当于从当前View X轴总长度的90%开始播放
     pivotY:相当于从当前View y轴总长度的50%开始播放 (加起来就是从 View*90%,View*50%处显示缩放动画)
     toXScale:结束后ViewX轴的长度 View * 2
     toYScale:结束后ViewY轴的长度 View * 2
    -->
</set>

java代码实现上面的xml ScaleAnimation

     public static void scaleAnim(View v) {
         //构造方法
        // ScaleAnimation(float fromX, float toX, float fromY, float toY,int pivotXType, float pivotXValue, int pivotYType, float pivotYValue) 
        //Animation.RELATIVE_TO_SELF 这个为相对于自己旋转,而不是选取屏幕当中的坐标旋转
         ScaleAnimation scale = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f, Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
        scale.setFillAfter(true);//设置动画动画结束后保存当前状态
        scale.setDuration(3000); //时间
        v.startAnimation(scale);
    }

3.Rotate(旋转)

    <set xmlns:android="http://schemas.android.com/apk/res/android">
    <!--旋转动画-->
    <rotate
        android:duration="3000"
        android:fillAfter="true"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatMode="restart"
        android:repeatCount="infinite"
        android:toDegrees="360" />
    <!--
    duration:执行时间
    fillAfter:保存状态
    fromDegrees:动画开始的角度
    interpolator:动画插入器,详情请百度
    pivotX:旋转的X轴中心
    pivotY:旋转的Y轴中心
    repeatMode:重新开始的时候反过来转
    repeatCount:设置无限循环 也可以设置执行的次数
    toDegrees:选择到多少度后停止
    -->
</set>

java代码实现上面的xml RotateAnimation

    public static void RotateAnim(View v){
        RotateAnimation rotateAnimation = new RotateAnimation(0,360,Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF,0.5f);
        rotateAnimation.setDuration(3000);//执行事件
        rotateAnimation.setInterpolator(new LinearInterpolator());
        rotateAnimation.setRepeatCount(Animation.INFINITE);//无限循环
        v.startAnimation(rotateAnimation);
    }

4.Translate (平移)

     <translate
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:toXDelta="100"
        android:toYDelta="100"
        android:duration="3000" />

    <!--fromXDelta :开始的X轴的位置
       fromYDelta :开始的Y轴的位置
       toXDelta :结束的X轴的位置
       toYDelta :结束的Y轴的位置
    -->

java代码实现上面的xml TranslateAnimation

       public static void translateAnim(View v) {
        //同上
        TranslateAnimation translateAnimation = new TranslateAnimation(0, 100, 0, 100);
        translateAnimation.setDuration(3000);
        translateAnimation.setFillAfter(true);
        v.startAnimation(translateAnimation);
    }

4.AnimationSet(动画集合)

其实顺序执行一段动画,就是设定动画开始的时间
startOffset这个属性

    <?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">

    <translate
        android:duration="2000"
        android:fromYDelta="0"
        android:toYDelta="-100%"></translate>
    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/linear_interpolator"
        android:pivotX="50%"
        android:pivotY="-50%"
        android:repeatCount="100"
        android:startOffset="2000"
        android:toDegrees="360" />

    <scale
        android:duration="3000"
        android:fillAfter="true"
        android:fromXScale="1"
        android:fromYScale="1"
        android:startOffset="4000"
        android:toXScale="0.2"
        android:toYScale="0.2" />

</set>

java代码实现上面的xml AnimationSet

     public static void SetAnim(View v){
        AnimationSet animationSet = new AnimationSet(true);
        int self = Animation.RELATIVE_TO_SELF; //相对自身
        TranslateAnimation translateAnimation = new TranslateAnimation(self,0,self,0,self,0,self,-1);
        translateAnimation.setDuration(2000); //执行2秒
        animationSet.addAnimation(translateAnimation);//添加到集合

        RotateAnimation rotateAnimation = new RotateAnimation(0,360,self,0.5f,self,-0.5f);
        rotateAnimation.setRepeatCount(100); //执行100次
        rotateAnimation.setStartOffset(2000); //延迟2秒后执行
        rotateAnimation.setInterpolator(new LinearInterpolator());//均速旋转
        rotateAnimation.setDuration(1000);//转一圈需要1秒
        rotateAnimation.setFillAfter(true);//保存当初状态
        animationSet.addAnimation(rotateAnimation); //添加

        ScaleAnimation scaleAnimation = new ScaleAnimation(1,1.5f,1,1.5f);
        scaleAnimation.setDuration(2000);
        scaleAnimation.setStartOffset(5000);//延迟5秒执行
        scaleAnimation.setFillAfter(true);//保存当初状态
        animationSet.addAnimation(scaleAnimation);

        v.startAnimation(animationSet);

    }

代码下载地址

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值