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 +} 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..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 @@ -46,6 +46,8 @@ class BubbleBuilder( internal var forceDragging: Boolean = true internal var isBubbleDraggable: Boolean = true + internal var isAnimatedBeforeExpand : Boolean = false + internal var expandViewInitialPoint : 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 { + expandViewInitialPoint.x = x + expandViewInitialPoint.y = y + return this + } + + fun pointOfShowExpandViewLocation(x: Int, y: Int) : BubbleBuilder { + 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 10ff669..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 @@ -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.expandViewInitialPoint ) 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.expandViewInitialPoint ) 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 +} 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) +}