メインコンテンツまでスキップ

An Introduction to ObjectAnimator in Android

An Introduction to ObjectAnimator in Android

ObjectAnimator is a powerful class in the Android framework that allows you to animate the properties of an object. It's a part of the animation API that provides a way to create smooth and seamless animations in your Android apps. In this article, we'll explore what ObjectAnimator is, how it works, and how you can use it in your Android applications.

What is ObjectAnimator?

ObjectAnimator is a class that allows you to animate the properties of an object in Android. It's a part of the animation API that provides a way to create smooth and seamless animations in your Android apps. With ObjectAnimator, you can animate any property of an object that has a setter method. This means that you can animate properties like alpha, rotation, translation, scale, and more.

How Does ObjectAnimator Work?

ObjectAnimator works by interpolating the values of an object's property over a specified duration. The interpolation is done using an easing function, which determines how the values change over time. ObjectAnimator uses a ValueAnimator under the hood, which is responsible for updating the values of the object's property during the animation.

Using ObjectAnimator in Your Android Apps

To use ObjectAnimator in your Android apps, you first need to create an instance of the ObjectAnimator class and specify the property you want to animate. Here's an example that animates the alpha property of a TextView:

TextView textView = findViewById(R.id.text_view);
ObjectAnimator animator = ObjectAnimator.ofFloat(textView, "alpha", 0f, 1f);
animator.setDuration(1000);
animator.start();

In this example, we create an instance of ObjectAnimator and specify the TextView object we want to animate, along with the property we want to animate (alpha). We then specify the initial value for the alpha property (0f) and the final value we want to animate to (1f). Finally, we set the duration of the animation to 1000 milliseconds and start the animation.

Let us look at some examples.

Example 1: Kotlin Android ObjectAnimator

Learn ObjectAnimator using the following simple example.

Here is the demo screenshot:

ObjectAnimator Example

Step 1: Create Project

Start by creating an empty Android Studio project.

Step 2: Dependencies

No external dependency is needed for this project.

Step 3: Design Layout

Design layout as follows:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.developers.objectanimator.MainActivity">

<Button
android:id="@+id/animate_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="15dp"
android:layout_marginEnd="15dp"
android:layout_marginStart="15dp"
android:text="@string/start_animating_text" />

<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Hello World!"
android:textColor="@android:color/black"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

Step 4: Animate

MainActivity.kt

import android.animation.ObjectAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
animate_button.setOnClickListener {
val animator = ObjectAnimator.ofFloat(textView, "textSize", 12f)
animator.duration = 800
animator.start()
}
}
}

Run

Copy the code or download it in the link below, build and run.

Reference

Here are the reference links:

NumberLink
1.Download Example
2.Follow code author
3.Code: Apache 2.0 License

Example 2: Kotlin Android Animated ImageView Example

This example explores how to programmatically make an animated ImageView based on AppCompatImageView and using the ObjectAnimator to animate its images.

This example will comprise the following files:

  • AnimImageView.kt
  • MainActivity.kt

Step 1: Create Project

  1. Open your AndroidStudio IDE.
  2. Go to File-->New-->Project to create a new project.

Step 2: Dependencies

No external or special dependencies are needed for this project.

Step 3: Design Layouts

*(a). activity_main.xml

Create a file named activity_main.xml and design it as follows. You can see that we are using the custom ImageView to render an Image:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.numero.anim_example.MainActivity">

<com.numero.anim_example.AnimImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:src="@drawable/circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Step 4: Write Code

Write Code as follows:

(a). AnimImageView.kt

Create a file named AnimImageView.kt and in it extend the AppCompatImageView and write code as follows:

Here is the full code

package com.numero.anim_example

import android.animation.ObjectAnimator
import android.content.Context
import android.support.v7.widget.AppCompatImageView
import android.util.AttributeSet
import android.animation.PropertyValuesHolder

class AnimImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : AppCompatImageView(context, attrs, defStyleAttr) {

val holderX = PropertyValuesHolder.ofFloat("alpha", 1f, 0f)
val scaleX = PropertyValuesHolder.ofFloat("scaleX", 1f, 2f)
val scaleY = PropertyValuesHolder.ofFloat("scaleY", 1f, 2f)

val anim = ObjectAnimator.ofPropertyValuesHolder(this, holderX, scaleX, scaleY).apply {
duration = 700
repeatMode = ObjectAnimator.RESTART
repeatCount = ObjectAnimator.INFINITE
}

override fun onAttachedToWindow() {
super.onAttachedToWindow()
anim.start()
}

override fun onDetachedFromWindow() {
super.onDetachedFromWindow()
anim.end()
}
}

(b). MainActivity.kt

Create a file named MainActivity.kt.

Here is the full code

package com.numero.anim_example

import android.support.v7.app.AppCompatActivity
import android.os.Bundle

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}

Run

Simply copy the source code into your Android Project,Build and Run.

Reference

  1. Download code or Browse here.
  2. Follow code author.

Conclusion

ObjectAnimator is a powerful class in the Android framework that allows you to animate the properties of an object. It's a part of the animation API that provides a way to create smooth and seamless animations in your Android apps. With ObjectAnimator, you can animate any property of an object that has a setter method. So, go ahead and start experimenting with ObjectAnimator in your Android apps to create beautiful and engaging animations.