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
17package com.example.android.apis.graphics;
18
19import android.graphics.Canvas;
20import android.graphics.drawable.Drawable;
21import android.view.animation.Animation;
22import android.view.animation.AnimationUtils;
23import android.view.animation.Transformation;
24
25public class AnimateDrawable extends ProxyDrawable {
26
27    private Animation mAnimation;
28    private Transformation mTransformation = new Transformation();
29
30    public AnimateDrawable(Drawable target) {
31        super(target);
32    }
33
34    public AnimateDrawable(Drawable target, Animation animation) {
35        super(target);
36        mAnimation = animation;
37    }
38
39    public Animation getAnimation() {
40        return mAnimation;
41    }
42
43    public void setAnimation(Animation anim) {
44        mAnimation = anim;
45    }
46
47    public boolean hasStarted() {
48        return mAnimation != null && mAnimation.hasStarted();
49    }
50
51    public boolean hasEnded() {
52        return mAnimation == null || mAnimation.hasEnded();
53    }
54
55    @Override
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