From 107fe95e5b340f274f9506475715b6e95d168b04 Mon Sep 17 00:00:00 2001 From: Ehsan Mohit Date: Tue, 2 Jan 2024 04:35:32 +0330 Subject: [PATCH 1/5] Add two property to receive expand view init point and active animate or not --- .../service/expandable/BubbleBuilder.kt | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt index 04eb8eb..c14ab5f 100644 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt +++ b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt @@ -46,6 +46,8 @@ class BubbleBuilder( internal var forceDragging: Boolean = true internal var isBubbleDraggable: Boolean = true + internal var isAnimatedBeforeExpand : Boolean = false + internal var expandPoint : Point = Point(0,0) fun defaultLayoutParams(): WindowManager.LayoutParams { return WindowManager.LayoutParams().apply { flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or @@ -212,4 +214,21 @@ class BubbleBuilder( return this } -} \ No newline at end of file + fun animateBeforeExpand(animated:Boolean) : BubbleBuilder { + isAnimatedBeforeExpand = animated + return this + } + + fun pointOfShowExpandViewLocationPx(x: Int, y: Int) : BubbleBuilder { + expandPoint.x = x + expandPoint.y = y + return this + } + + fun pointOfShowExpandViewLocation(x: Int, y: Int) : BubbleBuilder { + expandPoint.x = x.toPx() + expandPoint.y = y.toPx() + return this + } + +} From 4e66d258a1fac79103805539abb3942d327db44e Mon Sep 17 00:00:00 2001 From: Ehsan Mohit Date: Tue, 2 Jan 2024 04:39:10 +0330 Subject: [PATCH 2/5] Modify FloatingBubble 1. This modification enables animation before expanding the view is shown. 2. Add a new lambda function to the animate method to notify when the animation is done. --- .../floatingbubbleview/bubble/FloatingBubble.kt | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt index 359bede..4f0f90c 100644 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt +++ b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/bubble/FloatingBubble.kt @@ -22,6 +22,8 @@ class FloatingBubble( private val forceDragging: Boolean = false, // private val ignoreSwipeGesture: Boolean = true, containCompose: Boolean, + val animateBeforeExpandViewShow : Boolean = false, + val locationBeforeExpand : Point = Point(0,0), private val listener: FloatingBubbleListener? = null, onDispatchKeyEvent: ((KeyEvent) -> Boolean?)? = null ) : Bubble( @@ -141,7 +143,12 @@ class FloatingBubble( /** * pass close bubble point * */ - fun animateTo(x: Float, y: Float, stiffness: Float = SpringForce.STIFFNESS_MEDIUM) { + fun animateTo( + x: Float, + y: Float, + stiffness: Float = SpringForce.STIFFNESS_MEDIUM, + onEnd: (() -> Unit)? = null + ) { AnimHelper.animateSpringPath( startX = newPoint.x.toFloat(), startY = newPoint.y.toFloat(), @@ -155,6 +162,11 @@ class FloatingBubble( // builder.listener?.onMove(x.toFloat(), y.toFloat()) // don't call this line, it'll spam multiple MotionEvent.OnActionMove update() } + + override fun onEnd() { + onEnd?.invoke() + super.onEnd() + } }, stiffness = stiffness, ) @@ -236,4 +248,4 @@ class FloatingBubble( } } } -} \ No newline at end of file +} From faea2cdca2f862cdfb406247ac80bf7465d920b9 Mon Sep 17 00:00:00 2001 From: Ehsan Mohit Date: Tue, 2 Jan 2024 04:40:56 +0330 Subject: [PATCH 3/5] Modify ExpandableBubbleService This modification uses the new properties of FloatingBubble (animateBeforeExpandViewShow, locationBeforeExpand) to choose whether to animate the bubble view before showing the expanded view. --- .../expandable/ExpandableBubbleService.kt | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt index 10ff669..4fd1ba9 100644 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt +++ b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt @@ -48,7 +48,9 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { context, forceDragging = bubbleBuilder.forceDragging, containCompose = false, - listener = bubbleBuilder.listener + listener = bubbleBuilder.listener, + animateBeforeExpandViewShow = bubbleBuilder.isAnimatedBeforeExpand, + locationBeforeExpand = bubbleBuilder.expandPoint ) bubble!!.rootGroup.addView(bubbleBuilder.bubbleView) } else { @@ -56,7 +58,9 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { context, forceDragging = bubbleBuilder.forceDragging, containCompose = true, - listener = bubbleBuilder.listener + listener = bubbleBuilder.listener, + animateBeforeExpandViewShow = bubbleBuilder.isAnimatedBeforeExpand, + locationBeforeExpand = bubbleBuilder.expandPoint ) bubble!!.rootGroup.addView(bubbleBuilder.bubbleCompose) } @@ -99,13 +103,13 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { context, containCompose = false, forceDragging = false, - onDispatchKeyEvent = expandedBuilder.onDispatchKeyEvent + onDispatchKeyEvent = expandedBuilder.onDispatchKeyEvent, ) expandedBubble!!.rootGroup.addView(expandedView) } else { expandedBubble = FloatingBubble( - context, + context = context, containCompose = true, forceDragging = false, onDispatchKeyEvent = expandedBuilder.onDispatchKeyEvent @@ -138,8 +142,18 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { } fun expand() { - expandedBubble!!.show() - bubble?.remove() + if (bubble?.animateBeforeExpandViewShow == true) { + animateBubbleToLocationPx( + x = bubble?.locationBeforeExpand?.x ?: 0, + y = bubble?.locationBeforeExpand?.y ?: 0 + ) { + expandedBubble!!.show() + bubble?.remove() + } + } else { + expandedBubble!!.show() + bubble?.remove() + } state = 2 } @@ -168,8 +182,13 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { } // testing - internal fun animateBubbleToLocationPx(x: Int, y: Int) { - bubble?.animateTo(x.toFloat(), y.toFloat(), stiffness = SpringForce.STIFFNESS_VERY_LOW) + internal fun animateBubbleToLocationPx(x: Int, y: Int, onAnimateEnd: (() -> Unit)? = null) { + bubble?.animateTo( + x.toFloat(), + y.toFloat(), + stiffness = SpringForce.STIFFNESS_VERY_LOW, + onEnd = onAnimateEnd + ) } // testing @@ -278,7 +297,7 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { sez.refresh() createBubbles(this, configBubble(), configExpandedBubble()) - when(state){ + when (state) { 1 -> minimize() 2 -> expand() } @@ -300,4 +319,4 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { } -} \ No newline at end of file +} From 153245c22fcec451fa09f7ea532f1e5cff76af99 Mon Sep 17 00:00:00 2001 From: Ehsan Mohit Date: Tue, 2 Jan 2024 04:41:08 +0330 Subject: [PATCH 4/5] Modify to use new feature --- .../torrydo/testfloatingbubble/MyServiceKt.kt | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt b/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt index 61f995b..a3e66e6 100644 --- a/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt +++ b/app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt @@ -1,5 +1,6 @@ package com.torrydo.testfloatingbubble +import android.graphics.Point import android.view.KeyEvent import android.view.LayoutInflater import android.view.View @@ -27,6 +28,7 @@ class MyServiceKt : ExpandableBubbleService() { } override fun configBubble(): BubbleBuilder? { + val showExpandViewPoint = calculateExpandViewStartPoint() val imgView = ViewHelper.fromDrawable(this, R.drawable.ic_rounded_blue_diamond, 60, 60) imgView.setOnClickListener { @@ -85,6 +87,8 @@ class MyServiceKt : ExpandableBubbleService() { override fun onFingerDown(x: Float, y: Float) {} // ..., when finger tap the bubble }) + .animateBeforeExpand(true) + .pointOfShowExpandViewLocationPx(showExpandViewPoint.x,0) } @@ -98,10 +102,10 @@ class MyServiceKt : ExpandableBubbleService() { return ExpandedBubbleBuilder(this) // .expandedView(expandedView) .expandedCompose { - TestComposeView(popBack = {minimize()}) + TestComposeView(popBack = { minimize() }) } .onDispatchKeyEvent { - if(it.keyCode == KeyEvent.KEYCODE_BACK){ + if (it.keyCode == KeyEvent.KEYCODE_BACK) { minimize() } null @@ -113,4 +117,12 @@ class MyServiceKt : ExpandableBubbleService() { .enableAnimateToEdge(true) .dimAmount(0.5f) } -} \ No newline at end of file +} + +private fun MyServiceKt.calculateExpandViewStartPoint(): Point { + val metrics = resources.displayMetrics + val bubbleViewWidthPx = (60 * metrics.density).toInt() + val startPositionWidth = (metrics.widthPixels / 2) - bubbleViewWidthPx + val startPositionHeight = metrics.heightPixels + return Point(startPositionWidth,startPositionHeight) +} From e45d23471dab2e42deb6cff484e57ec06e6d2c44 Mon Sep 17 00:00:00 2001 From: Ehsan Mohit Date: Tue, 2 Jan 2024 05:12:34 +0330 Subject: [PATCH 5/5] Rename property for increase readability of code. --- .../service/expandable/BubbleBuilder.kt | 10 +++++----- .../service/expandable/ExpandableBubbleService.kt | 4 ++-- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt index c14ab5f..0fc068c 100644 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt +++ b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/BubbleBuilder.kt @@ -47,7 +47,7 @@ class BubbleBuilder( internal var isBubbleDraggable: Boolean = true internal var isAnimatedBeforeExpand : Boolean = false - internal var expandPoint : Point = Point(0,0) + internal var expandViewInitialPoint : Point = Point(0,0) fun defaultLayoutParams(): WindowManager.LayoutParams { return WindowManager.LayoutParams().apply { flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE or @@ -220,14 +220,14 @@ class BubbleBuilder( } fun pointOfShowExpandViewLocationPx(x: Int, y: Int) : BubbleBuilder { - expandPoint.x = x - expandPoint.y = y + expandViewInitialPoint.x = x + expandViewInitialPoint.y = y return this } fun pointOfShowExpandViewLocation(x: Int, y: Int) : BubbleBuilder { - expandPoint.x = x.toPx() - expandPoint.y = y.toPx() + expandViewInitialPoint.x = x.toPx() + expandViewInitialPoint.y = y.toPx() return this } diff --git a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt index 4fd1ba9..a0b3e6b 100644 --- a/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt +++ b/FloatingBubbleView/src/main/java/com/torrydo/floatingbubbleview/service/expandable/ExpandableBubbleService.kt @@ -50,7 +50,7 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { containCompose = false, listener = bubbleBuilder.listener, animateBeforeExpandViewShow = bubbleBuilder.isAnimatedBeforeExpand, - locationBeforeExpand = bubbleBuilder.expandPoint + locationBeforeExpand = bubbleBuilder.expandViewInitialPoint ) bubble!!.rootGroup.addView(bubbleBuilder.bubbleView) } else { @@ -60,7 +60,7 @@ abstract class ExpandableBubbleService : FloatingBubbleService() { containCompose = true, listener = bubbleBuilder.listener, animateBeforeExpandViewShow = bubbleBuilder.isAnimatedBeforeExpand, - locationBeforeExpand = bubbleBuilder.expandPoint + locationBeforeExpand = bubbleBuilder.expandViewInitialPoint ) bubble!!.rootGroup.addView(bubbleBuilder.bubbleCompose) }