Android 动画使用详解(二) 补间动画

动画在Android 开发中经常会被用到,好的动画效果可以达到事半功倍的效果。补间动画也是Android中常用的动画之一,相对属性动画来说,补间动画的点击事件不会跟着动画的位置变化而变化。后续将逐渐被属性动画替代。

  1. 透明动画 AlphaAnimation
  2. 旋转动画 ScaleAnimation
  3. 缩放动画 RotateAnimation
  4. 平移动画 TranslateAnimation
  5. 动画集合 AnimationSet
  6. XML 实现4种动画效果

欢迎关注微信公众号:程序员Android
公众号ID:ProgramAndroid
获取更多信息

微信公众号:ProgramAndroid

微信公众号:ProgramAndroid

我们不是牛逼的程序员,我们只是程序开发中的垫脚石。
我们不发送红包,我们只是红包的搬运工。

1. 透明动画

  • AlphaAnimation
 /**
         * 透明度动画AlphaAnimation 从不透明到完全透明
         * **/
        // 起始透明度 到结束透明度 不透明到透明(1f-0f)
        AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
        // 动画执行时间
        alphaAnimation.setDuration(4000);
        // 设置重复次数
        alphaAnimation.setRepeatCount(2);
        // 重复模式
        alphaAnimation.setRepeatMode(Animation.RESTART);
        // alphaAnimation.setRepeatMode(Animation.REVERSE);
        // 保持结束时候的状态
        alphaAnimation.setFillAfter(true);
        mImageView.startAnimation(alphaAnimation);

2. 旋转动画

  • RotateAnimation
   /**
         * 旋转动画RotateAnimation,旋转360度
         **/

        RotateAnimation rotateAnimation = new RotateAnimation(0, 360);
        rotateAnimation.setDuration(2000);
        rotateAnimation.setRepeatCount(2);
        mImageView.startAnimation(rotateAnimation);

3. 缩放动画

  • ScaleAnimation
        /**
         * 缩放动画 ScaleAnimation使用方法 缩放2倍
         * */
        ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2);
        scaleAnimation.setDuration(2000);
        scaleAnimation.setRepeatCount(2);
        scaleAnimation.setRepeatMode(Animation.RESTART);
        mImageView.startAnimation(scaleAnimation);

4. 平移动画

  • TranslateAnimation
    /***
         * 平移动画TranslateAnimation 从x,y 轴 从(0,0)平移到(300,200) *
         **/
        TranslateAnimation translateAnimation = new TranslateAnimation(0,
                300.f, 0, 200.f);
        translateAnimation.setDuration(2000);
        translateAnimation.setRepeatCount(2);
        translateAnimation.setRepeatMode(Animation.RESTART);
        mImageView.startAnimation(translateAnimation);

5. 动画集合

  • AnimationSet
   /***
         * 动画集合使用效果如下:
         * ***/
        AnimationSet sets = new AnimationSet(true);

        AlphaAnimation alphaAnimation1 = new AlphaAnimation(0.0f, 1.0f);
        TranslateAnimation translateAnimation1 = new TranslateAnimation(0,
                100.f, 0, 100.f);
        RotateAnimation rotateAnimation1 = new RotateAnimation(0, 360);
        ScaleAnimation scaleAnimation1 = new ScaleAnimation(1, 2, 1, 2);

        // 将动画添加到set集合中
        sets.addAnimation(alphaAnimation1);
        sets.addAnimation(translateAnimation1);
        sets.addAnimation(rotateAnimation1);
        sets.addAnimation(scaleAnimation1);

        sets.setDuration(4000);
        sets.setRepeatCount(2);
        sets.setRepeatMode(Animation.RESTART);
        mImageView.startAnimation(sets);

6. XML 实现动画效果

  • 1.动画的XML文件所属的res/anim/hyperspace_jump.xml
<set android:shareInterpolator="false">
    <scale
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromXScale="1.0"
        android:toXScale="1.4"
        android:fromYScale="1.0"
        android:toYScale="0.6"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="false"
        android:duration="700" />
    <set android:interpolator="@android:anim/decelerate_interpolator">
        <scale
           android:fromXScale="1.4"
           android:toXScale="0.0"
           android:fromYScale="0.6"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400"
           android:fillBefore="false" />
        <rotate
           android:fromDegrees="0"
           android:toDegrees="-45"
           android:toYScale="0.0"
           android:pivotX="50%"
           android:pivotY="50%"
           android:startOffset="700"
           android:duration="400" />
    </set>
</set>
  • 2.为图片Load 动画xml文件
   ImageView spaceshipImage = (ImageView) findViewById(R.id.spaceshipImage);
   Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
   spaceshipImage.startAnimation(hyperspaceJumpAnimation);

至此,本篇已结束,如有不对的地方,欢迎您的建议与指正。同时期待您的关注,感谢您的阅读,谢谢!

如有侵权,请联系小编,小编对此深感抱歉,届时小编会删除文章,立即停止侵权行为,请您多多包涵。

既然都看到这里,领两个红包在走吧!
以下两个红包每天都可以领取

1.支付宝搜索 522398497,或扫码支付宝红包海报。

支付宝扫一扫,每天领取大红包

2.微信红包,微信扫一扫即可领取红包

 

微信扫一扫,每天领取微信红包

小礼物走一走,来简书关注我

posted @ 2017-10-16 11:06  程序员Android的博客  阅读(90)  评论(0编辑  收藏  举报