summaryrefslogtreecommitdiff
path: root/platform/android/MapboxGLAndroidSDK/src/main/java/com/mapbox/mapboxsdk/utils/AnimatorUtils.java
blob: 4e0125eb8faa8ae42f35a7bb51ad770bbe12c18f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package com.mapbox.mapboxsdk.utils;

import android.animation.Animator;
import android.animation.AnimatorInflater;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import androidx.annotation.AnimatorRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.interpolator.view.animation.FastOutSlowInInterpolator;
import android.view.View;

/**
 * Animator utility class.
 */
public class AnimatorUtils {

  /**
   * Animate a view from an animator resource.
   *
   * @param view        the view to be animated
   * @param animatorRes the animator resource to be loaded
   * @param listener    the animator end listener
   */
  public static void animate(@NonNull final View view, @AnimatorRes int animatorRes,
                             @Nullable OnAnimationEndListener listener) {
    animate(view, animatorRes, -1, listener);
  }

  /**
   * Animate a view from an animator resource.
   *
   * @param view        the view to be animated
   * @param animatorRes the animator resource to be loaded
   * @param duration    the duration of the animator
   * @param listener    the animator end listener
   */
  public static void animate(@Nullable final View view, @AnimatorRes int animatorRes, int duration,
                             @Nullable final OnAnimationEndListener listener) {
    if (view == null) {
      return;
    }

    view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    Animator animator = AnimatorInflater.loadAnimator(view.getContext(), animatorRes);
    if (duration != -1) {
      animator.setDuration(duration);
    }

    animator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        view.setLayerType(View.LAYER_TYPE_NONE, null);
        if (listener != null) {
          listener.onAnimationEnd();
        }
      }
    });
    animator.setTarget(view);
    animator.start();
  }

  /**
   * Animate a view from an animator resource.
   *
   * @param view        the view to be animated
   * @param animatorRes the animator resource to be loaded
   */
  public static void animate(@NonNull final View view, @AnimatorRes int animatorRes) {
    animate(view, animatorRes, -1);
  }

  /**
   * Animate a view from an animator resource.
   *
   * @param view        the view to be animated
   * @param animatorRes the animator resource to be loaded
   * @param duration    the duration of the animator
   */
  public static void animate(@NonNull final View view, @AnimatorRes int animatorRes, int duration) {
    animate(view, animatorRes, duration, null);
  }

  /**
   * Animate a view rotation property to a value.
   *
   * @param view     the view to be rotated
   * @param rotation the value to animate to
   */
  public static void rotate(@NonNull final View view, float rotation) {
    view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(view, View.ROTATION, view.getRotation(), rotation);
    rotateAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        view.setLayerType(View.LAYER_TYPE_NONE, null);
      }
    });
    rotateAnimator.start();
  }

  /**
   * Animate a view rotation property by a value.
   *
   * @param view       the view to be rotated
   * @param rotationBy the value to animate by
   */
  public static void rotateBy(@NonNull final View view, float rotationBy) {
    view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    view.animate().rotationBy(rotationBy).setInterpolator(new FastOutSlowInInterpolator()).setListener(
      new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator animation) {
          super.onAnimationEnd(animation);
          view.setLayerType(View.LAYER_TYPE_NONE, null);
        }
      });
  }

  /**
   * Animate a view alpha property to a value.
   *
   * @param convertView the view to be animated
   * @param alpha       the value to animate to
   * @param listener    the animator end listener
   */
  public static void alpha(@NonNull final View convertView, float alpha,
                           @Nullable final OnAnimationEndListener listener) {
    convertView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
    ObjectAnimator rotateAnimator = ObjectAnimator.ofFloat(convertView, View.ALPHA, convertView.getAlpha(), alpha);
    rotateAnimator.addListener(new AnimatorListenerAdapter() {
      @Override
      public void onAnimationStart(Animator animation) {
        super.onAnimationStart(animation);
        convertView.setVisibility(View.VISIBLE);
      }

      @Override
      public void onAnimationEnd(Animator animation) {
        super.onAnimationEnd(animation);
        convertView.setLayerType(View.LAYER_TYPE_NONE, null);
        if (listener != null) {
          listener.onAnimationEnd();
        }
      }
    });
    rotateAnimator.start();
  }

  /**
   * Animate a view alpha property to a value.
   *
   * @param convertView the view to be animated
   * @param alpha       the value to animate to
   */
  public static void alpha(@NonNull final View convertView, float alpha) {
    alpha(convertView, alpha, null);
  }

  /**
   * An interface definition that is invoked when an animation ends.
   */
  public interface OnAnimationEndListener {
    void onAnimationEnd();
  }
}