|
| 1 | +package com.clock.study.activity; |
| 2 | + |
| 3 | +import android.animation.AnimatorInflater; |
| 4 | +import android.animation.AnimatorSet; |
| 5 | +import android.animation.Keyframe; |
| 6 | +import android.animation.ObjectAnimator; |
| 7 | +import android.animation.PropertyValuesHolder; |
| 8 | +import android.animation.ValueAnimator; |
| 9 | +import android.graphics.Color; |
| 10 | +import android.os.Bundle; |
| 11 | +import android.support.v4.view.ViewCompat; |
| 12 | +import android.support.v7.app.AppCompatActivity; |
| 13 | +import android.util.Log; |
| 14 | +import android.view.View; |
| 15 | +import android.view.animation.BounceInterpolator; |
| 16 | + |
| 17 | +import com.clock.study.R; |
| 18 | + |
| 19 | +/** |
| 20 | + * About Android Animator |
| 21 | + * |
| 22 | + * @author Clock |
| 23 | + * @since 2016-07-21 |
| 24 | + */ |
| 25 | +public class AnimatorActivity extends AppCompatActivity implements View.OnClickListener { |
| 26 | + |
| 27 | + private static final String TAG = AnimatorActivity.class.getSimpleName(); |
| 28 | + |
| 29 | + private View mTarget; |
| 30 | + |
| 31 | + @Override |
| 32 | + protected void onCreate(Bundle savedInstanceState) { |
| 33 | + super.onCreate(savedInstanceState); |
| 34 | + setContentView(R.layout.activity_animator); |
| 35 | + |
| 36 | + mTarget = findViewById(R.id.anim_target); |
| 37 | + |
| 38 | + findViewById(R.id.btn_value_anim).setOnClickListener(this); |
| 39 | + findViewById(R.id.btn_object_anim_alpha).setOnClickListener(this); |
| 40 | + findViewById(R.id.btn_object_anim_rotation).setOnClickListener(this); |
| 41 | + findViewById(R.id.btn_object_anim_set).setOnClickListener(this); |
| 42 | + findViewById(R.id.btn_object_anim_xml).setOnClickListener(this); |
| 43 | + findViewById(R.id.btn_simple_value_animator).setOnClickListener(this); |
| 44 | + findViewById(R.id.btn_value_animator_argb).setOnClickListener(this); |
| 45 | + findViewById(R.id.btn_bounce_interpolator).setOnClickListener(this); |
| 46 | + findViewById(R.id.btn_simple_key_frame).setOnClickListener(this); |
| 47 | + findViewById(R.id.btn_shake_key_frame).setOnClickListener(this); |
| 48 | + |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onClick(View v) { |
| 53 | + int viewId = v.getId(); |
| 54 | + if (viewId == R.id.btn_value_anim) { |
| 55 | + ValueAnimator valueAnimator = ValueAnimator.ofInt(1, 100); |
| 56 | + valueAnimator.setDuration(3000); |
| 57 | + valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 58 | + @Override |
| 59 | + public void onAnimationUpdate(ValueAnimator animation) { |
| 60 | + int currentValue = (int) animation.getAnimatedValue(); |
| 61 | + Log.i(TAG, "currentValue: " + currentValue); |
| 62 | + } |
| 63 | + }); |
| 64 | + valueAnimator.start(); |
| 65 | + |
| 66 | + } else if (viewId == R.id.btn_object_anim_alpha) { |
| 67 | + ObjectAnimator alphaObjectAnimator = ObjectAnimator.ofFloat(mTarget, "alpha", 1f, 0f, 1f); |
| 68 | + alphaObjectAnimator.setDuration(3000); |
| 69 | + alphaObjectAnimator.start(); |
| 70 | + |
| 71 | + } else if (viewId == R.id.btn_object_anim_rotation) { |
| 72 | + ObjectAnimator rotationObjectAnimator = ObjectAnimator.ofFloat(mTarget, "rotation", 0f, 360f); |
| 73 | + rotationObjectAnimator.setDuration(3000); |
| 74 | + rotationObjectAnimator.start(); |
| 75 | + |
| 76 | + } else if (viewId == R.id.btn_object_anim_set) { |
| 77 | + AnimatorSet animatorSet = new AnimatorSet(); |
| 78 | + ObjectAnimator alphaObjectAnimator = ObjectAnimator.ofFloat(mTarget, "alpha", 1f, 0f, 1f); |
| 79 | + ObjectAnimator rotationObjectAnimator = ObjectAnimator.ofFloat(mTarget, "rotation", 0f, 360f); |
| 80 | + animatorSet.play(alphaObjectAnimator).with(rotationObjectAnimator); |
| 81 | + //animatorSet.play(alphaObjectAnimator).after(rotationObjectAnimator); |
| 82 | + //animatorSet.playTogether(alphaObjectAnimator, rotationObjectAnimator); |
| 83 | + animatorSet.setDuration(3000); |
| 84 | + animatorSet.start(); |
| 85 | + |
| 86 | + } else if (viewId == R.id.btn_object_anim_xml) { |
| 87 | + AnimatorSet animatorSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.simple_animator); |
| 88 | + animatorSet.setTarget(mTarget); |
| 89 | + animatorSet.start(); |
| 90 | + |
| 91 | + } else if (viewId == R.id.btn_simple_value_animator) { |
| 92 | + displayColorAnimation(mTarget, "#0000ff", "#ff0000"); |
| 93 | + |
| 94 | + } else if (viewId == R.id.btn_value_animator_argb) { |
| 95 | + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) { |
| 96 | + int startColor = 0x00000000; |
| 97 | + int centerColor = 0xff00ff89; |
| 98 | + int endColor = 0x00000000; |
| 99 | + ValueAnimator valueAnimator = ValueAnimator.ofArgb(startColor, centerColor, endColor); |
| 100 | + valueAnimator.setDuration(6000); |
| 101 | + valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 102 | + @Override |
| 103 | + public void onAnimationUpdate(ValueAnimator animation) { |
| 104 | + int color = (int) animation.getAnimatedValue(); |
| 105 | + mTarget.setBackgroundColor(color); |
| 106 | + } |
| 107 | + }); |
| 108 | + valueAnimator.start(); |
| 109 | + } |
| 110 | + |
| 111 | + } else if (viewId == R.id.btn_bounce_interpolator) { |
| 112 | + ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(mTarget, "translationY", 0f, 1500f); |
| 113 | + objectAnimator.setInterpolator(new BounceInterpolator()); |
| 114 | + objectAnimator.setDuration(4000); |
| 115 | + objectAnimator.start(); |
| 116 | + |
| 117 | + } else if (viewId == R.id.btn_simple_key_frame) { |
| 118 | + Keyframe kf0 = Keyframe.ofFloat(0f, 0f); |
| 119 | + Keyframe kf1 = Keyframe.ofFloat(.5f, 360f); |
| 120 | + Keyframe kf2 = Keyframe.ofFloat(1f, 0f); |
| 121 | + PropertyValuesHolder pvhRotation = PropertyValuesHolder.ofKeyframe("rotation", kf0, kf1, kf2); |
| 122 | + ObjectAnimator rotationAnim = ObjectAnimator.ofPropertyValuesHolder(mTarget, pvhRotation); |
| 123 | + rotationAnim.setDuration(5000); |
| 124 | + rotationAnim.start(); |
| 125 | + |
| 126 | + } else if (viewId == R.id.btn_shake_key_frame) { |
| 127 | + displayShakeAnimator(mTarget, 1f); |
| 128 | + |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * 创建一个旋转动画 |
| 134 | + * |
| 135 | + * @param target |
| 136 | + * @param shakeFactor |
| 137 | + * @return |
| 138 | + */ |
| 139 | + private void displayShakeAnimator(View target, float shakeFactor) { |
| 140 | + Keyframe scaleXkf0 = Keyframe.ofFloat(0f, 1f); |
| 141 | + Keyframe scaleXkf1 = Keyframe.ofFloat(0.1f, 0.9f); |
| 142 | + Keyframe scaleXkf2 = Keyframe.ofFloat(0.2f, 0.9f); |
| 143 | + Keyframe scaleXkf3 = Keyframe.ofFloat(0.3f, 0.9f); |
| 144 | + Keyframe scaleXkf4 = Keyframe.ofFloat(0.4f, 1.1f); |
| 145 | + Keyframe scaleXkf5 = Keyframe.ofFloat(0.5f, 1.1f); |
| 146 | + Keyframe scaleXkf6 = Keyframe.ofFloat(0.6f, 1.1f); |
| 147 | + Keyframe scaleXkf7 = Keyframe.ofFloat(0.7f, 1.1f); |
| 148 | + Keyframe scaleXkf8 = Keyframe.ofFloat(0.8f, 1.1f); |
| 149 | + Keyframe scaleXkf9 = Keyframe.ofFloat(0.9f, 1.1f); |
| 150 | + Keyframe scaleXkf10 = Keyframe.ofFloat(1f, 1f); |
| 151 | + |
| 152 | + PropertyValuesHolder scaleXHolder = PropertyValuesHolder.ofKeyframe("scaleX", scaleXkf0, scaleXkf1, scaleXkf2, scaleXkf3, scaleXkf4, |
| 153 | + scaleXkf5, scaleXkf6, scaleXkf7, scaleXkf8, scaleXkf9, scaleXkf10); |
| 154 | + |
| 155 | + Keyframe scaleYkf0 = Keyframe.ofFloat(0f, 1f); |
| 156 | + Keyframe scaleYkf1 = Keyframe.ofFloat(0.1f, 0.9f); |
| 157 | + Keyframe scaleYkf2 = Keyframe.ofFloat(0.2f, 0.9f); |
| 158 | + Keyframe scaleYkf3 = Keyframe.ofFloat(0.3f, 0.9f); |
| 159 | + Keyframe scaleYkf4 = Keyframe.ofFloat(0.4f, 1.1f); |
| 160 | + Keyframe scaleYkf5 = Keyframe.ofFloat(0.5f, 1.1f); |
| 161 | + Keyframe scaleYkf6 = Keyframe.ofFloat(0.6f, 1.1f); |
| 162 | + Keyframe scaleYkf7 = Keyframe.ofFloat(0.7f, 1.1f); |
| 163 | + Keyframe scaleYkf8 = Keyframe.ofFloat(0.8f, 1.1f); |
| 164 | + Keyframe scaleYkf9 = Keyframe.ofFloat(0.9f, 1.1f); |
| 165 | + Keyframe scaleYkf10 = Keyframe.ofFloat(1f, 1f); |
| 166 | + PropertyValuesHolder scaleYHolder = PropertyValuesHolder.ofKeyframe("scaleY", scaleYkf0, scaleYkf1, scaleYkf2, scaleYkf3, scaleYkf4, |
| 167 | + scaleYkf5, scaleYkf6, scaleYkf7, scaleYkf8, scaleYkf9, scaleYkf10); |
| 168 | + |
| 169 | + |
| 170 | + PropertyValuesHolder rotationHolder = PropertyValuesHolder.ofKeyframe("rotation", |
| 171 | + Keyframe.ofFloat(0f, 0), |
| 172 | + Keyframe.ofFloat(0.1f, -3 * shakeFactor), |
| 173 | + Keyframe.ofFloat(0.2f, -3 * shakeFactor), |
| 174 | + Keyframe.ofFloat(0.3f, 3 * shakeFactor), |
| 175 | + Keyframe.ofFloat(0.4f, -3 * shakeFactor), |
| 176 | + Keyframe.ofFloat(0.5f, 3 * shakeFactor), |
| 177 | + Keyframe.ofFloat(0.6f, -3 * shakeFactor), |
| 178 | + Keyframe.ofFloat(0.7f, -3 * shakeFactor), |
| 179 | + Keyframe.ofFloat(0.8f, 3 * shakeFactor), |
| 180 | + Keyframe.ofFloat(0.9f, -3 * shakeFactor), |
| 181 | + Keyframe.ofFloat(1f, 0)); |
| 182 | + |
| 183 | + |
| 184 | + ObjectAnimator.ofPropertyValuesHolder(target, scaleXHolder, scaleYHolder, rotationHolder).setDuration(1000).start(); |
| 185 | + } |
| 186 | + |
| 187 | + /** |
| 188 | + * 显示颜色变化的动画 |
| 189 | + * |
| 190 | + * @param target |
| 191 | + * @param startColor |
| 192 | + * @param endColor |
| 193 | + */ |
| 194 | + private void displayColorAnimation(final View target, final String startColor, final String endColor) { |
| 195 | + ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, 100f); |
| 196 | + valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { |
| 197 | + @Override |
| 198 | + public void onAnimationUpdate(ValueAnimator animation) { |
| 199 | + if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB_MR1) { |
| 200 | + float fraction = animation.getAnimatedFraction(); |
| 201 | + if (target != null) { |
| 202 | + int startRed = Integer.parseInt(startColor.substring(1, 3), 16); |
| 203 | + int startGreen = Integer.parseInt(startColor.substring(3, 5), 16); |
| 204 | + int startBlue = Integer.parseInt(startColor.substring(5, 7), 16); |
| 205 | + int endRed = Integer.parseInt(endColor.substring(1, 3), 16); |
| 206 | + int endGreen = Integer.parseInt(endColor.substring(3, 5), 16); |
| 207 | + int endBlue = Integer.parseInt(endColor.substring(5, 7), 16); |
| 208 | + |
| 209 | + int redDiff = Math.abs(endRed - startRed); |
| 210 | + int greenDiff = Math.abs(endGreen - startGreen); |
| 211 | + int blueDiff = Math.abs(endBlue - startBlue); |
| 212 | + int colorDiff = redDiff + greenDiff + blueDiff; |
| 213 | + |
| 214 | + int currRed = getCurrentColor(startRed, endRed, colorDiff, fraction); |
| 215 | + int currGreen = getCurrentColor(startGreen, endGreen, colorDiff, fraction); |
| 216 | + int currBlue = getCurrentColor(startBlue, endBlue, colorDiff, fraction); |
| 217 | + |
| 218 | + String colorString = "#" + getHexString(currRed) + getHexString(currGreen) + getHexString(currBlue); |
| 219 | + int color = Color.parseColor(colorString); |
| 220 | + target.setBackgroundColor(color); |
| 221 | + |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + } |
| 226 | + }); |
| 227 | + valueAnimator.setDuration(3000); |
| 228 | + valueAnimator.start(); |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * 获取当前新颜色 |
| 234 | + * |
| 235 | + * @param startColor |
| 236 | + * @param endColor |
| 237 | + * @param colorDiff |
| 238 | + * @param fraction |
| 239 | + * @return |
| 240 | + */ |
| 241 | + private int getCurrentColor(int startColor, int endColor, int colorDiff, float fraction) { |
| 242 | + int currentColor = 0; |
| 243 | + if (startColor > endColor) { |
| 244 | + currentColor = (int) (startColor - fraction * colorDiff); |
| 245 | + } else { |
| 246 | + currentColor = (int) (startColor + fraction * colorDiff); |
| 247 | + } |
| 248 | + if (currentColor >= 0 && currentColor <= 256) {//最终的色值要确保在0到256之间 |
| 249 | + return currentColor; |
| 250 | + } else { |
| 251 | + currentColor = endColor; |
| 252 | + } |
| 253 | + return currentColor; |
| 254 | + } |
| 255 | + |
| 256 | + /** |
| 257 | + * 将10进制颜色值转换成16进制。 |
| 258 | + */ |
| 259 | + private String getHexString(int value) { |
| 260 | + String hexString = Integer.toHexString(value); |
| 261 | + if (hexString.length() == 1) { |
| 262 | + hexString = "0" + hexString; |
| 263 | + } |
| 264 | + return hexString; |
| 265 | + } |
| 266 | +} |
0 commit comments