如何在imageview上添加顺序动画,以便在android中重复放大和缩小

时间:2018-01-25 06:11:52

标签: android xml android-layout animation

我正在尝试在imageview上做动画,我是否需要按比例放大并缩小图像,以便它对另一个动画产生阴影感觉效果。

我尝试在anim文件夹中添加两个xml -

scale_up.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="false">
    <scale
        android:duration="1250"
        android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toXScale="1.5"
        android:toYScale="1.5" />

</set>

scale_down.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:shareInterpolator="false">
    <scale
        android:duration="1250"
        android:fromXScale="1.5"
        android:fromYScale="1.5"
        android:pivotX="50%"
        android:pivotY="50%"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toXScale="1.0"
        android:toYScale="1.0" />

</set>

和我的活动课

AnimationSet s = new AnimationSet(false);
View shadowView = view.findViewById(R.id.shadowView);
final Animation scaleDownAnimation = AnimationUtils.loadAnimation(context,   R.anim.scale_down);
final Animation scaleUpAnimation = AnimationUtils.loadAnimation(context,   R.anim.scale_up);
s.addAnimation(scaleUpAnimation);
s.addAnimation(scaleDownAnimation);
shadowView.startAnimation(s);

但动画并没有发生。我做错了什么?有人请帮忙。感谢

1 个答案:

答案 0 :(得分:0)

AnimationSet默认执行动画同时,而不是 sequentaly 。因此它可以同时放大和缩小。这就是你没有看到效果的原因。但您可以为动画设置起始偏移量:

AnimationSet s = new AnimationSet(false);
View shadowView = view.findViewById(R.id.shadowView);
final Animation scaleDownAnimation = AnimationUtils.loadAnimation(context,   R.anim.scale_down);
final Animation scaleUpAnimation = AnimationUtils.loadAnimation(context,   R.anim.scale_up);
scaleDownAnimation.setStartOffset(1250); //this line
s.addAnimation(scaleUpAnimation);
s.addAnimation(scaleDownAnimation);
shadowView.startAnimation(s