1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.graphics;
18 
19 import android.graphics.Canvas;
20 import android.graphics.drawable.Drawable;
21 import android.view.animation.Animation;
22 import android.view.animation.AnimationUtils;
23 import android.view.animation.Transformation;
24 
25 public class AnimateDrawable extends ProxyDrawable {
26 
27     private Animation mAnimation;
28     private Transformation mTransformation = new Transformation();
29 
AnimateDrawable(Drawable target)30     public AnimateDrawable(Drawable target) {
31         super(target);
32     }
33 
AnimateDrawable(Drawable target, Animation animation)34     public AnimateDrawable(Drawable target, Animation animation) {
35         super(target);
36         mAnimation = animation;
37     }
38 
getAnimation()39     public Animation getAnimation() {
40         return mAnimation;
41     }
42 
setAnimation(Animation anim)43     public void setAnimation(Animation anim) {
44         mAnimation = anim;
45     }
46 
hasStarted()47     public boolean hasStarted() {
48         return mAnimation != null && mAnimation.hasStarted();
49     }
50 
hasEnded()51     public boolean hasEnded() {
52         return mAnimation == null || mAnimation.hasEnded();
53     }
54 
55     @Override
draw(Canvas canvas)56     public void draw(Canvas canvas) {
57         Drawable dr = getProxy();
58         if (dr != null) {
59             int sc = canvas.save();
60             Animation anim = mAnimation;
61             if (anim != null) {
62                 anim.getTransformation(
63                                     AnimationUtils.currentAnimationTimeMillis(),
64                                     mTransformation);
65                 canvas.concat(mTransformation.getMatrix());
66             }
67             dr.draw(canvas);
68             canvas.restoreToCount(sc);
69         }
70     }
71 }
72 
73