Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create ImageView pulse effect using nine old androids animation

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.

like image 205
George Taskos Avatar asked Jan 08 '13 14:01

George Taskos


3 Answers

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);
like image 191
Matthias Robbers Avatar answered Nov 05 '22 19:11

Matthias Robbers


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);
like image 36
Chirag Darji Avatar answered Nov 05 '22 18:11

Chirag Darji


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" />
like image 5
Fareed Alnamrouti Avatar answered Nov 05 '22 17:11

Fareed Alnamrouti