AnimatedVectorDrawable.java revision 17cd4dfe3a05c2eddbcbc76066ff3b13fc3f2c8b
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.annotation.NonNull; 20import android.content.res.ColorStateList; 21import android.content.res.Resources; 22import android.content.res.Resources.Theme; 23import android.content.res.TypedArray; 24import android.graphics.Canvas; 25import android.graphics.ColorFilter; 26import android.graphics.Outline; 27import android.graphics.PorterDuff; 28import android.graphics.Rect; 29import android.util.ArrayMap; 30import android.util.AttributeSet; 31import android.util.Log; 32 33import com.android.internal.R; 34 35import org.xmlpull.v1.XmlPullParser; 36import org.xmlpull.v1.XmlPullParserException; 37 38import java.io.IOException; 39import java.util.ArrayList; 40 41/** 42 * This class uses {@link android.animation.ObjectAnimator} and 43 * {@link android.animation.AnimatorSet} to animate the properties of a 44 * {@link android.graphics.drawable.VectorDrawable} to create an animated drawable. 45 * <p> 46 * AnimatedVectorDrawable are normally defined as 3 separate XML files. 47 * </p> 48 * <p> 49 * First is the XML file for {@link android.graphics.drawable.VectorDrawable}. 50 * Note that we allow the animation happen on the group's attributes and path's 51 * attributes, which requires they are uniquely named in this xml file. Groups 52 * and paths without animations do not need names. 53 * </p> 54 * <li>Here is a simple VectorDrawable in this vectordrawable.xml file. 55 * <pre> 56 * <vector xmlns:android="http://schemas.android.com/apk/res/android" 57 * android:height="64dp" 58 * android:width="64dp" 59 * android:viewportHeight="600" 60 * android:viewportWidth="600" > 61 * <group 62 * android:name="rotationGroup" 63 * android:pivotX="300.0" 64 * android:pivotY="300.0" 65 * android:rotation="45.0" > 66 * <path 67 * android:name="v" 68 * android:fillColor="#000000" 69 * android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" /> 70 * </group> 71 * </vector> 72 * </pre></li> 73 * <p> 74 * Second is the AnimatedVectorDrawable's xml file, which defines the target 75 * VectorDrawable, the target paths and groups to animate, the properties of the 76 * path and group to animate and the animations defined as the ObjectAnimators 77 * or AnimatorSets. 78 * </p> 79 * <li>Here is a simple AnimatedVectorDrawable defined in this avd.xml file. 80 * Note how we use the names to refer to the groups and paths in the vectordrawable.xml. 81 * <pre> 82 * <animated-vector xmlns:android="http://schemas.android.com/apk/res/android" 83 * android:drawable="@drawable/vectordrawable" > 84 * <target 85 * android:name="rotationGroup" 86 * android:animation="@anim/rotation" /> 87 * <target 88 * android:name="v" 89 * android:animation="@anim/path_morph" /> 90 * </animated-vector> 91 * </pre></li> 92 * <p> 93 * Last is the Animator xml file, which is the same as a normal ObjectAnimator 94 * or AnimatorSet. 95 * To complete this example, here are the 2 animator files used in avd.xml: 96 * rotation.xml and path_morph.xml. 97 * </p> 98 * <li>Here is the rotation.xml, which will rotate the target group for 360 degrees. 99 * <pre> 100 * <objectAnimator 101 * android:duration="6000" 102 * android:propertyName="rotation" 103 * android:valueFrom="0" 104 * android:valueTo="360" /> 105 * </pre></li> 106 * <li>Here is the path_morph.xml, which will morph the path from one shape to 107 * the other. Note that the paths must be compatible for morphing. 108 * In more details, the paths should have exact same length of commands , and 109 * exact same length of parameters for each commands. 110 * Note that the path string are better stored in strings.xml for reusing. 111 * <pre> 112 * <set xmlns:android="http://schemas.android.com/apk/res/android"> 113 * <objectAnimator 114 * android:duration="3000" 115 * android:propertyName="pathData" 116 * android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z" 117 * android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z" 118 * android:valueType="pathType"/> 119 * </set> 120 * </pre></li> 121 * 122 * @attr ref android.R.styleable#AnimatedVectorDrawable_drawable 123 * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_name 124 * @attr ref android.R.styleable#AnimatedVectorDrawableTarget_animation 125 */ 126public class AnimatedVectorDrawable extends Drawable implements Animatable { 127 private static final String LOGTAG = AnimatedVectorDrawable.class.getSimpleName(); 128 129 private static final String ANIMATED_VECTOR = "animated-vector"; 130 private static final String TARGET = "target"; 131 132 private static final boolean DBG_ANIMATION_VECTOR_DRAWABLE = false; 133 134 private AnimatedVectorDrawableState mAnimatedVectorState; 135 136 private boolean mMutated; 137 138 public AnimatedVectorDrawable() { 139 this(null, null); 140 } 141 142 private AnimatedVectorDrawable(AnimatedVectorDrawableState state, Resources res) { 143 mAnimatedVectorState = new AnimatedVectorDrawableState(state, mCallback, res); 144 } 145 146 @Override 147 public Drawable mutate() { 148 if (!mMutated && super.mutate() == this) { 149 mAnimatedVectorState.mVectorDrawable.mutate(); 150 mMutated = true; 151 } 152 return this; 153 } 154 155 /** 156 * @hide 157 */ 158 public void clearMutated() { 159 super.clearMutated(); 160 mAnimatedVectorState.mVectorDrawable.clearMutated(); 161 mMutated = false; 162 } 163 164 @Override 165 public ConstantState getConstantState() { 166 mAnimatedVectorState.mChangingConfigurations = getChangingConfigurations(); 167 return mAnimatedVectorState; 168 } 169 170 @Override 171 public int getChangingConfigurations() { 172 return super.getChangingConfigurations() | mAnimatedVectorState.mChangingConfigurations; 173 } 174 175 @Override 176 public void draw(Canvas canvas) { 177 mAnimatedVectorState.mVectorDrawable.draw(canvas); 178 if (isStarted()) { 179 invalidateSelf(); 180 } 181 } 182 183 @Override 184 protected void onBoundsChange(Rect bounds) { 185 mAnimatedVectorState.mVectorDrawable.setBounds(bounds); 186 } 187 188 @Override 189 protected boolean onStateChange(int[] state) { 190 return mAnimatedVectorState.mVectorDrawable.setState(state); 191 } 192 193 @Override 194 protected boolean onLevelChange(int level) { 195 return mAnimatedVectorState.mVectorDrawable.setLevel(level); 196 } 197 198 @Override 199 public int getAlpha() { 200 return mAnimatedVectorState.mVectorDrawable.getAlpha(); 201 } 202 203 @Override 204 public void setAlpha(int alpha) { 205 mAnimatedVectorState.mVectorDrawable.setAlpha(alpha); 206 } 207 208 @Override 209 public void setColorFilter(ColorFilter colorFilter) { 210 mAnimatedVectorState.mVectorDrawable.setColorFilter(colorFilter); 211 } 212 213 @Override 214 public void setTintList(ColorStateList tint) { 215 mAnimatedVectorState.mVectorDrawable.setTintList(tint); 216 } 217 218 @Override 219 public void setHotspot(float x, float y) { 220 mAnimatedVectorState.mVectorDrawable.setHotspot(x, y); 221 } 222 223 @Override 224 public void setHotspotBounds(int left, int top, int right, int bottom) { 225 mAnimatedVectorState.mVectorDrawable.setHotspotBounds(left, top, right, bottom); 226 } 227 228 @Override 229 public void setTintMode(PorterDuff.Mode tintMode) { 230 mAnimatedVectorState.mVectorDrawable.setTintMode(tintMode); 231 } 232 233 @Override 234 public boolean setVisible(boolean visible, boolean restart) { 235 mAnimatedVectorState.mVectorDrawable.setVisible(visible, restart); 236 return super.setVisible(visible, restart); 237 } 238 239 /** {@hide} */ 240 @Override 241 public void setLayoutDirection(int layoutDirection) { 242 mAnimatedVectorState.mVectorDrawable.setLayoutDirection(layoutDirection); 243 } 244 245 @Override 246 public boolean isStateful() { 247 return mAnimatedVectorState.mVectorDrawable.isStateful(); 248 } 249 250 @Override 251 public int getOpacity() { 252 return mAnimatedVectorState.mVectorDrawable.getOpacity(); 253 } 254 255 @Override 256 public int getIntrinsicWidth() { 257 return mAnimatedVectorState.mVectorDrawable.getIntrinsicWidth(); 258 } 259 260 @Override 261 public int getIntrinsicHeight() { 262 return mAnimatedVectorState.mVectorDrawable.getIntrinsicHeight(); 263 } 264 265 @Override 266 public void getOutline(@NonNull Outline outline) { 267 mAnimatedVectorState.mVectorDrawable.getOutline(outline); 268 } 269 270 @Override 271 public void inflate(Resources res, XmlPullParser parser, AttributeSet attrs, Theme theme) 272 throws XmlPullParserException, IOException { 273 274 int eventType = parser.getEventType(); 275 float pathErrorScale = 1; 276 while (eventType != XmlPullParser.END_DOCUMENT) { 277 if (eventType == XmlPullParser.START_TAG) { 278 final String tagName = parser.getName(); 279 if (ANIMATED_VECTOR.equals(tagName)) { 280 final TypedArray a = obtainAttributes(res, theme, attrs, 281 R.styleable.AnimatedVectorDrawable); 282 int drawableRes = a.getResourceId( 283 R.styleable.AnimatedVectorDrawable_drawable, 0); 284 if (drawableRes != 0) { 285 VectorDrawable vectorDrawable = (VectorDrawable) res.getDrawable( 286 drawableRes, theme).mutate(); 287 vectorDrawable.setAllowCaching(false); 288 vectorDrawable.setCallback(mCallback); 289 pathErrorScale = vectorDrawable.getPixelSize(); 290 if (mAnimatedVectorState.mVectorDrawable != null) { 291 mAnimatedVectorState.mVectorDrawable.setCallback(null); 292 } 293 mAnimatedVectorState.mVectorDrawable = vectorDrawable; 294 } 295 a.recycle(); 296 } else if (TARGET.equals(tagName)) { 297 final TypedArray a = obtainAttributes(res, theme, attrs, 298 R.styleable.AnimatedVectorDrawableTarget); 299 final String target = a.getString( 300 R.styleable.AnimatedVectorDrawableTarget_name); 301 302 int id = a.getResourceId( 303 R.styleable.AnimatedVectorDrawableTarget_animation, 0); 304 if (id != 0) { 305 Animator objectAnimator = AnimatorInflater.loadAnimator(res, theme, id, 306 pathErrorScale); 307 setupAnimatorsForTarget(target, objectAnimator); 308 } 309 a.recycle(); 310 } 311 } 312 313 eventType = parser.next(); 314 } 315 } 316 317 @Override 318 public boolean canApplyTheme() { 319 return super.canApplyTheme() || mAnimatedVectorState != null 320 && mAnimatedVectorState.mVectorDrawable != null 321 && mAnimatedVectorState.mVectorDrawable.canApplyTheme(); 322 } 323 324 @Override 325 public void applyTheme(Theme t) { 326 super.applyTheme(t); 327 328 final VectorDrawable vectorDrawable = mAnimatedVectorState.mVectorDrawable; 329 if (vectorDrawable != null && vectorDrawable.canApplyTheme()) { 330 vectorDrawable.applyTheme(t); 331 } 332 } 333 334 private static class AnimatedVectorDrawableState extends ConstantState { 335 int mChangingConfigurations; 336 VectorDrawable mVectorDrawable; 337 ArrayList<Animator> mAnimators; 338 ArrayMap<Animator, String> mTargetNameMap; 339 340 public AnimatedVectorDrawableState(AnimatedVectorDrawableState copy, 341 Callback owner, Resources res) { 342 if (copy != null) { 343 mChangingConfigurations = copy.mChangingConfigurations; 344 if (copy.mVectorDrawable != null) { 345 final ConstantState cs = copy.mVectorDrawable.getConstantState(); 346 if (res != null) { 347 mVectorDrawable = (VectorDrawable) cs.newDrawable(res); 348 } else { 349 mVectorDrawable = (VectorDrawable) cs.newDrawable(); 350 } 351 mVectorDrawable = (VectorDrawable) mVectorDrawable.mutate(); 352 mVectorDrawable.setCallback(owner); 353 mVectorDrawable.setLayoutDirection(copy.mVectorDrawable.getLayoutDirection()); 354 mVectorDrawable.setBounds(copy.mVectorDrawable.getBounds()); 355 mVectorDrawable.setAllowCaching(false); 356 } 357 if (copy.mAnimators != null) { 358 final int numAnimators = copy.mAnimators.size(); 359 mAnimators = new ArrayList<Animator>(numAnimators); 360 mTargetNameMap = new ArrayMap<Animator, String>(numAnimators); 361 for (int i = 0; i < numAnimators; ++i) { 362 Animator anim = copy.mAnimators.get(i); 363 Animator animClone = anim.clone(); 364 String targetName = copy.mTargetNameMap.get(anim); 365 Object targetObject = mVectorDrawable.getTargetByName(targetName); 366 animClone.setTarget(targetObject); 367 mAnimators.add(animClone); 368 mTargetNameMap.put(animClone, targetName); 369 } 370 } 371 } else { 372 mVectorDrawable = new VectorDrawable(); 373 } 374 } 375 376 @Override 377 public Drawable newDrawable() { 378 return new AnimatedVectorDrawable(this, null); 379 } 380 381 @Override 382 public Drawable newDrawable(Resources res) { 383 return new AnimatedVectorDrawable(this, res); 384 } 385 386 @Override 387 public int getChangingConfigurations() { 388 return mChangingConfigurations; 389 } 390 } 391 392 private void setupAnimatorsForTarget(String name, Animator animator) { 393 Object target = mAnimatedVectorState.mVectorDrawable.getTargetByName(name); 394 animator.setTarget(target); 395 if (mAnimatedVectorState.mAnimators == null) { 396 mAnimatedVectorState.mAnimators = new ArrayList<Animator>(); 397 mAnimatedVectorState.mTargetNameMap = new ArrayMap<Animator, String>(); 398 } 399 mAnimatedVectorState.mAnimators.add(animator); 400 mAnimatedVectorState.mTargetNameMap.put(animator, name); 401 if (DBG_ANIMATION_VECTOR_DRAWABLE) { 402 Log.v(LOGTAG, "add animator for target " + name + " " + animator); 403 } 404 } 405 406 @Override 407 public boolean isRunning() { 408 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 409 final int size = animators.size(); 410 for (int i = 0; i < size; i++) { 411 final Animator animator = animators.get(i); 412 if (animator.isRunning()) { 413 return true; 414 } 415 } 416 return false; 417 } 418 419 private boolean isStarted() { 420 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 421 final int size = animators.size(); 422 for (int i = 0; i < size; i++) { 423 final Animator animator = animators.get(i); 424 if (animator.isStarted()) { 425 return true; 426 } 427 } 428 return false; 429 } 430 431 @Override 432 public void start() { 433 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 434 final int size = animators.size(); 435 for (int i = 0; i < size; i++) { 436 final Animator animator = animators.get(i); 437 if (!animator.isStarted()) { 438 animator.start(); 439 } 440 } 441 invalidateSelf(); 442 } 443 444 @Override 445 public void stop() { 446 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 447 final int size = animators.size(); 448 for (int i = 0; i < size; i++) { 449 final Animator animator = animators.get(i); 450 animator.end(); 451 } 452 } 453 454 /** 455 * Reverses ongoing animations or starts pending animations in reverse. 456 * <p> 457 * NOTE: Only works of all animations are ValueAnimators. 458 * @hide 459 */ 460 public void reverse() { 461 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 462 final int size = animators.size(); 463 for (int i = 0; i < size; i++) { 464 final Animator animator = animators.get(i); 465 if (animator.canReverse()) { 466 animator.reverse(); 467 } else { 468 Log.w(LOGTAG, "AnimatedVectorDrawable can't reverse()"); 469 } 470 } 471 } 472 473 /** 474 * @hide 475 */ 476 public boolean canReverse() { 477 final ArrayList<Animator> animators = mAnimatedVectorState.mAnimators; 478 final int size = animators.size(); 479 for (int i = 0; i < size; i++) { 480 final Animator animator = animators.get(i); 481 if (!animator.canReverse()) { 482 return false; 483 } 484 } 485 return true; 486 } 487 488 private final Callback mCallback = new Callback() { 489 @Override 490 public void invalidateDrawable(Drawable who) { 491 invalidateSelf(); 492 } 493 494 @Override 495 public void scheduleDrawable(Drawable who, Runnable what, long when) { 496 scheduleSelf(what, when); 497 } 498 499 @Override 500 public void unscheduleDrawable(Drawable who, Runnable what) { 501 unscheduleSelf(what); 502 } 503 }; 504} 505