Android Animations in Kotlin (original) (raw)

Last Updated : 17 May, 2022

Animation is a method in which a collection of images are combined in a specific way and processed then they appear as moving images. Building animations make on-screen objects seem to be alive. Android has quite a few tools to help you create animations with relative ease. so in this article we will learn to create animations using Kotlin. below are some attributes which we are using while writing the code in xml.

Table of Attributes :

XML ATTRIBUTES DESCRIPTION
android:duration It is used to specify the duration of animation to run
android:fromAlpha It is the starting alpha value for the animation, where 1.0 means fully opaque and 0.0 means fully transparent
android:toAlpha It is the ending alpha value
android:id Sets unique id of the view
android:fromYDelta It is the change in Y coordinate to be applied at the start of the animation
android:toYDelta It is the change in Y coordinate to be applied at the end of the animation
android:startOffset Delay occur when an animation runs (in milliseconds), once start time is reached.
android:pivotX It represents the X-axis coordinates to zoom from starting point.
android:pivotY It represents the Y-axis coordinates to zoom from starting point.
android:fromXScale Starting X size offset,
android:fromYScale Starting Y size offset,
android:toXScale Ending of X size offset
android:toYScale Ending of Y size offset
android:fromDegrees Starting angular position, in degrees.
android:toDegrees Ending angular position, in degrees.
android:interpolator An interpolator defines the rate of change of an animation

At first, we will create a new android application. Then, we will create some animations.
If you already created the project then ignore step 1.

Create New Project

  1. Open Android Studio
  2. Go to File => New => New Project.
  3. Then, select Empty Activity and click on next
    • Write application name as DynamicEditTextKotlin
    • Select minimum SDK as you need, here we have selected 21 as minimum SDK
    • Choose language as Kotlin and click on finish button.

If you have followed above process correctly, you will get a newly created project successfully.
After creating project we will modify xml files. In xml file we will create one TextView where all the animations are performed and Eight Buttons for Eight different animations.

Modify activity_main.xml file

Open res/layout/activity_main.xml file and add code into it.

XML `

`

After modifying the layout we will create xml files for animations. so we will first create a folder name anim.
In this folder, we will be adding the XML files which will be used to produce the animations. For this to happen, go to app/res right click and then select, Android Resource Directory and name it as anim.

bounce.xml

In this animation the text is bounce like a ball.

XML `

`

fade_in.xml

In Fade In animation the text will appear from background.

XML `

`

fade_out.xml

In Fade Out animation the colour of text is faded for a particular amount of time.

XML `

`

rotate.xml

In rotate animation the text is rotated for a particular amount of time.

XML `

`

slide_down.xml

In this animation the text will come from top to bottom.

XML `

`

slide_up.xml

In this animation the text will go from bottom to top.

XML `

`

zoom_in.xml

In this animation the text will appear bigger for a particular amount of time.

XML `

`

zoom_out.xml

In this animation the text will appear smaller for a particular amount of time.

XML `

`

After creating all animations in xml. we will create MainActivity.

Create MainActivity.kt file

Open app/src/main/java/net.geeksforgeeks.AnimationsInKotlin/MainActivity.kt file and add below code into it.

Kotlin `

package net.geeksforgeeks.animationsinkotlin

import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import android.os.Handler import android.view.View import android.view.animation.AnimationUtils import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) fade_in.setOnClickListener { textView.visibility = View.VISIBLE val animationFadeIn = AnimationUtils.loadAnimation(this, R.anim.fade_in) textView.startAnimation(animationFadeIn) } fade_out.setOnClickListener { val animationFadeOut = AnimationUtils.loadAnimation(this, R.anim.fade_out) textView.startAnimation(animationFadeOut) Handler().postDelayed({ textView.visibility = View.GONE }, 1000) } zoom_in.setOnClickListener { val animationZoomIn = AnimationUtils.loadAnimation(this, R.anim.zoom_in) textView.startAnimation(animationZoomIn) } zoom_out.setOnClickListener { val animationZoomOut = AnimationUtils.loadAnimation(this, R.anim.zoom_out) textView.startAnimation(animationZoomOut) } slide_down.setOnClickListener { val animationSlideDown = AnimationUtils.loadAnimation(this, R.anim.slide_in) textView.startAnimation(animationSlideDown) } slide_up.setOnClickListener { val animationSlideUp = AnimationUtils.loadAnimation(this, R.anim.slide_out) textView.startAnimation(animationSlideUp) } bounce.setOnClickListener { val animationBounce = AnimationUtils.loadAnimation(this, R.anim.bounce) textView.startAnimation(animationBounce) } rotate.setOnClickListener { val animationRotate = AnimationUtils.loadAnimation(this, R.anim.rotate) textView.startAnimation(animationRotate) } } }

`

As, AndroidManifest.xml file is very important file in android application, so below is the code of manifest file.

AndroidManifest.xml file

Code inside src/main/AndroidManifest.xml file would look like below

XML `

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

`

Run as Emulator:

You can find the complete code here: https://github.com/missyadavmanisha/AnimationsInKotlin