Last active
April 16, 2020 06:21
-
-
Save akueisara/d06671b93984db29c285f3c6f43d64e5 to your computer and use it in GitHub Desktop.
Android Property Animations Code Snippets (Udacity)
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
private fun ObjectAnimator.disableViewDuringAnimation(view: View) { | |
addListener(object : AnimatorListenerAdapter() { | |
override fun onAnimationStart(animation: Animator?) { | |
view.isEnabled = false | |
} | |
override fun onAnimationEnd(animation: Animator?) { | |
view.isEnabled = true | |
} | |
}) | |
} | |
private fun rotater() { | |
val animator = ObjectAnimator.ofFloat([Some Object such as an ImageView], View.ROTATION, -360f, 0f) | |
animator.duration = 1000 | |
animator.disableViewDuringAnimation([Some Button]) | |
animator.start() | |
} | |
private fun translater() { | |
val animator = ObjectAnimator.ofFloat([Some Object such as an ImageView], View.TRANSLATION_X, 200f) | |
animator.repeatCount = 1 | |
animator.repeatMode = ObjectAnimator.REVERSE | |
animator.disableViewDuringAnimation([Some Button]) | |
animator.start() | |
} | |
private fun scaler() { | |
val scaleX = PropertyValuesHolder.ofFloat(View.SCALE_X, 4f) | |
val scaleY = PropertyValuesHolder.ofFloat(View.SCALE_Y, 4f) | |
val animator = ObjectAnimator.ofPropertyValuesHolder([Some Object such as an ImageView], scaleX, scaleY) | |
animator.repeatCount = 1 | |
animator.repeatMode = ObjectAnimator.REVERSE | |
animator.disableViewDuringAnimation(scaleButton) | |
animator.start() | |
} | |
private fun fader() { | |
val animator = ObjectAnimator.ofFloat([Some Object such as an ImageView], View.ALPHA, 0f) | |
animator.repeatCount = 1 | |
animator.repeatMode = ObjectAnimator.REVERSE | |
animator.disableViewDuringAnimation(fade) | |
animator.start() | |
} | |
private fun colorizer() { | |
var animator = ObjectAnimator.ofArgb([Some Object such as an ImageView].parent, "backgroundColor", Color.BLACK, Color.RED) | |
animator.setDuration(500) | |
animator.repeatCount = 1 | |
animator.repeatMode = ObjectAnimator.REVERSE | |
animator.disableViewDuringAnimation(bgColor) | |
animator.start() | |
} | |
private fun shower() { | |
val container = star.parent as ViewGroup | |
val containerW = container.width | |
val containerH = container.height | |
var starW: Float = star.width.toFloat() | |
var starH: Float = star.height.toFloat() | |
val newStar = AppCompatImageView(this) | |
newStar.setImageResource(R.drawable.ic_star) | |
newStar.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, | |
FrameLayout.LayoutParams.WRAP_CONTENT) | |
container.addView(newStar) | |
newStar.scaleX = Math.random().toFloat() * 1.5f + .1f | |
newStar.scaleY = newStar.scaleX | |
starW *= newStar.scaleX | |
starH *= newStar.scaleY | |
newStar.translationX = Math.random().toFloat() * containerW - starW / 2 | |
val set = AnimatorSet() | |
set.playTogether(mover, rotator) | |
set.duration = (Math.random() * 1500 + 500).toLong() | |
set.addListener(object : AnimatorListenerAdapter() { | |
override fun onAnimationEnd(animation: Animator?) { | |
container.removeView(newStar) | |
} | |
}) | |
set.start() | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment