Site icon Howtodoandroid

Android RecyclerView Item Animations in Kotlin [Example]

Android RecyclerView Item Animations in Kotlin [Example]

We are using recyclerview, for almost all of our android applications. It’s very useful to know about recyclerviews. in this post, I have explained all about animating recyclerview using the simple anim XML file.

Steps to animate recyclerview items in android

  1. Create animation XML files.
  2. Apply the animation to the layout container.

Create an animation XML file

To create an animation XML layout, Create an anim directory in your res folder.

Anim directory

Then, create your animation XML file under the anim directory.

animation XML files

Apply the animation to the layout container

Once the animation XML file is created, we need to set the animation file into the layout root view. In our case, I have created adapter_movie.xml as an adapter layout for the recyclerview. And the root container is MatericalCardView and the id is “container“.

To add the animation to the layout we need to add,

holder.container.animation = AnimationUtils.loadAnimation(holder.itemView.context, R.anim.scale)

We have a lot of animation available on android. below we will see some of the popular animations on android recyclerview.

Different animations on android

Alpha

An animation that controls the alpha level of an object. Useful for fading things in and out. This animation ends up changing the alpha property of a Transformation.

Parameters

fromAlphafloat: Starting alpha value for the animation, where 1.0 means fully opaque and 0.0 means fully transparent.
toAlphafloat: Ending alpha value for the animation.

alpha.xml

<?xml version="1.0" encoding="utf-8"?> <alpha xmlns:android="http://schemas.android.com/apk/res/android" android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="500" > </alpha>
Code language: HTML, XML (xml)

Output

alpha animation

Rotate

An animation that controls the rotation of an object. This rotation takes place in the X-Y plane. You can specify the point to use for the center of the rotation, where (0,0) is the top-left point. If not specified, (0,0) is the default rotation point.

Parameters

fromDegreesfloat: Rotation offset to apply at the start of the animation.
toDegreesfloat: Rotation offset to apply at the end of the animation.

rotate.xml

<?xml version="1.0" encoding="utf-8"?> <rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="180" android:toDegrees="0" android:pivotX="100%" android:pivotY="100%" android:duration="250"> </rotate>
Code language: HTML, XML (xml)

output

rotate animation

Scale

An animation that controls the scale of an object. You can specify the point to use for the center of scaling. Scale Animation is basically increasing or decreasing the size of the View.

Parameters

fromXfloat: Horizontal scaling factor to apply at the start of the animation
toXfloat: Horizontal scaling factor to apply at the end of the animation
fromYfloat: Vertical scaling factor to apply at the start of the animation
toYfloat: Vertical scaling factor to apply at the end of the animation
pivotXfloat: The X coordinate of the point about which the object is being scaled, specified as an absolute number where 0 is the left edge. (This point remains fixed while the object changes size.)
pivotYfloat: The Y coordinate of the point about which the object is being scaled, is specified as an absolute number where 0 is the top edge. (This point remains fixed while the object changes size.)
scale animation parameters

scale.xml

<?xml version="1.0" encoding="utf-8"?> <scale xmlns:android="http://schemas.android.com/apk/res/android" android:duration="500" android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%"> </scale>
Code language: HTML, XML (xml)

output

scale animation

Translate

An animation that controls the position of an object. 

Parameters

fromXDeltafloat: Change in X coordinate to apply at the start of the animation
toXDeltafloat: Change in X coordinate to apply at the end of the animation
fromYDeltafloat: Change in Y coordinate to apply at the start of the animation
toYDeltafloat: Change in Y coordinate to apply at the end of the animation

translate.xml

<?xml version="1.0" encoding="utf-8"?> <translate android:fromYDelta="100%" android:fromXDelta="100%" android:toYDelta="0%" android:toXDelta="0%" android:duration="500" xmlns:android="http://schemas.android.com/apk/res/android" />
Code language: HTML, XML (xml)

Output

translate animation

Interpolators

We have the last attribute which is interpolators. Interpolators are basically the behavior of the animations. Normally animations work on Linear Interpolator. That interpolator moves the view evenly over each frame of the animation.

<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_interpolator" android:duration="500"> <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%" > </scale> </set>
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/decelerate_interpolator" android:duration="500"> <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%" > </scale> </set>
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:duration="500"> <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%" > </scale> </set>
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/anticipate_interpolator" android:duration="500"> <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%" > </scale> </set>
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/bounce_interpolator" android:duration="500"> <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%" > </scale> </set>
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/overshoot_interpolator" android:duration="500"> <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%" > </scale> </set>
Code language: HTML, XML (xml)
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/anticipate_overshoot_interpolator" android:duration="500"> <scale android:fromXScale="0" android:fromYScale="0" android:toXScale="1" android:toYScale="1" android:pivotY="50%" android:pivotX="50%" > </scale> </set>
Code language: HTML, XML (xml)

That’s it. thank you for the reading. You can download this example on GitHub.

Exit mobile version