Skip to content

Instantly share code, notes, and snippets.

@bitvale
bitvale / ItemsInOutAnimation.kt
Created May 5, 2019 20:52
For Medium article "Animate Everything! (Android Animation Showcase)"
fun animateItemsOut() = animate(R.anim.item_animation_out)
fun animateItemsIn() = animate(R.anim.item_animation_in)
private fun animate(@AnimRes animationId: Int) {
var startOffset = 0L
for (i in childCount - 1 downTo 0) {
val set = AnimationUtils.loadAnimation(context, animationId)
set.startOffset = startOffset
getChildAt(i)?.startAnimation(set)
@bitvale
bitvale / AnimatedLayoutManager.kt
Created May 5, 2019 20:41
For Medium article "Animate Everything! (Android Animation Showcase)"
class AnimatedLayoutManager constructor(private val context: Context) : LinearLayoutManager(context) {
// other code
override fun scrollVerticallyBy(dy: Int, recycler: RecyclerView.Recycler, state: RecyclerView.State): Int {
if (childCount == 0) return 0
val legal = super.scrollVerticallyBy(dy, recycler, state)
calculateDy(dy)
updateViews()
return legal
@bitvale
bitvale / ArcView.kt
Created May 4, 2019 14:07
For Medium article "Animate Everything! (Android Animation Showcase)"
override fun onDraw(canvas: Canvas) {
canvas?.drawOval(defRect, backgroundPaint)
canvas?.drawOval(animatedRect, foregroundPaint)
}
fun startAnimation() {
// ...
val animateLeft = ObjectAnimator.ofFloat(animatedRect, "left", animatedRect.left, toRect.left)
val animateTop = ObjectAnimator.ofFloat(animatedRect, "top", animatedRect.top, toRect.top)
val animateRight = ObjectAnimator.ofFloat(animatedRect, "right", animatedRect.right, toRect.right)
@bitvale
bitvale / hamburger.shapeshifter
Created May 4, 2019 13:54
For Medium article "Animate Everything! (Android Animation Showcase)"
{
"version": 1,
"layers": {
"vectorLayer": {
"id": "2258",
"name": "vector",
"type": "vector",
"width": 24,
"height": 24,
"children": [
@bitvale
bitvale / LightAngle.kt
Created January 7, 2019 13:39
For Medium article "The power of Android Porter/Duff Mode"
private var angle = 0f
set(value) {
field = value
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
postInvalidateOnAnimation()
} else {
invalidate()
}
}
@bitvale
bitvale / TextPaint.kt
Created January 7, 2019 13:37
For Medium article "The power of Android Porter/Duff Mode"
textPaint.apply {
// some other initialization
xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)
}
@bitvale
bitvale / LightPath.kt
Created January 7, 2019 13:36
For Medium article "The power of Android Porter/Duff Mode"
val topY = textPaint.ascent() * -1 - textBounds.height()
lightPath.moveTo(lightPivotX - letterWidth / 2f, topY)
lightPath.moveTo(lightPivotX + letterWidth / 2f, topY)
lightPath.lineTo(lightPivotX + width / 2f, width.toFloat())
lightPath.lineTo(lightPivotX - width / 2f, width.toFloat())
lightPath.lineTo(lightPivotX - letterWidth / 2f, topY)
lightPath.close()
@bitvale
bitvale / LightAnimator.kt
Created January 7, 2019 08:27
For Medium article "The power of Android Porter/Duff Mode"
private var animator = ValueAnimator.ofFloat(0f, 1f).apply {
addUpdateListener {
val value = it.animatedValue as Float
angle = lerp(0f, FULL_CIRCLE, value)
}
interpolator = CustomSpringInterpolator(INTERPOLATOR_FACTOR)
repeatMode = ValueAnimator.RESTART
repeatCount = ValueAnimator.INFINITE
duration = ANIMATION_DURATION
}
@bitvale
bitvale / onDraw.kt
Created January 7, 2019 07:54
For Medium article "The power of Android Porter/Duff Mode"
override fun onDraw(canvas: Canvas ?) {
canvas?.withRotation(angle, lightPivotX, lightPivotY) {
drawPath(lightPath, lightPaint)
}
canvas?.drawBitmap(textBitmap, 0f, 0f, textPaint)
}
@bitvale
bitvale / onDraw.kt
Created November 22, 2018 20:17
For Medium article "Android Dynamic Custom View is Easy"
override fun onDraw(canvas: Canvas ?) {
// draw switcher (green rect)
canvas?.drawRoundRect(switcherRect, switcherCornerRadius, switcherCornerRadius, switcherPaint)
// draw icon (white rect)
canvas?.withTranslation(x = iconTranslateX) {
drawRoundRect(iconRect, switcherCornerRadius, switcherCornerRadius, iconPaint)
}
}