Skip to content

Commit

Permalink
added: BalloonWindowListener
Browse files Browse the repository at this point in the history
  • Loading branch information
hyuntae0117 committed Feb 8, 2019
1 parent 5247611 commit 5f5ded8
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 5 deletions.
35 changes: 30 additions & 5 deletions BalloonWindow/src/main/java/com/ht/balloonwindow/BalloonWindow.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,14 @@ class BalloonWindow : PopupWindow {

var balloonColor: Int? = null

var x: Int = 0
var y: Int = 0

private var measuredContentsWidth: Int? = null
private var measuredContentsHeight: Int? = null

var listener: BalloonWindowListener? = null

constructor(context: Context, targetView: View, position: Position, offset: Int = 0) : super(context) {
this.context = context
this.position = position
Expand All @@ -49,26 +54,28 @@ class BalloonWindow : PopupWindow {
}

override fun dismiss() {
listener?.willDisappear(this)

val animation = AlphaAnimation(1f, 0f)
animation.duration = 100
contentView.startAnimation(animation)
animation.setAnimationListener(object: Animation.AnimationListener{
override fun onAnimationRepeat(p0: Animation?) {}

override fun onAnimationEnd(p0: Animation?) {
exit()
super@BalloonWindow.dismiss()

listener?.didDisappear(this@BalloonWindow)
}

override fun onAnimationStart(p0: Animation?) {
}
})
}

fun exit() {
super.dismiss()
}

fun show(view: View) {
listener?.willAppear(this)

contentView = BalloonView(context, view)
val balloonView = (contentView as BalloonView)
balloonView.contentsLl.setPadding(
Expand All @@ -84,6 +91,10 @@ class BalloonWindow : PopupWindow {
paddingBottom = bottom
}

fun setBalloonListener(listener: BalloonWindowListener) {
this.listener = listener
}

private fun getAbsolutePosition(view: View): Pair<Int, Int> {
val locations = IntArray(2)
view.getLocationOnScreen(locations)
Expand Down Expand Up @@ -137,9 +148,22 @@ class BalloonWindow : PopupWindow {

update(xPos, yPos, this@BalloonWindow.width, this@BalloonWindow.height)

x = xPos
y = yPos
val anim = ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, pivotX, Animation.RELATIVE_TO_SELF, pivotY)
anim.duration = 300
contentView.startAnimation(anim)
anim.setAnimationListener(object: Animation.AnimationListener{
override fun onAnimationStart(p0: Animation?) {
}

override fun onAnimationRepeat(p0: Animation?) {
}

override fun onAnimationEnd(p0: Animation?) {
listener?.didAppear(this@BalloonWindow)
}
})
}

inner class BalloonView: ConstraintLayout {
Expand Down Expand Up @@ -177,6 +201,7 @@ class BalloonWindow : PopupWindow {
set.connect(R.id.contentsLl, ConstraintSet.RIGHT, R.id.container, ConstraintSet.RIGHT)
set.applyTo(container)


arrowTopIv.visibility = View.VISIBLE
arrowRightIv.visibility = View.GONE
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.ht.balloonwindow

interface BalloonWindowListener {
fun willAppear(window: BalloonWindow) {

}

fun didAppear(window: BalloonWindow) {

}

fun willDisappear(window: BalloonWindow) {

}

fun didDisappear(window: BalloonWindow) {

}
}
21 changes: 21 additions & 0 deletions app/src/main/java/com/ht/example/balloonwindow/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
package com.ht.example.balloonwindow

import android.animation.ValueAnimator
import android.graphics.Color
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v4.content.ContextCompat
import android.util.Log
import android.widget.TextView
import com.ht.balloonwindow.BalloonWindow
import com.ht.balloonwindow.BalloonWindowListener
import kotlinx.android.synthetic.main.activity_main.*
import kotlin.math.PI
import kotlin.math.sign
import kotlin.math.sin

class MainActivity : AppCompatActivity() {

Expand All @@ -23,7 +29,22 @@ class MainActivity : AppCompatActivity() {
window.offset = -35
window.show(view)

val anim = ValueAnimator.ofFloat(0f, PI.toFloat())
anim.duration = 1500
anim.repeatCount = 3

window.setBalloonListener(object: BalloonWindowListener {
override fun didAppear(window: BalloonWindow) {
anim.addUpdateListener {
val value = it.animatedValue as Float
val sign = sin(value)
window.update(window.x, (window.y + (30 * sign)).toInt(), window.width, window.height)
}
anim.start()
}
})
}

belowBtn.setOnClickListener {
val view = TextView(this)
view.text = "position:below\noffset:55\nmargin:25\nballoonColor: #f1f1f1"
Expand Down

0 comments on commit 5f5ded8

Please sign in to comment.