Android 动画总结-补间动画

AlphaAnimation:透明度(alpha)渐变效果,对应< alpha/>”标签。

TranslateAnimation:位移渐变,需要指定移动点的开始和结束坐标,对应<translate/>标签。

ScaleAnimation:缩放渐变,可以指定缩放的参考点,对应<scale/>标签。

RotateAnimation:旋转渐变,可以指定旋转的参考点,对应<rotate/>标签。

AnimationSet:组合渐变,支持组合多种渐变效果,对应<set/>标签。

这里写图片描述

在xml中定义

放在res\anim

如:
anim_alpha.xml

<?xml version="1.0" encoding="utf-8"?>
<alpha xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    android:fromAlpha="1.0"
    android:repeatCount="3"
    android:repeatMode="reverse"
    android:toAlpha="0.2" /><!--
            fromAlpha 起始时透明度
            toAlpha    结束时透明度
                0.0表示完全透明
                1.0表示完全不透明
    -->

anim_translation.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="2000"
    android:fillAfter="true"
    android:fromXDelta="0"
    android:fromYDelta="0"
    android:toXDelta="200"
    android:toYDelta="200" />

    <!--
            fromXDelta 属性为动画起始时 X坐标上的位置
            toXDelta   属性为动画结束时 X坐标上的位置
            fromYDelta 属性为动画起始时 Y坐标上的位置
            toYDelta   属性为动画结束时 Y坐标上的位置
            注意:
                     没有指定fromXType toXType fromYType toYType 时候,
                     默认是以自己为相对参照物
    -->

anim_rotate.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="3000"
    android:fillAfter="true"
    android:fromDegrees="0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="+720" />
    <!--
      属性:interpolator 指定一个动画的插入器
            accelerate_decelerate_interpolator  加速-减速
            accelerate_interpolator        加速
            decelerate_interpolator        减速

            fromDegrees 属性为动画起始时物件的角度
            toDegrees   属性为动画结束时物件旋转的角度 可以大于360度
            说明:
                     当角度为负数——表示逆时针旋转
                     当角度为正数——表示顺时针旋转
                     (负数from——to正数:顺时针旋转)
                     (负数from——to负数:逆时针旋转)
                     (正数from——to正数:顺时针旋转)
                     (正数from——to负数:逆时针旋转)

            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置

            说明:        以上两个属性值 从0%-100%中取值
                         50%为物件的X或Y方向坐标上的中点位置
    -->

anim_scale.xml

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fillAfter="true"
    android:fromXScale="0.0"
    android:fromYScale="0.0"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="2"
    android:toYScale="2" />
    <!--
       属性:interpolator 指定一个动画的插入器
            accelerate_decelerate_interpolator  加速-减速
            accelerate_interpolator        加速
            decelerate_interpolator        减速
            fromXScale 属性为动画起始时 X坐标上的伸缩尺寸
            toXScale   属性为动画结束时 X坐标上的伸缩尺寸

            fromYScale 属性为动画起始时Y坐标上的伸缩尺寸
            toYScale   属性为动画结束时Y坐标上的伸缩尺寸

            说明:
                 以上四种属性值

                    0.0表示收缩到没有
                    1.0表示正常无伸缩
                    值小于1.0表示收缩
                    值大于1.0表示放大

            pivotX     属性为动画相对于物件的X坐标的开始位置
            pivotY     属性为动画相对于物件的Y坐标的开始位置

            说明:
                    以上两个属性值 从0%-100%中取值
                    50%为物件的X或Y方向坐标上的中点位置

        布尔型值:
            fillAfter 属性 当设置为true ,该动画转化在动画结束后被应用

-->

anim_set.xml

<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:shareInterpolator="true">

    <scale
        android:duration="2000"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toXScale="1.5"
        android:toYScale="1.5" />

    <rotate
        android:duration="2000"
        android:fromDegrees="0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toDegrees="360" />

    <translate
        android:duration="2000"
        android:fromXDelta="0"
        android:fromYDelta="0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toXDelta="300"
        android:toYDelta="0" />

    <alpha
        android:duration="2000"
        android:fromAlpha="1.0"
        android:repeatCount="1"
        android:repeatMode="reverse"
        android:toAlpha="0.1" />
</set>

作用于控件

Animation animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
mImage.startAnimation(animation);

TweenAnimXMLActivity

public class TweenAnimXMLActivity extends AppCompatActivity {
    private ImageView mImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_anim_xml);
        mImage = (ImageView) findViewById(R.id.image);
    }

    public void onClick(View view) {
        Animation animation = null;
        switch (view.getId()) {
            case R.id.btn_alpha:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
                break;
            case R.id.btn_translation:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_translation);
                break;
            case R.id.btn_rotate:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
                break;
            case R.id.btn_scale:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_scale);
                break;
            case R.id.btn_set:
                animation = AnimationUtils.loadAnimation(this, R.anim.anim_set);
                break;
            default:
                break;
        }
        mImage.startAnimation(animation);
    }
}

直接在Java代码中定义

这里写图片描述

TweenAnimCodeActivity

