| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.view.animation; |
| 18 |
|
| 19 |
import android.annotation.AnimRes; |
| 20 |
import android.annotation.InterpolatorRes; |
| 21 |
import android.annotation.TestApi; |
| 22 |
import android.compat.annotation.UnsupportedAppUsage; |
| 23 |
import android.content.Context; |
| 24 |
import android.content.res.Resources; |
| 25 |
import android.content.res.Resources.NotFoundException; |
| 26 |
import android.content.res.Resources.Theme; |
| 27 |
import android.content.res.XmlResourceParser; |
| 28 |
import android.os.SystemClock; |
| 29 |
import android.util.AttributeSet; |
| 30 |
import android.util.Xml; |
| 31 |
|
| 32 |
import org.xmlpull.v1.XmlPullParser; |
| 33 |
import org.xmlpull.v1.XmlPullParserException; |
| 34 |
|
| 35 |
import java.io.IOException; |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class AnimationUtils { |
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
private static final int TOGETHER = 0; |
| 47 |
private static final int SEQUENTIALLY = 1; |
| 48 |
|
| 49 |
private static class AnimationState { |
| 50 |
boolean animationClockLocked; |
| 51 |
long currentVsyncTimeMillis; |
| 52 |
long lastReportedTimeMillis; |
| 53 |
}; |
| 54 |
|
| 55 |
private static ThreadLocal<AnimationState> sAnimationState |
| 56 |
= new ThreadLocal<AnimationState>() { |
| 57 |
@Override |
| 58 |
protected AnimationState initialValue() { |
| 59 |
return new AnimationState(); |
| 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 |
@TestApi |
| 87 |
public static void lockAnimationClock(long vsyncMillis) { |
| 88 |
AnimationState state = sAnimationState.get(); |
| 89 |
state.animationClockLocked = true; |
| 90 |
state.currentVsyncTimeMillis = vsyncMillis; |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
@TestApi |
| 100 |
public static void unlockAnimationClock() { |
| 101 |
sAnimationState.get().animationClockLocked = false; |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static long currentAnimationTimeMillis() { |
| 115 |
AnimationState state = sAnimationState.get(); |
| 116 |
if (state.animationClockLocked) { |
| 117 |
|
| 118 |
return Math.max(state.currentVsyncTimeMillis, |
| 119 |
state.lastReportedTimeMillis); |
| 120 |
} |
| 121 |
state.lastReportedTimeMillis = SystemClock.uptimeMillis(); |
| 122 |
return state.lastReportedTimeMillis; |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public static Animation loadAnimation(Context context, @AnimRes int id) |
| 134 |
throws NotFoundException { |
| 135 |
|
| 136 |
XmlResourceParser parser = null; |
| 137 |
try { |
| 138 |
parser = context.getResources().getAnimation(id); |
| 139 |
return createAnimationFromXml(context, parser); |
| 140 |
} catch (XmlPullParserException ex) { |
| 141 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 142 |
Integer.toHexString(id)); |
| 143 |
rnf.initCause(ex); |
| 144 |
throw rnf; |
| 145 |
} catch (IOException ex) { |
| 146 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 147 |
Integer.toHexString(id)); |
| 148 |
rnf.initCause(ex); |
| 149 |
throw rnf; |
| 150 |
} finally { |
| 151 |
if (parser != null) parser.close(); |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
private static Animation createAnimationFromXml(Context c, XmlPullParser parser) |
| 156 |
throws XmlPullParserException, IOException { |
| 157 |
|
| 158 |
return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser)); |
| 159 |
} |
| 160 |
|
| 161 |
@UnsupportedAppUsage |
| 162 |
private static Animation createAnimationFromXml(Context c, XmlPullParser parser, |
| 163 |
AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException { |
| 164 |
|
| 165 |
Animation anim = null; |
| 166 |
|
| 167 |
|
| 168 |
int type; |
| 169 |
int depth = parser.getDepth(); |
| 170 |
|
| 171 |
while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 172 |
&& type != XmlPullParser.END_DOCUMENT) { |
| 173 |
|
| 174 |
if (type != XmlPullParser.START_TAG) { |
| 175 |
continue; |
| 176 |
} |
| 177 |
|
| 178 |
String name = parser.getName(); |
| 179 |
|
| 180 |
if (name.equals("set")) { |
| 181 |
anim = new AnimationSet(c, attrs); |
| 182 |
createAnimationFromXml(c, parser, (AnimationSet)anim, attrs); |
| 183 |
} else if (name.equals("alpha")) { |
| 184 |
anim = new AlphaAnimation(c, attrs); |
| 185 |
} else if (name.equals("scale")) { |
| 186 |
anim = new ScaleAnimation(c, attrs); |
| 187 |
} else if (name.equals("rotate")) { |
| 188 |
anim = new RotateAnimation(c, attrs); |
| 189 |
} else if (name.equals("translate")) { |
| 190 |
anim = new TranslateAnimation(c, attrs); |
| 191 |
} else if (name.equals("cliprect")) { |
| 192 |
anim = new ClipRectAnimation(c, attrs); |
| 193 |
} else if (name.equals("extend")) { |
| 194 |
anim = new ExtendAnimation(c, attrs); |
| 195 |
} else { |
| 196 |
throw new RuntimeException("Unknown animation name: " + parser.getName()); |
| 197 |
} |
| 198 |
|
| 199 |
if (parent != null) { |
| 200 |
parent.addAnimation(anim); |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
return anim; |
| 205 |
|
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
public static LayoutAnimationController loadLayoutAnimation(Context context, @AnimRes int id) |
| 217 |
throws NotFoundException { |
| 218 |
|
| 219 |
XmlResourceParser parser = null; |
| 220 |
try { |
| 221 |
parser = context.getResources().getAnimation(id); |
| 222 |
return createLayoutAnimationFromXml(context, parser); |
| 223 |
} catch (XmlPullParserException ex) { |
| 224 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 225 |
Integer.toHexString(id)); |
| 226 |
rnf.initCause(ex); |
| 227 |
throw rnf; |
| 228 |
} catch (IOException ex) { |
| 229 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 230 |
Integer.toHexString(id)); |
| 231 |
rnf.initCause(ex); |
| 232 |
throw rnf; |
| 233 |
} finally { |
| 234 |
if (parser != null) parser.close(); |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
private static LayoutAnimationController createLayoutAnimationFromXml(Context c, |
| 239 |
XmlPullParser parser) throws XmlPullParserException, IOException { |
| 240 |
|
| 241 |
return createLayoutAnimationFromXml(c, parser, Xml.asAttributeSet(parser)); |
| 242 |
} |
| 243 |
|
| 244 |
private static LayoutAnimationController createLayoutAnimationFromXml(Context c, |
| 245 |
XmlPullParser parser, AttributeSet attrs) throws XmlPullParserException, IOException { |
| 246 |
|
| 247 |
LayoutAnimationController controller = null; |
| 248 |
|
| 249 |
int type; |
| 250 |
int depth = parser.getDepth(); |
| 251 |
|
| 252 |
while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 253 |
&& type != XmlPullParser.END_DOCUMENT) { |
| 254 |
|
| 255 |
if (type != XmlPullParser.START_TAG) { |
| 256 |
continue; |
| 257 |
} |
| 258 |
|
| 259 |
String name = parser.getName(); |
| 260 |
|
| 261 |
if ("layoutAnimation".equals(name)) { |
| 262 |
controller = new LayoutAnimationController(c, attrs); |
| 263 |
} else if ("gridLayoutAnimation".equals(name)) { |
| 264 |
controller = new GridLayoutAnimationController(c, attrs); |
| 265 |
} else { |
| 266 |
throw new RuntimeException("Unknown layout animation name: " + name); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
return controller; |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
|
| 279 |
|
| 280 |
|
| 281 |
public static Animation makeInAnimation(Context c, boolean fromLeft) { |
| 282 |
Animation a; |
| 283 |
if (fromLeft) { |
| 284 |
a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_left); |
| 285 |
} else { |
| 286 |
a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_right); |
| 287 |
} |
| 288 |
|
| 289 |
a.setInterpolator(new DecelerateInterpolator()); |
| 290 |
a.setStartTime(currentAnimationTimeMillis()); |
| 291 |
return a; |
| 292 |
} |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
public static Animation makeOutAnimation(Context c, boolean toRight) { |
| 303 |
Animation a; |
| 304 |
if (toRight) { |
| 305 |
a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_right); |
| 306 |
} else { |
| 307 |
a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_out_left); |
| 308 |
} |
| 309 |
|
| 310 |
a.setInterpolator(new AccelerateInterpolator()); |
| 311 |
a.setStartTime(currentAnimationTimeMillis()); |
| 312 |
return a; |
| 313 |
} |
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
public static Animation makeInChildBottomAnimation(Context c) { |
| 324 |
Animation a; |
| 325 |
a = AnimationUtils.loadAnimation(c, com.android.internal.R.anim.slide_in_child_bottom); |
| 326 |
a.setInterpolator(new AccelerateInterpolator()); |
| 327 |
a.setStartTime(currentAnimationTimeMillis()); |
| 328 |
return a; |
| 329 |
} |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
public static Interpolator loadInterpolator(Context context, @AnimRes @InterpolatorRes int id) |
| 340 |
throws NotFoundException { |
| 341 |
XmlResourceParser parser = null; |
| 342 |
try { |
| 343 |
parser = context.getResources().getAnimation(id); |
| 344 |
return createInterpolatorFromXml(context.getResources(), context.getTheme(), parser); |
| 345 |
} catch (XmlPullParserException ex) { |
| 346 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 347 |
Integer.toHexString(id)); |
| 348 |
rnf.initCause(ex); |
| 349 |
throw rnf; |
| 350 |
} catch (IOException ex) { |
| 351 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 352 |
Integer.toHexString(id)); |
| 353 |
rnf.initCause(ex); |
| 354 |
throw rnf; |
| 355 |
} finally { |
| 356 |
if (parser != null) parser.close(); |
| 357 |
} |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
public static Interpolator loadInterpolator(Resources res, Theme theme, int id) throws NotFoundException { |
| 371 |
XmlResourceParser parser = null; |
| 372 |
try { |
| 373 |
parser = res.getAnimation(id); |
| 374 |
return createInterpolatorFromXml(res, theme, parser); |
| 375 |
} catch (XmlPullParserException ex) { |
| 376 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 377 |
Integer.toHexString(id)); |
| 378 |
rnf.initCause(ex); |
| 379 |
throw rnf; |
| 380 |
} catch (IOException ex) { |
| 381 |
NotFoundException rnf = new NotFoundException("Can't load animation resource ID #0x" + |
| 382 |
Integer.toHexString(id)); |
| 383 |
rnf.initCause(ex); |
| 384 |
throw rnf; |
| 385 |
} finally { |
| 386 |
if (parser != null) |
| 387 |
parser.close(); |
| 388 |
} |
| 389 |
|
| 390 |
} |
| 391 |
|
| 392 |
private static Interpolator createInterpolatorFromXml(Resources res, Theme theme, XmlPullParser parser) |
| 393 |
throws XmlPullParserException, IOException { |
| 394 |
|
| 395 |
BaseInterpolator interpolator = null; |
| 396 |
|
| 397 |
|
| 398 |
int type; |
| 399 |
int depth = parser.getDepth(); |
| 400 |
|
| 401 |
while (((type = parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth) |
| 402 |
&& type != XmlPullParser.END_DOCUMENT) { |
| 403 |
|
| 404 |
if (type != XmlPullParser.START_TAG) { |
| 405 |
continue; |
| 406 |
} |
| 407 |
|
| 408 |
AttributeSet attrs = Xml.asAttributeSet(parser); |
| 409 |
|
| 410 |
String name = parser.getName(); |
| 411 |
|
| 412 |
if (name.equals("linearInterpolator")) { |
| 413 |
interpolator = new LinearInterpolator(); |
| 414 |
} else if (name.equals("accelerateInterpolator")) { |
| 415 |
interpolator = new AccelerateInterpolator(res, theme, attrs); |
| 416 |
} else if (name.equals("decelerateInterpolator")) { |
| 417 |
interpolator = new DecelerateInterpolator(res, theme, attrs); |
| 418 |
} else if (name.equals("accelerateDecelerateInterpolator")) { |
| 419 |
interpolator = new AccelerateDecelerateInterpolator(); |
| 420 |
} else if (name.equals("cycleInterpolator")) { |
| 421 |
interpolator = new CycleInterpolator(res, theme, attrs); |
| 422 |
} else if (name.equals("anticipateInterpolator")) { |
| 423 |
interpolator = new AnticipateInterpolator(res, theme, attrs); |
| 424 |
} else if (name.equals("overshootInterpolator")) { |
| 425 |
interpolator = new OvershootInterpolator(res, theme, attrs); |
| 426 |
} else if (name.equals("anticipateOvershootInterpolator")) { |
| 427 |
interpolator = new AnticipateOvershootInterpolator(res, theme, attrs); |
| 428 |
} else if (name.equals("bounceInterpolator")) { |
| 429 |
interpolator = new BounceInterpolator(); |
| 430 |
} else if (name.equals("pathInterpolator")) { |
| 431 |
interpolator = new PathInterpolator(res, theme, attrs); |
| 432 |
} else { |
| 433 |
throw new RuntimeException("Unknown interpolator name: " + parser.getName()); |
| 434 |
} |
| 435 |
} |
| 436 |
return interpolator; |
| 437 |
} |
| 438 |
} |