import
android.animation.Animator
import
android.annotation.SuppressLint
import
android.content.res.ColorStateList
import
android.os.Build
import
android.os.Bundle
import
android.view.View
import
android.view.ViewAnimationUtils
import
androidx.annotation.RequiresApi
import
androidx.appcompat.app.AppCompatActivity
import
androidx.core.content.res.ResourcesCompat
import
com.google.android.material.floatingactionbutton.FloatingActionButton
import
kotlin.math.hypot
import
kotlin.math.max
class
MainActivity : AppCompatActivity() {
private
lateinit var mRevealLayout: View
private
lateinit var mFab: FloatingActionButton
private
var isRevealed =
false
@RequiresApi
(Build.VERSION_CODES.M)
override fun onCreate(savedInstanceState: Bundle?) {
super
.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
mRevealLayout = findViewById(R.id.revealLayout)
mFab = findViewById(R.id.fab)
mFab.backgroundTintList = ColorStateList.valueOf(
ResourcesCompat.getColor(
resources,
R.color.green_500,
null
)
)
mFab.setOnClickListener {
revealLayoutFun()
}
}
@RequiresApi
(Build.VERSION_CODES.M)
@SuppressLint
(
"ResourceAsColor"
)
private
fun revealLayoutFun() {
if
(!isRevealed) {
val x: Int = mRevealLayout.right /
2
val y: Int = mRevealLayout.bottom /
2
val startRadius =
0
val endRadius = hypot(
mRevealLayout.width.toDouble(),
mRevealLayout.height.toDouble()
).toInt()
mFab.backgroundTintList = ColorStateList.valueOf(
ResourcesCompat.getColor(
resources,
R.color.white,
null
)
)
mFab.setImageResource(R.drawable.ic_close)
val anim = ViewAnimationUtils.createCircularReveal(
mRevealLayout,
x,
y,
startRadius.toFloat(),
endRadius.toFloat()
)
mRevealLayout.visibility = View.VISIBLE
anim.start()
isRevealed =
true
}
else
{
val x: Int = mRevealLayout.right /
2
val y: Int = mRevealLayout.bottom /
2
val startRadius: Int = max(mRevealLayout.width, mRevealLayout.height)
val endRadius =
0
mFab.backgroundTintList = ColorStateList.valueOf(
ResourcesCompat.getColor(
resources,
R.color.green_500,
null
)
)
mFab.setImageResource(R.drawable.ic_add)
val anim = ViewAnimationUtils.createCircularReveal(
mRevealLayout,
x,
y,
startRadius.toFloat(),
endRadius.toFloat()
)
anim.addListener(object : Animator.AnimatorListener {
override fun onAnimationStart(animator: Animator) {}
override fun onAnimationEnd(animator: Animator) {
mRevealLayout.visibility = View.GONE
}
override fun onAnimationCancel(animator: Animator) {}
override fun onAnimationRepeat(animator: Animator) {}
})
anim.start()
isRevealed =
false
}
}
}