Created
January 13, 2020 17:03
-
-
Save huuphuoc1396/3a3a73cce8066d79db1fba3f6f260da8 to your computer and use it in GitHub Desktop.
Transform and scale animation on a View
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.android.moveandscale | |
import android.animation.ObjectAnimator | |
import android.graphics.Point | |
import android.os.Bundle | |
import android.view.ViewGroup | |
import android.widget.ImageView | |
import android.widget.RelativeLayout | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.core.content.ContextCompat | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
val mainLayout = RelativeLayout(this).apply { | |
// MATCH_PARENT equal to -1 | |
layoutParams = ViewGroup.LayoutParams(-1, -1) | |
} | |
setContentView(mainLayout) | |
val size = Point() | |
windowManager.defaultDisplay.apply { | |
getSize(size) | |
} | |
val imageView = ImageView(this).apply { | |
setImageDrawable(ContextCompat.getDrawable(this@MainActivity, R.drawable.image)) | |
} | |
val layoutParams = ViewGroup.LayoutParams(720, 405) | |
mainLayout.addView(imageView, layoutParams) | |
imageView.setOnClickListener { | |
ObjectAnimator.ofFloat( | |
imageView, | |
"translationX", | |
imageView.x, | |
size.x.toFloat() / 2 - imageView.width / 2 | |
).setDuration(600).start() | |
ObjectAnimator.ofFloat( | |
imageView, | |
"translationY", | |
imageView.y, | |
size.y.toFloat() / 2 - imageView.height | |
).setDuration(600).start() | |
ObjectAnimator.ofFloat( | |
imageView, | |
"scaleX", | |
1f, | |
size.x.toFloat() / imageView.width | |
).setDuration(600).start() | |
val height = size.x.toFloat() * imageView.height.toFloat() / imageView.width | |
ObjectAnimator.ofFloat( | |
imageView, | |
"scaleY", | |
1f, | |
height / imageView.height | |
).setDuration(600).start() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment