Created
October 26, 2019 03:46
-
-
Save wooooooak/f958b6c50e667fd54b985e2b0b9138e0 to your computer and use it in GitHub Desktop.
(Android) Rotate imageView 90 degree With adjusting imageView height
This file contains 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
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