Skip to content

Instantly share code, notes, and snippets.

@bitvale
bitvale / ItemsInOutAnimation.kt
Created May 5, 2019
For Medium article "Animate Everything! (Android Animation Showcase)"
View ItemsInOutAnimation.kt
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
For Medium article "Animate Everything! (Android Animation Showcase)"
View AnimatedLayoutManager.kt
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
For Medium article "Animate Everything! (Android Animation Showcase)"
View ArcView.kt
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
For Medium article "Animate Everything! (Android Animation Showcase)"
View hamburger.shapeshifter
{
"version": 1,
"layers": {
"vectorLayer": {
"id": "2258",
"name": "vector",
"type": "vector",
"width": 24,
"height": 24,
"children": [
@bitvale
bitvale / LightAngle.kt
Created Jan 7, 2019
For Medium article "The power of Android Porter/Duff Mode"
View LightAngle.kt
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 Jan 7, 2019
For Medium article "The power of Android Porter/Duff Mode"
View TextPaint.kt
textPaint.apply {
// some other initialization
xfermode = PorterDuffXfermode(PorterDuff.Mode.DST_ATOP)
}
@bitvale
bitvale / LightPath.kt
Created Jan 7, 2019
For Medium article "The power of Android Porter/Duff Mode"
View LightPath.kt
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 Jan 7, 2019
For Medium article "The power of Android Porter/Duff Mode"
View LightAnimator.kt
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 Jan 7, 2019
For Medium article "The power of Android Porter/Duff Mode"
View onDraw.kt
override fun onDraw(canvas: Canvas ?) {
canvas?.withRotation(angle, lightPivotX, lightPivotY) {
drawPath(lightPath, lightPaint)
}
canvas?.drawBitmap(textBitmap, 0f, 0f, textPaint)
}
@bitvale
bitvale / onDraw.kt
Created Nov 22, 2018
For Medium article "Android Dynamic Custom View is Easy"
View onDraw.kt
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)
}
}