Skip to content

Instantly share code, notes, and snippets.

@alexjlockwood
Last active July 23, 2019 10:37
Show Gist options
  • Save alexjlockwood/ba60bd778416dec89befbb9305b299ca to your computer and use it in GitHub Desktop.
Save alexjlockwood/ba60bd778416dec89befbb9305b299ca to your computer and use it in GitHub Desktop.
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.util.AttributeSet
import android.view.View
import androidx.annotation.ColorInt
import java.lang.Math.pow
import kotlin.math.cos
import kotlin.math.sqrt
private const val N = 12
private const val W = 44f
private const val H = W * 3
private const val PERIOD = 5000
private const val PI = Math.PI.toFloat()
private const val TWO_PI = 2 * PI
private val LIGHT = Color.parseColor("#EAEAEA")
private val DARK = Color.parseColor("#222222")
class CrossesSquaresView(context: Context, attrs: AttributeSet?) : View(context, attrs) {
private val paint = Paint(Paint.ANTI_ALIAS_FLAG)
private val t: Float
get() = (System.currentTimeMillis() % PERIOD) / PERIOD.toFloat()
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val t = t
canvas.drawBackground(
if (t * 3 < 1) {
LIGHT
} else if (t * 3 < 2) {
DARK
} else {
val t = t * 3f - 2f
if (t > .5f) DARK else LIGHT
}
)
canvas.save()
canvas.translate(width / 2f, height / 2f)
when {
t * 3 < 1 -> {
val t = t * 3f
for (i in -N..N) {
for (j in -N..N) {
val tt = ease(t, 3f)
canvas.save()
canvas.translate(i * H - j * W * tt, j * H + i * W * tt)
canvas.rotate(90 * tt)
canvas.drawCross(DARK)
canvas.restore()
}
}
}
t * 3 < 2 -> {
val t = t * 3f - 1f
canvas.save()
canvas.scale(-1f, 1f)
for (i in -N..N) {
for (j in -N..N) {
val tt = 1f - ease(t, 3f)
canvas.save()
canvas.translate((i + .5f) * H + (j + .5f) * W * tt, (j + .5f) * H - (i + .5f) * W * tt)
canvas.rotate(-90f * tt)
canvas.drawCross(LIGHT)
canvas.restore()
}
}
canvas.restore()
}
else -> {
val t = t * 3f - 2f
paint.color = if (t > .5f) LIGHT else DARK
for (i in -N..N) {
for (j in -N..N) {
canvas.save()
val tt = ease(t, 3f)
canvas.translate(
(i + .5f * if (t > .5f) 1f else 0f) * H,
(j + .5f * if (t > .5f) 1f else 0f) * H
)
canvas.rotate(90 * tt)
val s = map(cos(TWO_PI * tt), 1f, -1f, 1f, sqrt(2f) * 3f / 4f)
canvas.scale(s, s)
canvas.drawRect(-W, -W, W, W, paint)
canvas.restore()
}
}
}
}
canvas.restore()
postInvalidateOnAnimation()
}
private fun Canvas.drawBackground(@ColorInt color: Int) {
paint.color = color
drawRect(0f, 0f, width.toFloat(), height.toFloat(), paint)
}
private fun Canvas.drawCross(@ColorInt color: Int) {
paint.color = color
drawRect(-W / 2f, -H / 2f, W / 2f, H / 2f, paint)
drawRect(-H / 2f, -W / 2f, H / 2f, W / 2f, paint)
}
}
private fun map(value: Float, start1: Float, stop1: Float, start2: Float, stop2: Float): Float {
return start2 + (stop2 - start2) * ((value - start1) / (stop1 - start1))
}
private fun ease(p: Float, g: Float): Float {
return if (p < 0.5f) {
0.5f * pow(2.0 * p, g.toDouble()).toFloat()
} else {
1f - 0.5f * pow(2.0 * (1f - p), g.toDouble()).toFloat()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment