AnimatedRotateDrawable.java revision cda212d79d449468384cc7744878b8c99984059c
1/*
2 * Copyright (C) 2009 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 android.graphics.drawable;
18
19import android.graphics.Canvas;
20import android.graphics.Rect;
21import android.graphics.ColorFilter;
22import android.content.res.Resources;
23import android.content.res.TypedArray;
24import android.content.res.Resources.Theme;
25import android.util.AttributeSet;
26import android.util.TypedValue;
27import android.util.Log;
28import android.os.SystemClock;
29
30import org.xmlpull.v1.XmlPullParser;
31import org.xmlpull.v1.XmlPullParserException;
32
33import java.io.IOException;
34
35import com.android.internal.R;
36
37/**
38 * @hide
39 */
40public class AnimatedRotateDrawable extends Drawable implements Drawable.Callback, Runnable,
41        Animatable {
42
43    private AnimatedRotateState mState;
44    private boolean mMutated;
45    private float mCurrentDegrees;
46    private float mIncrement;
47    private boolean mRunning;
48
49    public AnimatedRotateDrawable() {
50        this(null, null);
51    }
52
53    private AnimatedRotateDrawable(AnimatedRotateState rotateState, Resources res) {
54        mState = new AnimatedRotateState(rotateState, this, res);
55        init();
56    }
57
58    private void init() {
59        final AnimatedRotateState state = mState;
60        mIncrement = 360.0f / state.mFramesCount;
61        final Drawable drawable = state.mDrawable;
62        if (drawable != null) {
63            drawable.setFilterBitmap(true);
64            if (drawable instanceof BitmapDrawable) {
65                ((BitmapDrawable) drawable).setAntiAlias(true);
66            }
67        }
68    }
69
70    @Override
71    public void draw(Canvas canvas) {
72        int saveCount = canvas.save();
73
74        final AnimatedRotateState st = mState;
75        final Drawable drawable = st.mDrawable;
76        final Rect bounds = drawable.getBounds();
77
78        int w = bounds.right - bounds.left;
79        int h = bounds.bottom - bounds.top;
80
81        float px = st.mPivotXRel ? (w * st.mPivotX) : st.mPivotX;
82        float py = st.mPivotYRel ? (h * st.mPivotY) : st.mPivotY;
83
84        canvas.rotate(mCurrentDegrees, px + bounds.left, py + bounds.top);
85
86        drawable.draw(canvas);
87
88        canvas.restoreToCount(saveCount);
89    }
90
91    public void start() {
92        if (!mRunning) {
93            mRunning = true;
94            nextFrame();
95        }
96    }
97
98    public void stop() {
99        mRunning = false;
100        unscheduleSelf(this);
101    }
102
103    public boolean isRunning() {
104        return mRunning;
105    }
106
107    private void nextFrame() {
108        unscheduleSelf(this);
109        scheduleSelf(this, SystemClock.uptimeMillis() + mState.mFrameDuration);
110    }
111
112    public void run() {
113        // TODO: This should be computed in draw(Canvas), based on the amount
114        // of time since the last frame drawn
115        mCurrentDegrees += mIncrement;
116        if (mCurrentDegrees > (360.0f - mIncrement)) {
117            mCurrentDegrees = 0.0f;
118        }
119        invalidateSelf();
120        nextFrame();
121    }
122
123    @Override
124    public boolean setVisible(boolean visible, boolean restart) {
125        mState.mDrawable.setVisible(visible, restart);
126        boolean changed = super.setVisible(visible, restart);
127        if (visible) {
128            if (changed || restart) {
129                mCurrentDegrees = 0.0f;
130                nextFrame();
131            }
132        } else {
133            unscheduleSelf(this);
134        }
135        return changed;
136    }
137
138    /**
139     * Returns the drawable rotated by this RotateDrawable.
140     */
141    public Drawable getDrawable() {
142        return mState.mDrawable;
143    }
144
145    @Override
146    public int getChangingConfigurations() {
147        return super.getChangingConfigurations()
148                | mState.mChangingConfigurations
149                | mState.mDrawable.getChangingConfigurations();
150    }
151
152    @Override
153    public void setAlpha(int alpha) {
154        mState.mDrawable.setAlpha(alpha);
155    }
156
157    @Override
158    public int getAlpha() {
159        return mState.mDrawable.getAlpha();
160    }
161
162    @Override
163    public void setColorFilter(ColorFilter cf) {
164        mState.mDrawable.setColorFilter(cf);
165    }
166
167    @Override
168    public int getOpacity() {
169        return mState.mDrawable.getOpacity();
170    }
171
172    public void invalidateDrawable(Drawable who) {
173        final Callback callback = getCallback();
174        if (callback != null) {
175            callback.invalidateDrawable(this);
176        }
177    }
178
179    public void scheduleDrawable(Drawable who, Runnable what, long when) {
180        final Callback callback = getCallback();
181        if (callback != null) {
182            callback.scheduleDrawable(this, what, when);
183        }
184    }
185
186    public void unscheduleDrawable(Drawable who, Runnable what) {
187        final Callback callback = getCallback();
188        if (callback != null) {
189            callback.unscheduleDrawable(this, what);
190        }
191    }
192
193    @Override
194    public boolean getPadding(Rect padding) {
195        return mState.mDrawable.getPadding(padding);
196    }
197
198    @Override
199    public boolean isStateful() {
200        return mState.mDrawable.isStateful();
201    }
202
203    @Override
204    protected void onBoundsChange(Rect bounds) {
205        mState.mDrawable.setBounds(bounds.left, bounds.top, bounds.right, bounds.bottom);
206    }
207
208    @Override
209    public int getIntrinsicWidth() {
210        return mState.mDrawable.getIntrinsicWidth();
211    }
212
213    @Override
214    public int getIntrinsicHeight() {
215        return mState.mDrawable.getIntrinsicHeight();
216    }
217
218    @Override
219    public ConstantState getConstantState() {
220        if (mState.canConstantState()) {
221            mState.mChangingConfigurations = getChangingConfigurations();
222            return mState;
223        }
224        return null;
225    }
226
227    @Override
228    public void inflate(Resources r, XmlPullParser parser, AttributeSet attrs, Theme theme)
229            throws XmlPullParserException, IOException {
230
231        final TypedArray a = r.obtainAttributes(attrs, R.styleable.AnimatedRotateDrawable);
232
233        super.inflateWithAttributes(r, parser, a, R.styleable.AnimatedRotateDrawable_visible);
234
235        TypedValue tv = a.peekValue(R.styleable.AnimatedRotateDrawable_pivotX);
236        final boolean pivotXRel = tv.type == TypedValue.TYPE_FRACTION;
237        final float pivotX = pivotXRel ? tv.getFraction(1.0f, 1.0f) : tv.getFloat();
238
239        tv = a.peekValue(R.styleable.AnimatedRotateDrawable_pivotY);
240        final boolean pivotYRel = tv.type == TypedValue.TYPE_FRACTION;
241        final float pivotY = pivotYRel ? tv.getFraction(1.0f, 1.0f) : tv.getFloat();
242
243        setFramesCount(a.getInt(R.styleable.AnimatedRotateDrawable_framesCount, 12));
244        setFramesDuration(a.getInt(R.styleable.AnimatedRotateDrawable_frameDuration, 150));
245
246        final int res = a.getResourceId(R.styleable.AnimatedRotateDrawable_drawable, 0);
247        Drawable drawable = null;
248        if (res > 0) {
249            drawable = r.getDrawable(res);
250        }
251
252        a.recycle();
253
254        int outerDepth = parser.getDepth();
255        int type;
256        while ((type = parser.next()) != XmlPullParser.END_DOCUMENT &&
257               (type != XmlPullParser.END_TAG || parser.getDepth() > outerDepth)) {
258
259            if (type != XmlPullParser.START_TAG) {
260                continue;
261            }
262
263            if ((drawable = Drawable.createFromXmlInner(r, parser, attrs, theme)) == null) {
264                Log.w("drawable", "Bad element under <animated-rotate>: "
265                        + parser .getName());
266            }
267        }
268
269        if (drawable == null) {
270            Log.w("drawable", "No drawable specified for <animated-rotate>");
271        }
272
273        final AnimatedRotateState rotateState = mState;
274        rotateState.mDrawable = drawable;
275        rotateState.mPivotXRel = pivotXRel;
276        rotateState.mPivotX = pivotX;
277        rotateState.mPivotYRel = pivotYRel;
278        rotateState.mPivotY = pivotY;
279
280        init();
281
282        if (drawable != null) {
283            drawable.setCallback(this);
284        }
285    }
286
287    public void setFramesCount(int framesCount) {
288        mState.mFramesCount = framesCount;
289        mIncrement = 360.0f / mState.mFramesCount;
290    }
291
292    public void setFramesDuration(int framesDuration) {
293        mState.mFrameDuration = framesDuration;
294    }
295
296    @Override
297    public Drawable mutate() {
298        if (!mMutated && super.mutate() == this) {
299            mState.mDrawable.mutate();
300            mMutated = true;
301        }
302        return this;
303    }
304
305    final static class AnimatedRotateState extends Drawable.ConstantState {
306        Drawable mDrawable;
307
308        int mChangingConfigurations;
309
310        boolean mPivotXRel;
311        float mPivotX;
312        boolean mPivotYRel;
313        float mPivotY;
314        int mFrameDuration;
315        int mFramesCount;
316
317        private boolean mCanConstantState;
318        private boolean mCheckedConstantState;
319
320        public AnimatedRotateState(AnimatedRotateState source, AnimatedRotateDrawable owner,
321                Resources res) {
322            if (source != null) {
323                if (res != null) {
324                    mDrawable = source.mDrawable.getConstantState().newDrawable(res);
325                } else {
326                    mDrawable = source.mDrawable.getConstantState().newDrawable();
327                }
328                mDrawable.setCallback(owner);
329                mDrawable.setLayoutDirection(source.mDrawable.getLayoutDirection());
330                mPivotXRel = source.mPivotXRel;
331                mPivotX = source.mPivotX;
332                mPivotYRel = source.mPivotYRel;
333                mPivotY = source.mPivotY;
334                mFramesCount = source.mFramesCount;
335                mFrameDuration = source.mFrameDuration;
336                mCanConstantState = mCheckedConstantState = true;
337            }
338        }
339
340        @Override
341        public Drawable newDrawable() {
342            return new AnimatedRotateDrawable(this, null);
343        }
344
345        @Override
346        public Drawable newDrawable(Resources res) {
347            return new AnimatedRotateDrawable(this, res);
348        }
349
350        @Override
351        public int getChangingConfigurations() {
352            return mChangingConfigurations;
353        }
354
355        public boolean canConstantState() {
356            if (!mCheckedConstantState) {
357                mCanConstantState = mDrawable.getConstantState() != null;
358                mCheckedConstantState = true;
359            }
360
361            return mCanConstantState;
362        }
363    }
364}
365