public class TweenAnimCodeActivity extends AppCompatActivity {
    private static final String TAG = "TweenAnimCodeActivity";
    private ImageView mImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tween_anim_code);
        mImage = (ImageView) findViewById(R.id.image);
    }

    public void onClick(View view) {
        Animation animation;
        switch (view.getId()) {
            case R.id.btn_alpha:
                animation = new AlphaAnimation(1.0f, 0.2f);
                break;
            case R.id.btn_translation:
//                animation = new TranslateAnimation(0, 200, 0, 200);
//                参考坐标系
//                Animation.ABSOLUTE  默认
//                Animation.RELATIVE_TO_SELF  相对自己  左上角 (0, 0)
//                Animation.RELATIVE_TO_PARENT  相对父View
                animation = new TranslateAnimation(Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 200,
                        Animation.ABSOLUTE, 0, Animation.ABSOLUTE, 200);
                break;
            case R.id.btn_rotate:
//                animation = new RotateAnimation(0, 720, 0.5f, 0.5f);
                // pivotX、pivotY  缩放, 旋转中心
                animation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_SELF, 0.5f, Animation
                        .RELATIVE_TO_SELF, 0.5f);
//                        设置插值器
//                        LinearInterpolator(匀速)
//                        AccelerateInterpolator(先慢后快)
//                        AccelerateDecelerateInterpolator(先慢中快后慢)
//                        DecelerateInterpolator(先快后慢)
//                        CycleInterpolator(循环播放,速度为正弦曲线)
//                        AnticipateInterpolator(先回撤,再匀速向前)
//                        OvershootInterpolator(超过,拉回)
//                        BounceInterpolator(回弹)
                animation.setInterpolator(new AccelerateDecelerateInterpolator());
                // 相对父View
                // 父View width(100dp) height(200dp)
                //旋转中心位于 相对控件(0, 0) X50, Y100的地方
//                animation = new RotateAnimation(0, 720, Animation.RELATIVE_TO_PARENT, 0.5f,
//                        Animation.RELATIVE_TO_PARENT, 0.5f);
                break;
            case R.id.btn_scale:
//                animation = new ScaleAnimation(0.2f, 2, 0.2f, 2, 0.5f, 0.5f);
                animation = new ScaleAnimation(0.2f, 2, 0.2f, 2, Animation.RELATIVE_TO_SELF, 0.5f, Animation
                        .RELATIVE_TO_SELF, 0.5f);
                break;
            case R.id.btn_set:
                ScaleAnimation scaleAnimation = new ScaleAnimation(1, 2, 1, 2, Animation
                        .RELATIVE_TO_SELF, 0.5f, Animation
                        .RELATIVE_TO_SELF, 0.5f);
                scaleAnimation.setDuration(1000);
                scaleAnimation.setRepeatCount(Animation.INFINITE);
                scaleAnimation.setRepeatMode(Animation.REVERSE);
                AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.2f);
                alphaAnimation.setDuration(1000);
                alphaAnimation.setRepeatCount(Animation.INFINITE);
                alphaAnimation.setRepeatMode(Animation.REVERSE);
                //是否公用插值器
                AnimationSet animationSet = new AnimationSet(false);
                animationSet.addAnimation(scaleAnimation);
                animationSet.addAnimation(alphaAnimation);
                //设置动画延时时间(多久后开始)
//                animationSet.setStartOffset(2000);
                //设置动画结束之后是否保持动画的目标状态
//                animationSet.setFillAfter(true);
                //设置动画结束之后是否保持动画开始时的状态
//                animationSet.setFillBefore(false);
                mImage.startAnimation(animationSet);
                //取消动画
//                animationSet.cancel();
                //释放资源
//                animationSet.reset();
                return;
            default:
                return;
        }
        animation.setDuration(1000);//间隔
        animation.setFillAfter(true);//结束后应用
        animation.setRepeatCount(3);//重复次数
        animation.setRepeatMode(Animation.REVERSE);//重复模式
        animation.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {
                Log.d(TAG, "onAnimationStart() called with: animation = [" + animation + "]");
            }

            @Override
            public void onAnimationEnd(Animation animation) {
                Log.d(TAG, "onAnimationEnd() called with: animation = [" + animation + "]");
            }

            @Override
            public void onAnimationRepeat(Animation animation) {
                Log.d(TAG, "onAnimationRepeat() called with: animation = [" + animation + "]");
            }
        });
        mImage.startAnimation(animation);
    }
}

源码参见: http://download.csdn.net/detail/maimiho/9660930
Android 动画总结-Activity切换动画 http://write.blog.csdn.net/mdeditor
Android 动画总结-Layout动画 http://blog.csdn.net/maimiho/article/details/52888887
Android 动画总结-帧动画 http://blog.csdn.net/maimiho/article/details/52893291
Android 动画总结-补间动画 http://blog.csdn.net/maimiho/article/details/52893403
Android 动画总结-属性动画 http://blog.csdn.net/maimiho/article/details/52894023
Android 动画总结-ViewPropertyAnimator http://blog.csdn.net/maimiho/article/details/52894151
Android 动画总结-矢量动画 http://blog.csdn.net/maimiho/article/details/52894266

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值