How to setup animation in android

Date : 20/06/2019

Introduction

Animation in android application are used to remove raw motions in android. However without an animation there can’t be good user experience so, we used. As a simple you can write an animation in XML file and use in your layout XML file.

An animation resource can define one of two types of animations:

Property Animation

Then creates an animation by modifying an object’s property values over a set period of time with an Animator.

View Animation
There is two types in this framework.

Tween animation : Creates animation by performing series of transformation in a single image with animation.

Frame animation : Creates animation by showing sequence of images with animationdrawable.

Here i will only explain briefly about view animator. Then required elements in view animator are explain a little.

Elements

<set>

Container that holds animation elements. You can specify <set> inside a <set> as nested.

<alpha>

A fade-in or fade-out animation. 

android:fromAlpha. Starting opacity offset, where 0.0 is transparent and 1.0 is opaque

android:toAlpha. Ending opacity offset, where 0.0 is transparent and 1.0 is opaque.

<animator>

Performs an animation over a specified amount of time.

<translate>

A vertical and/or horizontal motion.

Supports the attributes are in this three formats:

values from -100 to 100 ending with “%”, indicating a percentage relative to itself.

values from -100 to 100 ending in “%p”, indicating a percentage relative to its parent; a float value with no suffix, indicating an absolute value.

Implementation


Step 1 : Here is a simple animation XML file which animate from right to left in view for the period mentioned in duration filed.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="500">

    <translate
        android:fromXDelta="100%p"
        android:interpolator="@android:anim/decelerate_interpolator"
        android:toXDelta="0" />

    <alpha
        android:fromAlpha="0.5"
                    android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:toAlpha="1" />

</set>

Step 2 : Basically you need to create XML file with layoutAnimation container in your another XML file.

file name with layout_animation_right_to_left.xml is :

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation
    xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"
    android:animation="@anim/right_to_left"
    android:animationOrder="normal"
    android:delay="15%">

</layoutAnimation>

Step 3 : After that, the implementation of animation in your java should be like the following

final LayoutAnimationController controller =
        AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_right_to_left);

With the LayoutAnimationController object, you have set it to whatever view you want to animate.

Thank you for using pheonixsolutions.

If you find it useful, share it with your friends to keep it alive.

Leave a Reply