Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Stop running from animations

Ivan Maric
November 18, 2017

Stop running from animations

Creating eye candy animations takes time and effort, but the end result is worth it. To make your life a bit easier, in this talk Ivan will share with you 10 tips he learned while working on all sorts of animations. Those tips will help you break your fear of animations and after this talk, you will have no more excuses.

Ivan Maric

November 18, 2017
Tweet

More Decks by Ivan Maric

Other Decks in Programming

Transcript

  1. ObjectAnimator animatorX = ObjectAnimator .ofFloat(animatedView, "translationX", 0, dx).setDuration(ANIM_DURATION); ObjectAnimator animatorY

    = ObjectAnimator .ofFloat(animatedView, "translationY", 0, dy).setDuration(ANIM_DURATION); AnimatorSet animator = new AnimatorSet(); animator.playTogether(animatorX, animatorY); animator.start(); OBJECT ANIMATOR
  2. ObjectAnimator animatorX = ObjectAnimator .ofFloat(animatedView, "translationX", 0, dx).setDuration(ANIM_DURATION); ObjectAnimator animatorY

    = ObjectAnimator .ofFloat(animatedView, "translationY", 0, dy).setDuration(ANIM_DURATION); AnimatorSet animator = new AnimatorSet(); animator.playTogether(animatorX, animatorY); animator.start(); OBJECT ANIMATOR
  3. ObjectAnimator animatorX = ObjectAnimator .ofFloat(animatedView, "translationX", 0, dx).setDuration(ANIM_DURATION); ObjectAnimator animatorY

    = ObjectAnimator .ofFloat(animatedView, "translationY", 0, dy).setDuration(ANIM_DURATION); AnimatorSet animator = new AnimatorSet(); animator.playTogether(animatorX, animatorY); animator.start(); OBJECT ANIMATOR float getTranslationX() setTranslationX(float)
  4. Animation animation = new TranslateAnimation( Animation.RELATIVE_TO_PARENT, 0f, Animation.RELATIVE_TO_PARENT, 0.9f, Animation.RELATIVE_TO_PARENT,

    0f, Animation.RELATIVE_TO_PARENT, 0.9f); animation.setDuration(ANIM_DURATION); animation.setFillAfter(true); animatedView.startAnimation(animation); VIEW ANIMATION
  5. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/logo" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/infinum_logo" android:layout_above="@+id/firstText"/>

    <TextView android:id="@id/firstText" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/first_text"/> <TextView android:id="@+id/secondText" android:layout_width="match_parent" android:layout_height="wrap_content" android:visibility="invisible" android:layout_centerInParent="true" android:text="@string/second_text"/> </RelativeLayout>
  6. Move & fade out text TranslateAnimation moveOutText = new TranslateAnimation(

    Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, -0.8f); AlphaAnimation fadeOut = new AlphaAnimation(1f, 0f); AnimationSet firstTextAnimation = new AnimationSet(true); firstTextAnimation.addAnimation(moveText); firstTextAnimation.addAnimation(fadeOut); firstTextAnimation.setDuration(600);
  7. Move & fade in text TranslateAnimation moveInText = new TranslateAnimation(

    Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f); AlphaAnimation fadeIn = new AlphaAnimation(0f, 1f); AnimationSet secondTextAnimation = new AnimationSet(true); secondTextAnimation.addAnimation(moveInText); secondTextAnimation.addAnimation(fadeIn); secondTextAnimation.setDuration(600); secondTextAnimation.setFillAfter(true);
  8. Scale & fade out all ScaleAnimation logoScaleAnimation = new ScaleAnimation(

    1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 1f); ScaleAnimation textScaleAnimation = new ScaleAnimation( 1f, 2f, 1f, 2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0f); AnimationSet logoAnim = new AnimationSet(true); logoAnim.addAnimation(logoScaleAnimation); logoAnim.addAnimation(fadeOut); logoAnim.setDuration(800); logoAnim.setStartOffset(800); logoAnim.setFillAfter(true); AnimationSet textAnim = new AnimationSet(true); textAnim.addAnimation(textScaleAnimation); textAnim.addAnimation(fadeOut); textAnim.setDuration(800); textAnim.setStartOffset(800); textAnim.setFillAfter(true);
  9. firstTextAnimation.setAnimationListener( new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) {

    firstText.setVisibility(View.INVISIBLE); secondText.setVisibility(View.VISIBLE); secondText.startAnimation(secondTextAnimation); } }); secondTextAnimation.setAnimationListener( new Animation.AnimationListener() { @Override public void onAnimationEnd(Animation animation) { logo.startAnimation(logoAnim); secondText.startAnimation(textAnim); } }); firstText.startAnimation(firstTextAnimation);
  10. firstText.animate().alpha(0f) .translationY(firstText.getHeight()) .setDuration(600); secondText.setTranslationY(secondText.getHeight() / 2); secondText.setAlpha(0); secondText.animate().alpha(1f) .translationY(0) .setDuration(600);

    logo.setPivotX(logo.getWidth() / 2f); logo.setPivotY(logo.getHeight()); secondText.setPivotX(secondText.getWidth() / 2f); secondText.setPivotY(0); logo.animate().alpha(0) .scaleX(2).scaleY(2) .setDuration(800).setStartDelay(800); secondText.animate().alpha(0) .scaleX(2).scaleY(2) .setDuration(800).setStartDelay(800);
  11. Mapped value 0 0,2 0,4 0,6 0,8 1 1,2 Elapsed

    time 0 0,2 0,4 0,6 0,8 1 Linear Accelerate Overshoot
  12. HOW IT WORKS Determine elapsed time Calculate positions, sizes, colors….

    Draw objects Repeat → Invalidate view with delay
  13. public class AnimatedDrawable extends Drawable public AnimatedDrawable() { // Init

    all Paint and other resources used in draw method } @Override public void draw(Canvas canvas) { } }
  14. public class AnimatedDrawable extends Drawable implements Animatable { public AnimatedDrawable()

    { // Init all Paint and other resources used in draw method } @Override public void draw(Canvas canvas) { draw(canvas, (SystemClock.uptimeMillis() - startTime) / DURATION); } @Override public void start() { startTime = SystemClock.uptimeMillis(); } }
  15. public class AnimatedDrawable extends Drawable implements Animatable, Runnable, Drawable.Callback {

    public AnimatedDrawable() { // Init all Paint and other resources used in draw method } @Override public void draw(Canvas canvas) { draw(canvas, (SystemClock.uptimeMillis() - startTime) / DURATION); } @Override public void start() { startTime = SystemClock.uptimeMillis(); } @Override public void run() { invalidateSelf(); scheduleSelf(this, SystemClock.uptimeMillis() + FRAME_DELAY); } @Override public void invalidateDrawable(Drawable who) { who.invalidateSelf(); } }
  16. WHAT CAN YOU DO Don’t slack on math class Learn

    and research Search practical use
  17. WHAT CAN YOU DO Probably too late for that Learn

    and research Search practical use
  18. DRAWABLE ANIMATION AnimationDrawable drawable = (AnimationDrawable) ContextCompat .getDrawable(this, R.drawable.drawable_animation); image.setImageDrawable(drawable);

    drawable.start(); <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"> <item android:drawable="@drawable/anim_000" android:duration="30"/> <item android:drawable="@drawable/anim_001" android:duration="30"/> <item android:drawable="@drawable/anim_002" android:duration="30"/> <item android:drawable="@drawable/anim_003" android:duration="30"/> <!--...-->
  19. Thank you! [email protected] @MARIC_IV Visit infinum.co or find us on

    social networks: infinum.co infinumco infinumco infinum