AnimatedVectorDrawable.java revision 7e7ea9da6785a8345feffb754ddcb55b16cf69c9
1/* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except 5 * in compliance with the License. You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software distributed under the License 10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express 11 * or implied. See the License for the specific language governing permissions and limitations under 12 * the License. 13 */ 14 15package android.graphics.drawable; 16 17import android.animation.Animator; 18import android.animation.AnimatorInflater; 19import android.content.res.Resources; 20import android.content.res.Resources.Theme; 21import android.content.res.TypedArray; 22import android.graphics.Canvas; 23import android.graphics.ColorFilter; 24import android.graphics.Rect; 25import android.util.AttributeSet; 26import android.util.Log; 27 28import com.android.internal.R; 29 30import org.xmlpull.v1.XmlPullParser; 31import org.xmlpull.v1.XmlPullParserException; 32 33import java.io.IOException; 34import java.util.ArrayList; 35 36/** 37 * This class uses {@link android.animation.ObjectAnimator} and 38 * {@link android.animation.AnimatorSet} to animate the properties of a 39 * {@link android.graphics.drawable.VectorDrawable} to create an animated drawable. 40 * <p> 41 * AnimatedVectorDrawable are normally defined as 3 separate XML files. 42 * </p> 43 * <p> 44 * First is the XML file for {@link android.graphics.drawable.VectorDrawable}. 45 * Note that we allow the animation happen on the group's attributes and path's 46 * attributes, which requires they are uniquely named in this xml file. Groups 47 * and paths without animations do not need names. 48 * </p> 49 * <li>Here is a simple VectorDrawable in this vectordrawable.xml file. 50 * <pre> 51 * <vector xmlns:android="http://schemas.android.com/apk/res/android" > 52 * <size 53 * android:height="64dp" 54 * android:width="64dp" /> 55 * <viewport 56 * android:viewportHeight="600" 57 * android:viewportWidth="600" /> 58 * <group 59 * android:name="rotationGroup" 60 * android:pivotX="300.0" 61 * android:pivotY="300.0" 62 * android:rotation="45.0" > 63 * <path 64 * android:name="v" 65 * android:fill="#000000" 66 * android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /> 67 * </group> 68 * </vector> 69 * </pre></li> 70 * <p> 71 * Second is the AnimatedVectorDrawable's xml file, which defines the target 72 * VectorDrawable, the target paths and groups to animate, the properties of the 73 * path and group to animate and the animations defined as the ObjectAnimators 74 * or AnimatorSets. 75 * </p> 76 * <li>Here is a simple AnimatedVectorDrawable defined in this avd.xml file. 77 * Note how we use the names to refer to the groups and paths in the vectordrawable.xml. 78 * <pre> 79 * <animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 80 * android:drawable="@drawable/vectordrawable" > 81 * <target 82 * android:name="rotationGroup" 83 * android:animation="@anim/rotation" /> 84 * <target 85 * android:name="v" 86 * android:animation="@anim/path_morph" /> 87 * </animated-vector> 88 * </pre></li> 89 * <p> 90 * Last is the Animator xml file, which is the same as a normal ObjectAnimator 91 * or AnimatorSet. 92 * To complete this example, here are the 2 animator files used in avd.xml: 93 * rotation.xml and path_morph.xml. 94 * </p> 95 * <li>Here is the rotation.xml, which will rotate the target group for 360 degrees. 96 * <pre> 97 * <objectAnimator 98 * android:duration="6000" 99 * android:propertyName="rotation" 100 * android:valueFrom="0" 101 * android:valueTo="360" /> 102 * </pre></li> 103 * <li>Here is the path_morph.xml, which will morph the path from one shape to 104 * the other. Note that the paths must be compatible for morphing. 105 * In more details, the paths should have exact same length of commands , and 106 * exact same length of parameters for each commands. 107 * Note that the path string are better stored in strings.xml for reusing. 108 * <pre> 109 * <set xmlns:android="http://schemas.android.com/apk/res/android"> 110 * <objectAnimator 111 * android:duration="3000" 112 * android:propertyName="pathData" 113 * android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z" 114 * android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z" 115 * android:valueType="pathType"/> 116 * </set> 117 * </pre></li> 118 * 119 * @attr ref android.R.styleable#AnimatedVectorDrawable_drawable 120 * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_name 121 * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_animation 122 */ 123public class AnimatedVectorDrawable extends Drawable implements Animatable { 124 private static final String LOGTAG = AnimatedVectorDrawable.class.getSimpleName(); 125 126 private static final String ANIMATED_VECTOR = "animated-vector"; 127 private static final String TARGET = "target"; 128 129 private static final boolean DBG_ANIMATION_VECTOR_DRAWABLE = false; 130 131 private final AnimatedVectorDrawableState mAnimatedVectorState; 132 133 134 public AnimatedVectorDrawable() { 135 mAnimatedVectorState = new AnimatedVectorDrawableState( 136 new AnimatedVectorDrawableState(null)); 137 } 138 139 private AnimatedVectorDrawable(AnimatedVectorDrawableState state, Resources res, 140 Theme theme) { 141 // TODO: Correctly handle the constant state for AVD. 142 mAnimatedVectorState = new AnimatedVectorDrawableState(state); 143 if (theme != null && canApplyTheme()) { 144 applyTheme(theme); 145 } 146 } 147 148 @Override 149 public ConstantState getConstantState() { 150 return null; 151 } 152 153 @Override 154 public void draw(Canvas canvas) { 155 mAnimatedVectorState.mVectorDrawable.draw(canvas); 156 if (isRunning()) { 157 invalidateSelf(); 158 } 159 } 160 161 @Override 162 protected void onBoundsChange(Rect bounds) { 163 mAnimatedVectorState.mVectorDrawable.setBounds(bounds); 164 } 165 166 @Override 167 public int getAlpha() { 168 return mAnimatedVectorState.mVectorDrawable.getAlpha(); 169 } 170 171 @Override 172 public void setAlpha(int alpha) { 173 mAnimatedVectorState.mVectorDrawable.setAlpha(alpha); 174 } 175 176 @Override 177 public void setColorFilter(ColorFilter colorFilter) { 178 mAnimatedVectorState.mVectorDrawable.setColorFilter(colorFilter); 179 } 180 181 @Override 182 public int getOpacity() { 183 return mAnimatedVectorState.mVectorDrawable.getOpacity(); 184 } 185 186 @Override 187 public int getIntrinsicWidth() { 188 return mAnimatedVectorState.mVectorDrawable.getIntrinsicWidth(); 189 } 190 191 @Override 192 public int getIntrinsicHeight() { 193 return mAnimatedVectorState.mVectorDrawable.getIntrinsicHeight(); 194 } 195 196 @Override 197 public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme) 198 throws XmlPullParserException, IOException { 199 200 int eventType = parser.getEventType(); 201 while (eventType != XmlPullParser.END_DOCUMENT) { 202 if (eventType == XmlPullParser.START_TAG) { 203 final String tagName = parser.getName(); 204 if (ANIMATED_VECTOR.equals(tagName)) { 205 final TypedArray a = obtainAttributes(res, theme, attrs, 206 R.styleable.AnimatedVectorDrawable); 207 int drawableRes = a.getResourceId( 208 R.styleable.AnimatedVectorDrawable_drawable, 0); 209 if (drawableRes != 0) { 210 mAnimatedVectorState.mVectorDrawable = (VectorDrawable) res.getDrawable( 211 drawableRes); 212 } 213 a.recycle(); 214 } else if (TARGET.equals(tagName)) { 215 final TypedArray a = obtainAttributes(res, theme, attrs, 216 R.styleable.AnimatedVectorDrawableTarget); 217 final String target = a.getString( 218 R.styleable.AnimatedVectorDrawableTarget_name); 219 220 int id = a.getResourceId( 221 R.styleable.AnimatedVectorDrawableTarget_animation, 0); 222 if (id != 0) { 223 Animator objectAnimator = AnimatorInflater.loadAnimator(res, theme, id); 224 setupAnimatorsForTarget(target, objectAnimator); 225 } 226 a.recycle(); 227 } 228 } 229 230 eventType = parser.next(); 231 } 232 } 233 234 @Override 235 public boolean canApplyTheme() { 236 return super.canApplyTheme() || mAnimatedVectorState != null 237 && mAnimatedVectorState.canApplyTheme(); 238 } 239 240 @Override 241 public void applyTheme(Theme t) { 242 super.applyTheme(t); 243 244 final VectorDrawable vectorDrawable = mAnimatedVectorState.mVectorDrawable; 245 if (vectorDrawable != null && vectorDrawable.canApplyTheme()) { 246 vectorDrawable.applyTheme(t); 247 } 248 } 249 250 private static class AnimatedVectorDrawableState extends ConstantState { 251 int mChangingConfigurations; 252 VectorDrawable mVectorDrawable; 253 ArrayList<Animator> mAnimators; 254 255 public AnimatedVectorDrawableState(AnimatedVectorDrawableState copy) { 256 if (copy != null) { 257 mChangingConfigurations = copy.mChangingConfigurations; 258 // TODO: Make sure the constant state are handled correctly. 259 mVectorDrawable = new VectorDrawable(); 260 mAnimators = new ArrayList<Animator>(); 261 } 262 } 263 264 @Override 265 public Drawable newDrawable() { 266 return new AnimatedVectorDrawable(this, null, null); 267 } 268 269 @Override 270 public Drawable newDrawable(Resources res) { 271 return new AnimatedVectorDrawable(this, res, null); 272 } 273 274 @Override 275 public Drawable newDrawable(Resources res, Theme theme) { 276 return new AnimatedVectorDrawable(this, res, theme); 277 } 278 279 @Override 280 public int getChangingConfigurations() { 281 return mChangingConfigurations; 282 } 283 } 284 285 private void setupAnimatorsForTarget(String name, Animator animator) { 286 Object target = mAnimatedVectorState.mVectorDrawable.getTargetByName(name); 287 animator.setTarget(target); 288 mAnimatedVectorState.mAnimators.add(animator); 289 if (DBG_ANIMATION_VECTOR_DRAWABLE) { 290 Log.v(LOGTAG, "add animator for target " + name + " " + animator); 291 } 292 } 293 294 @Override 295 public boolean isRunning() { 296 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 297 final int size = animators.size(); 298 for (int i = 0; i < size; i++) { 299 final Animator animator = animators.get(i); 300 if (animator.isRunning()) { 301 return true; 302 } 303 } 304 return false; 305 } 306 307 @Override 308 public void start() { 309 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 310 final int size = animators.size(); 311 for (int i = 0; i < size; i++) { 312 final Animator animator = animators.get(i); 313 if (animator.isPaused()) { 314 animator.resume(); 315 } else if (!animator.isRunning()) { 316 animator.start(); 317 } 318 } 319 invalidateSelf(); 320 } 321 322 @Override 323 public void stop() { 324 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 325 final int size = animators.size(); 326 for (int i = 0; i < size; i++) { 327 final Animator animator = animators.get(i); 328 animator.pause(); 329 } 330 } 331} 332