Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature set animation before expand show #58

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(),
Expand All @@ -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,
)
Expand Down Expand Up @@ -236,4 +248,4 @@ class FloatingBubble(
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -212,4 +214,21 @@ class BubbleBuilder(
return this
}

}
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
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,19 @@ 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 {
bubble = FloatingBubble(
context,
forceDragging = bubbleBuilder.forceDragging,
containCompose = true,
listener = bubbleBuilder.listener
listener = bubbleBuilder.listener,
animateBeforeExpandViewShow = bubbleBuilder.isAnimatedBeforeExpand,
locationBeforeExpand = bubbleBuilder.expandViewInitialPoint
)
bubble!!.rootGroup.addView(bubbleBuilder.bubbleCompose)
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -278,7 +297,7 @@ abstract class ExpandableBubbleService : FloatingBubbleService() {
sez.refresh()
createBubbles(this, configBubble(), configExpandedBubble())

when(state){
when (state) {
1 -> minimize()
2 -> expand()
}
Expand All @@ -300,4 +319,4 @@ abstract class ExpandableBubbleService : FloatingBubbleService() {
}


}
}
18 changes: 15 additions & 3 deletions app/src/main/java/com/torrydo/testfloatingbubble/MyServiceKt.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.torrydo.testfloatingbubble

import android.graphics.Point
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)

}

Expand All @@ -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
Expand All @@ -113,4 +117,12 @@ class MyServiceKt : ExpandableBubbleService() {
.enableAnimateToEdge(true)
.dimAmount(0.5f)
}
}
}

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)
}