From 12d31df66f403bb3ef9f7a51ec19bca0d424e274 Mon Sep 17 00:00:00 2001 From: Naveen Singh Date: Mon, 7 Oct 2024 17:50:21 +0530 Subject: [PATCH] Add scale on click animation --- .../launcher/adapters/LaunchersAdapter.kt | 36 +++++++++++++++++++ .../org/fossify/launcher/extensions/View.kt | 18 ++++++++++ .../main/res/layout/item_launcher_label.xml | 1 - 3 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 app/src/main/kotlin/org/fossify/launcher/extensions/View.kt diff --git a/app/src/main/kotlin/org/fossify/launcher/adapters/LaunchersAdapter.kt b/app/src/main/kotlin/org/fossify/launcher/adapters/LaunchersAdapter.kt index 540c2d9..cbe85a3 100644 --- a/app/src/main/kotlin/org/fossify/launcher/adapters/LaunchersAdapter.kt +++ b/app/src/main/kotlin/org/fossify/launcher/adapters/LaunchersAdapter.kt @@ -3,6 +3,7 @@ package org.fossify.launcher.adapters import android.annotation.SuppressLint import android.graphics.drawable.Drawable import android.view.LayoutInflater +import android.view.MotionEvent import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.DiffUtil @@ -19,6 +20,7 @@ import org.fossify.commons.extensions.realScreenSize import org.fossify.launcher.R import org.fossify.launcher.activities.SimpleActivity import org.fossify.launcher.databinding.ItemLauncherLabelBinding +import org.fossify.launcher.extensions.animateScale import org.fossify.launcher.extensions.config import org.fossify.launcher.interfaces.AllAppsListener import org.fossify.launcher.models.AppLauncher @@ -73,6 +75,7 @@ class LaunchersAdapter( } inner class ViewHolder(view: View) : RecyclerView.ViewHolder(view) { + @SuppressLint("ClickableViewAccessibility") fun bindView(launcher: AppLauncher): View { val binding = ItemLauncherLabelBinding.bind(itemView) itemView.apply { @@ -113,6 +116,30 @@ class LaunchersAdapter( ) true } + + setOnTouchListener { _, event -> + when (event.action) { + MotionEvent.ACTION_DOWN -> { + binding.launcherIcon.drawable.alpha = LAUNCHER_ALPHA_PRESSED + animateScale( + from = LAUNCHER_SCALE_NORMAL, + to = LAUNCHER_SCALE_PRESSED, + duration = LAUNCHER_SCALE_UP_DURATION + ) + } + + MotionEvent.ACTION_UP, + MotionEvent.ACTION_CANCEL -> { + binding.launcherIcon.drawable.alpha = LAUNCHER_ALPHA_NORMAL + animateScale( + from = LAUNCHER_SCALE_PRESSED, + to = LAUNCHER_SCALE_NORMAL, + duration = LAUNCHER_SCALE_DOWN_DURATION + ) + } + } + false + } } return itemView @@ -120,6 +147,15 @@ class LaunchersAdapter( } override fun onChange(position: Int) = currentList.getOrNull(position)?.getBubbleText() ?: "" + + companion object { + private const val LAUNCHER_SCALE_NORMAL = 1f + private const val LAUNCHER_SCALE_PRESSED = 1.15f + private const val LAUNCHER_SCALE_UP_DURATION = 100L + private const val LAUNCHER_SCALE_DOWN_DURATION = 50L + private const val LAUNCHER_ALPHA_NORMAL = 255 + private const val LAUNCHER_ALPHA_PRESSED = 200 + } } private class AppLauncherDiffCallback : DiffUtil.ItemCallback() { diff --git a/app/src/main/kotlin/org/fossify/launcher/extensions/View.kt b/app/src/main/kotlin/org/fossify/launcher/extensions/View.kt new file mode 100644 index 0000000..dcb17af --- /dev/null +++ b/app/src/main/kotlin/org/fossify/launcher/extensions/View.kt @@ -0,0 +1,18 @@ +package org.fossify.launcher.extensions + +import android.view.View +import android.view.animation.AccelerateDecelerateInterpolator + +fun View.animateScale( + from: Float, + to: Float, + duration: Long, +) = animate() + .scaleX(to) + .scaleY(to) + .setDuration(duration) + .setInterpolator(AccelerateDecelerateInterpolator()) + .withStartAction { + scaleX = from + scaleY = from + } \ No newline at end of file diff --git a/app/src/main/res/layout/item_launcher_label.xml b/app/src/main/res/layout/item_launcher_label.xml index 360970a..8834fd9 100644 --- a/app/src/main/res/layout/item_launcher_label.xml +++ b/app/src/main/res/layout/item_launcher_label.xml @@ -3,7 +3,6 @@ android:id="@+id/launcher_holder" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:background="?attr/selectableItemBackground" android:clickable="true" android:focusable="true" android:paddingStart="@dimen/small_margin"