Skip to content

Commit

Permalink
Split CircularImageButton into two classes
Browse files Browse the repository at this point in the history
In preparation for one of the subsequent commits.

Don't reuse animations, as these are apparently mutable. Also, reduce hide
animation duration by a factor of 2.
  • Loading branch information
oakkitten committed Nov 23, 2024
1 parent 5acaf7c commit 97b5a9e
Showing 1 changed file with 30 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,48 @@ import com.ubergeek42.WeechatAndroid.upload.applicationContext
import com.ubergeek42.WeechatAndroid.upload.f


class CircularImageButton @JvmOverloads constructor(
open class FloatingImageButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : AppCompatImageButton(context, attrs) {
init {
background = null
outlineProvider = pillOutlineProvider
clipToOutline = true
}

fun show() {
fun show(animate: Boolean = true) {
if (visibility != VISIBLE) {
visibility = VISIBLE
startAnimation(showAnimation)
if (animate) startAnimation(makeShowAnimation())
}
}

fun hide() {
fun hide(animate: Boolean = true) {
if (visibility != INVISIBLE) {
visibility = INVISIBLE
startAnimation(hideAnimation)
if (animate) startAnimation(makeHideAnimation())
}
}

private fun makeShowAnimation() = ScaleAnimation(
0f, 1f, 0f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
.apply { duration = shortAnimTime }

private fun makeHideAnimation() = ScaleAnimation(
1f, .5f, 1f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
.apply { duration = shortAnimTime / 2 }
}


class CircularImageButton @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
) : FloatingImageButton(context, attrs) {
init {
background = null
outlineProvider = pillOutlineProvider
clipToOutline = true
}

override fun setBackgroundColor(color: Int) {
if (backgroundPaint.color != color) {
backgroundPaint.color = color
Expand All @@ -51,18 +69,6 @@ class CircularImageButton @JvmOverloads constructor(
canvas.drawPaint(backgroundPaint)
super.onDraw(canvas)
}

private val showAnimation = ScaleAnimation(
0f, 1f, 0f, 1f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
.apply { duration = animationDuration }

private val hideAnimation = ScaleAnimation(
1f, .5f, 1f, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f)
.apply { duration = animationDuration }
}


Expand All @@ -73,5 +79,5 @@ private val pillOutlineProvider = object : ViewOutlineProvider() {
}


private val animationDuration = applicationContext
private val shortAnimTime = applicationContext
.resources.getInteger(android.R.integer.config_shortAnimTime).toLong()

0 comments on commit 97b5a9e

Please sign in to comment.