How to create ImageView pulse effect using nine ol

2019-03-08 05:46发布

问题:

I wonder how I should create a pulse effect using nine olad androids framework animation.

To understand better lets say you have an ImageView and want to have a "pulse" effect like make the image little smaller and then back to original size, scaling will be centered.

I use nine olad androids for backward compatibility.

Any other option is welcome.

Thank you.

回答1:

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);


回答2:

heart_pulse.xml put heart_pulse.xml in res/anim folder Add android:interpolator

then use in your activity like below

<?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);


回答3:

to use @ Matthias Robbers solution directly from the XML, you can do the following: create 2 files:

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>

then in your layout xml file just add this animation to any view you need, for example:

<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" />