
An android scale animation changes a view’s size over time, creating effects such as zooming, shrinking, button feedback, and card emphasis. Android supports scale animations through XML resources, Kotlin property animation APIs, and modern animation tools. This guide explains how to create them, control their behavior, and avoid common visual and accessibility problems.
How Android Scale Animation Works
A scale animation changes a view’s horizontal and vertical scale:
scaleXcontrols width.scaleYcontrols height.scaleX = 1fandscaleY = 1frepresent the original size.- Values below
1fshrink the view. - Values above
1fenlarge it.
For example, scaling a view from 1f to 1.2f makes it appear 20% larger. The view’s layout dimensions do not change; Android transforms how the view is drawn on screen.
Scaling usually occurs around a pivot point. By default, the pivot is near the center of the view, so the view grows outward in all directions. You can change the pivot with pivotX and pivotY, which is useful for effects such as a menu expanding from its corner.
There are two main animation systems:
- View animation, often defined in XML.
- Property animation, which changes actual view properties such as
scaleXandscaleY.
Property animation is generally more flexible and is the preferred option for new Kotlin-based projects.
Create an Android Scale Animation in XML
XML animations are convenient when you want reusable resources managed separately from your Kotlin or Java code.
Create a file at:
``text res/anim/scale_up.xml ``
Add this animation:
```xml <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:interpolator/accelerate_decelerate">
<scale android:duration="250" android:fromXScale="1.0" android:fromYScale="1.0" android:toXScale="1.15" android:toYScale="1.15" android:pivotX="50%" android:pivotY="50%" /> </set> ```
Start it from an Activity or Fragment:
``kotlin val animation = AnimationUtils.loadAnimation(this, R.anim.scale_up) binding.cardView.startAnimation(animation) ``
This enlarges the card to 115% of its original visual size over 250 milliseconds.
XML view animations are simple, but they mainly affect rendering. If you need to read or continue using the changed scale value, property animation is usually a better choice. Also remember that an XML animation may visually move a view without changing its layout position, which can affect touch behavior and clipping.
Build an Android Scale Animation with Kotlin
For most interactive UI effects, use ViewPropertyAnimator. It provides concise code and automatically animates view properties.
```kotlin binding.playButton.apply { scaleX = 0.8f scaleY = 0.8f alpha = 0f
animate() .scaleX(1f) .scaleY(1f) .alpha(1f) .setDuration(300L) .start() } ```
This example combines scaling and fading to reveal a button. Because scaleX and scaleY are real view properties, the final state remains available after the animation finishes.
For a press effect, shrink the view briefly and restore it when the touch ends:
```kotlin binding.watchButton.setOnTouchListener { view, event -> when (event.actionMasked) { MotionEvent.ACTION_DOWN -> { view.animate() .scaleX(0.94f) .scaleY(0.94f) .setDuration(80L) .start() }
MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> { view.animate() .scaleX(1f) .scaleY(1f) .setDuration(100L) .start() } }
false } ```
For a one-time animation with more control, use ObjectAnimator:
```kotlin val scaleX = ObjectAnimator.ofFloat(view, View.SCALE_X, 1f, 1.2f, 1f) val scaleY = ObjectAnimator.ofFloat(view, View.SCALE_Y, 1f, 1.2f, 1f)
AnimatorSet().apply { playTogether(scaleX, scaleY) duration = 350L interpolator = OvershootInterpolator() start() } ```
Use AnimatorSet when several properties must run together or in sequence.
Improve Timing, Pivot Points, and Performance
Animation timing strongly affects how natural an effect feels. Short interactions, such as button presses, commonly work well between 80 and 150 milliseconds. Emphasis or entrance animations can use approximately 200 to 400 milliseconds. Avoid making routine interactions slow because users may interpret the delay as poor performance.
Interpolators control the rate of change:
AccelerateDecelerateInterpolatorstarts and ends gently.FastOutSlowInInterpolatoris common in Material-style interfaces.OvershootInterpolatorcreates a spring-like enlargement.LinearInterpolatormoves at a constant rate but can feel mechanical.
Set the pivot when the default center-based effect is not appropriate:
```kotlin view.pivotX = 0f view.pivotY = view.height.toFloat()
view.animate() .scaleX(1.1f) .scaleY(1.1f) .setDuration(220L) .start() ```
This makes the view expand from its bottom-left corner. Set the pivot after layout if you depend on the view’s measured width or height.
Scale transforms are usually efficient because Android can perform them during rendering without rebuilding the entire layout. However, avoid animating large, complex views unnecessarily. Hardware acceleration, shadows, transparency, and nested layouts can increase rendering costs. Test on an older Android device and watch for dropped frames.
Also prevent conflicting animations. Calling animate() repeatedly while an earlier animation is still running can produce inconsistent results. Call view.animate().cancel() before starting a new animation when necessary.
Accessibility and Common Troubleshooting
Animations should support the interface rather than block it. Respect the system’s reduced-motion preference when your app provides extensive motion. You can check Android’s animator duration scale setting through system settings and reduce or disable nonessential effects when appropriate.
Do not communicate important information through scale alone. A button that becomes larger should still have a clear label, state, or content description. Make sure scaling does not push content outside its parent or make controls overlap.
If the animation appears clipped, inspect the parent container’s clipping behavior and available space. If the view jumps when returning to its original size, explicitly set both scaleX and scaleY to 1f. If the effect grows from the wrong location, adjust pivotX and pivotY.
For RecyclerView items, reset animated properties when binding:
``kotlin holder.itemView.scaleX = 1f holder.itemView.scaleY = 1f holder.itemView.alpha = 1f ``
Without this reset, recycled views may retain the scale from a previous item.
Android Scale Animation FAQ
What is the simplest way to make a view grow when tapped?
Use ViewPropertyAnimator:
``kotlin view.animate() .scaleX(1.1f) .scaleY(1.1f) .setDuration(200L) .start() ``
Restore both properties to 1f when the effect should end.
Does scaling change a view’s layout size?
No. Scaling changes the view’s visual rendering, not its measured width, height, or position in the layout. Use layout changes or constraints when surrounding views must move.
Why does my Android scale animation look blurry or get clipped?
Large scaling can reveal filtering limitations, while parent containers may clip transformed content. Keep scale values reasonable, check parent clipping settings, and test on multiple screen sizes and devices.
Conclusion
An android scale animation is an effective way to provide feedback, emphasize content, or create polished transitions. Start with ViewPropertyAnimator for simple Kotlin effects, use XML for reusable resources, and tune duration, interpolators, and pivot points carefully. Always reset animated properties, test performance, and respect accessibility preferences for a reliable user experience.