Rant alert
I’ve been having this idea for a while, and when having dinner with my other programmer friend yesterday, I jokingly said that “Have you noticed there is a very popular programming paradigm that’s been adopted everywhere but no one is aware of it?” Yes, it is what I call Next Job Oriented Programming(NJOP).
Don’t get me wrong, I’m not against adopting new tech by any means, it’s good and healthy to keep our tech stack up to date.
In this post let’s take a deeper look at some of the more advanced uses of ObjectAnimator.
KeyFrame First example is the usage of KeyFrames. Here is the final result:
Let’s look at the code
val imageView = findViewById<ImageView>(R.id.imageView) /** In total we want to move 300dp. 0% of time passed, it has moved 0dp. 30% of time passed, it has moved 100dp. 60% of time passed, it has moved 120dp.
In this post let’s look at how to do this cool animation using ObjectAnimator.
This is built on top of previous post
class CameraView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val paint = Paint(Paint.ANTI_ALIAS_FLAG) private val imageSize = dp2px(200) private val leftPadding = dp2px(100) private val topPadding = dp2px(100) private val image = getAvatar(imageSize) private val camera = Camera() private var cameraRotationUpper = 0.
Let’s look at how to use camera in canvas transformation.
First this is what we want to implement:
The trick is to draw the upper and lower part separately. And we are using this “reverse drawing” technique.
Upper part override fun onDraw(canvas: Canvas) { super.onDraw(canvas) canvas.save() canvas.drawBitmap(image, leftPadding, topPadding, paint) canvas.restore() }
override fun onDraw(canvas: Canvas) { super.onDraw(canvas) canvas.save() canvas.translate(-(leftPadding + imageSize / 2), -(topPadding + imageSize / 2)) canvas.
In this post I want to go into details on canvas transformations, especially if we want to combine them.
Let’s take the simple example of translation + rotation. The end result is like this:
We can easily see that the image has been translated to (300, 200) and then rotated 45 degrees.
class TransformView @JvmOverloads constructor( context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0 ) : View(context, attrs, defStyleAttr) { private val paint = Paint(Paint.