Android Tutorial
Software Setup and Configuration
Android Studio
File Structure
Components
Core Topics
Layout
View
Button
Intent and Intent Filters
Toast
RecyclerView
Fragments
Adapters
Other UI Component
Image Loading Libraries
Date and Time
Material Design
Bars
Working with Google Maps
Chart
Animation
Database
Advance Android
Jetpack
Architecture
App Publish
App Monetization
Creating an animation similar to the Instagram or Twitter heart animation can be a delightful feature for users. This animation typically involves a quick scale-up and scale-down of the heart icon when liked.
Here's a basic outline followed by a simple implementation using Android's built-in animation framework:
Have a heart icon (e.g., ic_heart.xml
) in your res/drawable
folder. You can use Android's Vector Drawable or a PNG icon, depending on your preference.
In your XML layout (activity_main.xml
or wherever you need the icon):
<ImageView android:id="@+id/heartImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_heart" android:scaleType="centerInside"/>
Here, we'll create a simple scale animation that makes the heart briefly scale up (1.5x its original size) and then scale back down to its original size.
In res/anim/heart_scale.xml
:
<set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:duration="100" android:fromXScale="1.0" android:fromYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:toXScale="1.5" android:toYScale="1.5" /> <scale android:duration="100" android:fromXScale="1.5" android:fromYScale="1.5" android:pivotX="50%" android:pivotY="50%" android:startOffset="100" android:toXScale="1.0" android:toYScale="1.0" /> </set>
In your MainActivity.kt
or corresponding Kotlin/Java file:
val heartImageView: ImageView = findViewById(R.id.heartImageView) heartImageView.setOnClickListener { val animation = AnimationUtils.loadAnimation(this, R.anim.heart_scale) it.startAnimation(animation) }
With this setup, when the heart icon is tapped, it will briefly scale up and then return to its original size, mimicking the like animation seen on platforms like Instagram and Twitter.
You can further refine and customize this animation, e.g., by adding a bounce interpolator or tweaking the scale and duration values to best match the desired effect. Also, sophisticated libraries like Lottie allow for complex animations and can be used to replicate more detailed animations, especially if you have a design team providing custom animation assets.
Creating a heart animation in Android Kotlin:
// Example of creating a heart animation val heartImageView: ImageView = findViewById(R.id.heartImageView) val heartAnimation = AnimationUtils.loadAnimation(this, R.anim.heart_pulse_animation) heartImageView.startAnimation(heartAnimation)
Twitter heart animation example in Android:
// Example of Twitter-like heart animation val heartImageView: ImageView = findViewById(R.id.heartImageView) val twitterHeartAnimation = AnimationUtils.loadAnimation(this, R.anim.twitter_heart_animation) heartImageView.startAnimation(twitterHeartAnimation)
Animating heart-shaped icons in Android:
// Example of animating a heart-shaped icon val heartIcon: ImageView = findViewById(R.id.heartIcon) val scaleAnimation = ObjectAnimator.ofPropertyValuesHolder( heartIcon, PropertyValuesHolder.ofFloat("scaleX", 1.0f, 1.2f, 1.0f), PropertyValuesHolder.ofFloat("scaleY", 1.0f, 1.2f, 1.0f) ) scaleAnimation.duration = 500 scaleAnimation.start()
Heart animation on button click in Android:
// Example of heart animation on button click val likeButton: Button = findViewById(R.id.likeButton) val heartImageView: ImageView = findViewById(R.id.heartImageView) likeButton.setOnClickListener { val heartAnimation = AnimationUtils.loadAnimation(this, R.anim.heart_pulse_animation) heartImageView.startAnimation(heartAnimation) }
Customizing heart-like animations in Android:
// Example of customizing a heart-like animation val heartIcon: ImageView = findViewById(R.id.heartIcon) val customAnimation = ObjectAnimator.ofPropertyValuesHolder( heartIcon, PropertyValuesHolder.ofFloat("scaleX", 1.0f, 1.5f, 1.0f), PropertyValuesHolder.ofFloat("scaleY", 1.0f, 1.5f, 1.0f) ) customAnimation.duration = 700 customAnimation.interpolator = AccelerateDecelerateInterpolator() customAnimation.start()
Animating hearts with ObjectAnimator in Android:
// Example of animating hearts with ObjectAnimator val heartIcon: ImageView = findViewById(R.id.heartIcon) val scaleAnimation = ObjectAnimator.ofPropertyValuesHolder( heartIcon, PropertyValuesHolder.ofFloat("scaleX", 1.0f, 1.5f, 1.0f), PropertyValuesHolder.ofFloat("scaleY", 1.0f, 1.5f, 1.0f) ) scaleAnimation.duration = 700 scaleAnimation.start()
Pulse animation for heart icons in Android:
// Example of pulse animation for heart icons val heartIcon: ImageView = findViewById(R.id.heartIcon) val pulseAnimation = ObjectAnimator.ofPropertyValuesHolder( heartIcon, PropertyValuesHolder.ofFloat("scaleX", 1.0f, 1.2f, 1.0f), PropertyValuesHolder.ofFloat("scaleY", 1.0f, 1.2f, 1.0f) ) pulseAnimation.duration = 800 pulseAnimation.repeatCount = ObjectAnimator.INFINITE pulseAnimation.repeatMode = ObjectAnimator.REVERSE pulseAnimation.start()
Adding like animation to ImageView in Android:
// Example of adding like animation to ImageView val likeImageView: ImageView = findViewById(R.id.likeImageView) val likeAnimation = AnimationUtils.loadAnimation(this, R.anim.like_animation) likeImageView.startAnimation(likeAnimation)