Pular para o conteúdo principal

Animator class Examples

Understanding the Animator Class in Android.

Android provides a powerful animation framework that allows developers to create complex animations that can be applied to UI elements. The Animator class is one of the key components of this framework, and it provides the foundation for creating animations in Android.

In this article, we'll take a detailed look at the Animator class in Android, and explore how it can be used to create animations in your Android applications.

What is the Animator Class?

The Animator class is a core component of the Android animation framework, and it provides a way to create and manage animations in your application. The Animator class is designed to work with a variety of different animation types, including property animations and value animations.

At its core, the Animator class provides a way to specify a set of values that should be animated over a specified period of time. These values can represent any number of different properties, such as the position, scale, or opacity of a UI element.

Creating an Animator

To create an Animator object in your Android application, you can use the ObjectAnimator class. Here's an example of how to create an ObjectAnimator that animates the alpha property of a View:

val anim = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f)
anim.duration = 1000
anim.start()

In this example, we're creating an ObjectAnimator that targets the "alpha" property of a View. The animation will start with an alpha value of 1.0 (fully opaque), and end with an alpha value of 0.0 (fully transparent). The duration of the animation is set to 1000 milliseconds, and the animation is started immediately.

Creating a ValueAnimator

In addition to ObjectAnimator, the Animator class also provides a ValueAnimator class. ValueAnimator is a more general-purpose animation class that can be used to animate any arbitrary set of values. Here's an example of how to create a ValueAnimator that animates a set of integers:

val anim = ValueAnimator.ofInt(0, 100)
anim.duration = 1000
anim.addUpdateListener { valueAnimator ->
val value = valueAnimator.animatedValue as Int
// Update UI with the new value
}
anim.start()

In this example, we're creating a ValueAnimator that animates a set of integers from 0 to 100. The duration of the animation is set to 1000 milliseconds, and we've added an update listener to the animation using the addUpdateListener() method. This listener will be called every time the animation is updated, and we can use it to update UI elements with the new value of the animation.

Using Interpolators

In addition to specifying the values and duration of an animation, you can also use Interpolators to control the rate at which an animation progresses over time. Interpolators can be used to create a variety of different animation effects, such as easing in and out of an animation or creating a bounce effect.

Here's an example of how to use an Interpolator with an ObjectAnimator:

val interpolator = AccelerateDecelerateInterpolator()
val anim = ObjectAnimator.ofFloat(view, "alpha", 1f, 0f)
anim.duration = 1000
anim.interpolator = interpolator
anim.start()

In this example, we're using the AccelerateDecelerateInterpolator to create an animation that starts slowly, accelerates in the middle, and then slows down again at the end. We've set the interpolator on the ObjectAnimator using the interpolator property.

Examples

Circular Reveal using Animator

In this example you will learn how to implement the circular reveal using the Animator class.

Step 1: Create Project

Start by creating an empty Android Studio project.

Step 2: Dependencies

No external dependencies are needed for this project.

Step : Write Code

MainActivity.kt

import android.content.res.ColorStateList
import android.opengl.Visibility
import android.os.Build
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.core.content.res.ResourcesCompat
import android.view.View
import android.view.ViewAnimationUtils
import kotlinx.android.synthetic.main.activity_main.*
import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import kotlin.math.hypot

class MainActivity : AppCompatActivity() {

var isOpen: Boolean = false

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

private fun revealMenu() {
if (!isOpen) {
val x = main_layout.right
val y = main_layout.bottom

val startRadius = 0
val endRadius = hypot(main_layout.width.toDouble(), main_layout.height.toDouble())

floating_button.backgroundTintList = ColorStateList.valueOf(ResourcesCompat
.getColor(applicationContext.resources, android.R.color.white, null))
floating_button.setImageResource(R.drawable.ic_close_black_24dp)
if (Build.VERSION.SDK_INT >= 21) {
val anim = ViewAnimationUtils.createCircularReveal(menu_layout,
x, y, startRadius.toFloat(), endRadius.toFloat())
//duration can be custom according to you even can be left and default value will be taken
//in ms
anim.duration=800
menu_layout.visibility = View.VISIBLE
anim.start()
isOpen = true
} else {
//When below API 21 for example in Kitkat
menu_layout.visibility = View.VISIBLE
isOpen = true
}
} else {
val x = layout_text.left
val y = layout_text.top

val startRadius = 0
val end = hypot(main_layout.width.toDouble(), main_layout.height.toDouble())

floating_button.backgroundTintList = ColorStateList.valueOf(ResourcesCompat
.getColor(applicationContext.resources, R.color.colorPrimary, null))
floating_button.setImageResource(R.drawable.ic_add_white_24dp)
if (Build.VERSION.SDK_INT >= 21) {
val anim = ViewAnimationUtils.createCircularReveal(layout_text,
x, y, startRadius.toFloat(), end.toFloat())
//duration can be custom according to you even can be left and default value will be taken
//in ms
anim.duration=800
anim.addListener(object : AnimatorListenerAdapter(){
override fun onAnimationEnd(animation: Animator?) {
super.onAnimationEnd(animation)
menu_layout.visibility = View.GONE
}
})
anim.start()
isOpen = false
} else {
//When below API 21 for example in Kitkat
menu_layout.visibility = View.GONE
isOpen = false
}
}
}
}

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

Conclusion

The Animator class is a powerful component of the Android animation framework, and it provides a way to create complex animations in your Android applications. In this article, we've explored how to create both ObjectAnimator and ValueAnimator objects, and how to use Interpolators to control the rate at which animations progress over time.

With the Animator class, you can create a wide variety of different animation effects that can help make your Android applications more engaging and interactive.