如何使用九个老机器人动画创建ImageView脉冲效果



我想知道如何使用九个旧的android框架动画创建脉冲效果。

为了更好地理解,让我们假设你有一个ImageView,想要有一个"脉冲"效果,比如将图像缩小一点,然后恢复到原始大小,缩放将居中。

为了向后兼容,我使用了9个旧的机器人。

欢迎使用其他选项

谢谢。

R.anim.pulse:

<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="1000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:repeatCount="1"
    android:repeatMode="reverse"
    android:toXScale="0.5"
    android:toYScale="0.5" />
ImageView imageView = (ImageView) findViewById(R.id.image);
Animation pulse = AnimationUtils.loadAnimation(this, R.anim.pulse);
imageView.startAnimation(pulse);

heart_pulse.xml把heart_pulse.xml放到res/animm文件夹中添加android:插入器

然后在你的活动中使用,就像下面

<?xml version="1.0" encoding="utf-8"?>
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="0.5"
    android:toYScale="0.5"
    android:duration="1000"
    android:repeatCount="infinite"
    android:repeatMode="reverse"/>
ImageView imageView =(ImageView)findViewById(R.id.imageView);
Animation pulse = AnimationUtils.loadAnimation(this, R.anim.heart_pulse);
imageView.startAnimation(pulse);

直接从XML中使用@ Matthias Robbers的解决方案,您可以这样做:创建2个文件:

1 - pulse.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale xmlns:android="http://schemas.android.com/apk/res/android"
        android:fromXScale="1"
        android:fromYScale="1"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.8"
        android:toYScale="0.8"
        android:duration="500"
        android:repeatCount="infinite"
        android:repeatMode="reverse"/>
</set>

2 - pulse_layout_animation.xml

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/pulse">
</layoutAnimation>

然后在你的布局XML文件中添加这个动画到任何你需要的视图,例如:

<ImageView
    android:layout_width="55dp"
    android:layout_height="55dp"
    android:src="@drawable/heart"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:layoutAnimation="@anim/pulse_layout_animation" />

最新更新