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

Geo*_*kos 30 android android-animation android-imageview

我想知道如何使用九个olad架构动画创建一个脉冲效果.

为了更好地理解你可以说你有一个ImageView,并希望有一个"脉冲"效果,如使图像小一点,然后回到原始大小,缩放将居中.

我使用九个olad机器人向后兼容.

欢迎任何其他选择.

谢谢.

Mat*_*ers 103

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" />
Run Code Online (Sandbox Code Playgroud)
ImageView imageView = (ImageView) findViewById(R.id.image);
Animation pulse = AnimationUtils.loadAnimation(this, R.anim.pulse);
imageView.startAnimation(pulse);
Run Code Online (Sandbox Code Playgroud)

  • 我不这么认为,但你可以通过使用我昨天发布的第一个版本的XML来实现这一点:http://stackoverflow.com/revisions/14217749/1增加第二个动画的`startOffset`. (2认同)

小智 13

heart_pulse.xml将heart_pulse.xml放在res/anim文件夹中添加android:interpolator

然后在你的活动中使用,如下所示

<?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);
Run Code Online (Sandbox Code Playgroud)

  • 答案:“ imageView.clearAnimation();” (2认同)

Far*_*uti 5

要直接从 XML 使用 @Matthias Robbers 解决方案,您可以执行以下操作:创建 2 个文件:

1-脉冲.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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

然后在您的布局 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" />
Run Code Online (Sandbox Code Playgroud)