Skip to content

Instantly share code, notes, and snippets.

🇿🇦
Working remotely

Rebecca Franks riggaroo

🇿🇦
Working remotely
Block or report user

Report or block riggaroo

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@riggaroo
riggaroo / SpringAnimationVelocity.kt
Created Mar 28, 2019
Example setting start velocity of a spring animation.
View SpringAnimationVelocity.kt
SpringAnimation(this, floatPropertyAnimY, point.y).apply {
setStartVelocity(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5000f, resources.displayMetrics))
start()
}
@riggaroo
riggaroo / SpringAnimation.kt
Created Mar 28, 2019
Example using SpringAnimation to animate a custom view using FloatPropertyCompat properties.
View SpringAnimation.kt
private fun animateToPoint(point: Point) {
SpringAnimation(this, floatPropertyAnimX, point.x).apply {
spring.stiffness = SpringForce.STIFFNESS_MEDIUM
spring.dampingRatio = SpringForce.DAMPING_RATIO_MEDIUM_BOUNCY
start()
}
SpringAnimation(this, floatPropertyAnimY, point.y).apply {
spring.stiffness = SpringForce.STIFFNESS_MEDIUM
@riggaroo
riggaroo / FloatPropertyCompatExamples.kt
Created Mar 28, 2019
Example of using FloatPropertyCompat for SpringAnimations.
View FloatPropertyCompatExamples.kt
private val floatPropertyAnimX = object : FloatPropertyCompat<ColorDropperView>(PROPERTY_X) {
override fun setValue(dropper: ColorDropperView?, value: Float) {
dropper?.setDropperX(value)
}
override fun getValue(dropper: ColorDropperView?): Float {
return dropper?.getDropperX() ?: 0f
}
}
@riggaroo
riggaroo / ValueAnimator.kt
Created Mar 28, 2019
Example showing how to use PropertyValuesHolder to animate a custom views properties.
View ValueAnimator.kt
private fun animateToPoint(point: Point) {
val propertyX = PropertyValuesHolder.ofFloat(ColorDropperView.PROPERTY_X, dropperPoint.x, point.x)
val propertyY = PropertyValuesHolder.ofFloat(ColorDropperView.PROPERTY_Y, dropperPoint.y, point.y)
val animator = ValueAnimator()
animator.setValues(propertyX, propertyY)
animator.interpolator = OvershootInterpolator()
animator.duration = 100
animator.addUpdateListener { animation ->
val animatedX = animation.getAnimatedValue(ColorDropperView.PROPERTY_X) as Float
@riggaroo
riggaroo / CustomView-Snippet6.kt
Created Mar 21, 2019
Using extension function to draw to canvas.
View CustomView-Snippet6.kt
canvas.withTranslate(200f, 300f) {
drawCircle(...)
}
@riggaroo
riggaroo / CustomView-Snippet5.kt
Created Mar 21, 2019
Valid with function inside parentheses
View CustomView-Snippet5.kt
canvas.withTranslate(200f, 300f, {
drawCircle(...)
})
@riggaroo
riggaroo / CanvasExt.kt
Created Mar 21, 2019
Sample of Canvas extension function from Android KTX
View CanvasExt.kt
/**
* Wrap the specified [block] in calls to [Canvas.save]/[Canvas.translate]
* and [Canvas.restoreToCount].
*/
inline fun Canvas.withTranslation(
x: Float = 0.0f,
y: Float = 0.0f,
block: Canvas.() -> Unit
) {
val checkpoint = save()
@riggaroo
riggaroo / CustomView.kt
Last active Mar 21, 2019
Custom Canvas Drawing with KTX
View CustomView.kt
canvas.withTranslation(200f, 300f) {
drawCircle(150f, 150f, RADIUS, circlePaint)
withRotation(45f) {
drawRect(rect, rectPaint)
}
}
@riggaroo
riggaroo / CustomView-Snippet2.kt
Last active Mar 21, 2019
Custom Drawing on Canvas without KTX
View CustomView-Snippet2.kt
val translateCheckpoint = canvas.save()
canvas.translate(200f, 300f)
canvas.drawCircle(150f, 150f, RADIUS, circlePaint) // drawn on the translated canvas
val rotateCheckpoint = canvas.save()
canvas.rotate(45f)
canvas.drawRect(rect, rectPaint) // drawn on the translated and rotated canvas
canvas.restoreToCount(rotateCheckpoint)
canvas.restoreToCount(translateCheckpoint)
@riggaroo
riggaroo / CustomView-Snippet1.kt
Last active Mar 21, 2019
Drawing to Canvas without KTX
View CustomView-Snippet1.kt
canvas.save()
canvas.translate(200f, 300f)
canvas.drawCircle(...) // drawn on the translated canvas
canvas.restore()
You can’t perform that action at this time.