summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authord34d <clark@cyngn.com>2016-05-04 08:59:27 -0700
committerd34d <clark@cyngn.com>2016-05-04 08:59:27 -0700
commit9ba0e80ae500d6068d700d55894cee12756a40bb (patch)
tree5da2612aa9961bbdd87a2e150b152c1b535b7cf0
parent70656f2a77b29a7a71393ec8c152bbfdb6cc1af7 (diff)
downloadandroid_external_cyanogen_UICommon-9ba0e80ae500d6068d700d55894cee12756a40bb.tar.gz
android_external_cyanogen_UICommon-9ba0e80ae500d6068d700d55894cee12756a40bb.tar.bz2
android_external_cyanogen_UICommon-9ba0e80ae500d6068d700d55894cee12756a40bb.zip
Add StopMotionVectorDrawable to uicommon
The StopMotionVectorDrawable provides control over the position within an animation set of the warpped AnimatedVectorDrawable. Change-Id: I586c54c7dbd5c9f6f373a87e544bc0669f4ba8f1
-rw-r--r--src/org/cyanogenmod/graphics/drawable/StopMotionVectorDrawable.java97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/org/cyanogenmod/graphics/drawable/StopMotionVectorDrawable.java b/src/org/cyanogenmod/graphics/drawable/StopMotionVectorDrawable.java
new file mode 100644
index 0000000..869c866
--- /dev/null
+++ b/src/org/cyanogenmod/graphics/drawable/StopMotionVectorDrawable.java
@@ -0,0 +1,97 @@
+/*
+ * Copyright (C) 2016 The CyanogenMod Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License
+ */
+
+package org.cyanogenmod.graphics.drawable;
+
+import android.animation.Animator;
+import android.animation.AnimatorSet;
+import android.animation.ValueAnimator;
+import android.graphics.drawable.AnimatedVectorDrawable;
+import android.graphics.drawable.Drawable;
+import android.graphics.drawable.DrawableWrapper;
+import android.util.Log;
+
+import java.lang.reflect.Field;
+import java.util.ArrayList;
+
+/**
+ * Wraps an {@link AnimatedVectorDrawable} and provides methods for setting the temporal position
+ * within the backing {@link AnimatorSet} of the wrapped {@link AnimatedVectorDrawable}.
+ */
+public class StopMotionVectorDrawable extends DrawableWrapper {
+ private static final String TAG = StopMotionVectorDrawable.class.getSimpleName();
+
+ private AnimatedVectorDrawable mDrawable;
+ private AnimatorSet mAnimatorSet;
+
+ /**
+ * Creates a new wrapper around the specified drawable.
+ *
+ * @param dr The drawable to wrap. Must be an {@link AnimatedVectorDrawable}
+ */
+ public StopMotionVectorDrawable(Drawable dr) {
+ super(dr);
+ setDrawable(dr);
+ }
+
+ /**
+ * {@see DrawableWrapper$setDrawable}
+ * @param dr the wrapped drawable
+ * @throws IllegalArgumentException IF drawable is not an {@link AnimatedVectorDrawable}
+ */
+ @Override
+ public void setDrawable(Drawable dr) {
+ if (dr != null && !(dr instanceof AnimatedVectorDrawable)) {
+ throw new IllegalArgumentException("Drawable must be an AnimatedVectorDrawable");
+ }
+
+ super.setDrawable(dr);
+ mDrawable = (AnimatedVectorDrawable) dr;
+ if (mDrawable != null) {
+ mDrawable.reset();
+ getAnimatorSetViaReflection();
+ }
+ }
+
+ /**
+ * {@see android.animation.ValueAnimator#setCurrentFraction}
+ * @param fraction The fraction to which the animation is advanced or rewound. Values outside
+ * the range of 0 to the maximum fraction for the animator will be clamped to
+ * the correct range.
+ */
+ public void setCurrentFraction(float fraction) {
+ if (mDrawable == null || mAnimatorSet == null) return;
+
+ ArrayList<Animator> animators = mAnimatorSet.getChildAnimations();
+ for (Animator animator : animators) {
+ if (animator instanceof ValueAnimator) {
+ ((ValueAnimator) animator).setCurrentFraction(fraction);
+ }
+ }
+
+ mDrawable.invalidateSelf();
+ }
+
+ private void getAnimatorSetViaReflection() {
+ try {
+ Field _mAnimatorSet = AnimatedVectorDrawable.class.getDeclaredField("mAnimatorSet");
+ _mAnimatorSet.setAccessible(true);
+ mAnimatorSet = (AnimatorSet) _mAnimatorSet.get(mDrawable);
+ } catch (NoSuchFieldException | IllegalAccessException e) {
+ Log.e(TAG, "Could not get mAnimatorSet via reflection", e);
+ }
+ }
+}