Skip to content

Instantly share code, notes, and snippets.

@wooooooak
Created October 26, 2019 03:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wooooooak/f958b6c50e667fd54b985e2b0b9138e0 to your computer and use it in GitHub Desktop.
Save wooooooak/f958b6c50e667fd54b985e2b0b9138e0 to your computer and use it in GitHub Desktop.
(Android) Rotate imageView 90 degree With adjusting imageView height
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val deviceWidth = displayMetrics.widthPixels
val deviceHeight = displayMetrics.heightPixels
val imageView = image
image.load(
"https://www.91-img.com/pictures/133188-v4-oppo-f11-mobile-phone-large-1" +
".jpg"
)
button.setOnClickListener {
val currentRotation = imageView.rotation
val currentImageViewHeight = imageView.height
val heightGap = if (currentImageViewHeight > deviceWidth) {
deviceWidth - currentImageViewHeight
} else {
deviceHeight - currentImageViewHeight
}
if (currentRotation % 90 == 0.toFloat()) {
ValueAnimator.ofFloat(0f, 1f).apply {
duration = 500
addUpdateListener {
val animatedValue = it.animatedValue as Float
imageView.run {
layoutParams.height =
currentImageViewHeight + (heightGap * animatedValue)
.toInt()
rotation = currentRotation + 90 * animatedValue
requestLayout()
}
}
}.start()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment