blob: 952c86ad72b928583fa04b3009dbda594fa01d1b (
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
|
/*
* 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.lineageos.internal.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);
Class<?> innerClazz = Class.forName("android.graphics.drawable.AnimatedVectorDrawable$VectorDrawableAnimatorUI");
mDrawable.forceAnimationOnUI();
Object _inner = _mAnimatorSet.get(mDrawable);
Field _mSet = innerClazz.getDeclaredField("mSet");
_mSet.setAccessible(true);
mAnimatorSet = (AnimatorSet) _mSet.get(_inner);
} catch (NoSuchFieldException | IllegalAccessException | ClassNotFoundException e) {
Log.e(TAG, "Could not get mAnimatorSet via reflection", e);
}
}
}
|