blob: f5e39bef3d638e65c80a683b3f11ce29dc92742a [file] [log] [blame]
Tony Mantlerd1e32722015-11-17 15:14:00 -08001/*
2 * Copyright (C) 2015 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.android.settingslib.widget;
18
19import android.content.Context;
20import android.graphics.drawable.AnimatedRotateDrawable;
21import android.graphics.drawable.Drawable;
22import android.util.AttributeSet;
23import android.view.View;
24import android.widget.ImageView;
25
26public class AnimatedImageView extends ImageView {
27 private AnimatedRotateDrawable mDrawable;
28 private boolean mAnimating;
29
30 public AnimatedImageView(Context context) {
31 super(context);
32 }
33
34 public AnimatedImageView(Context context, AttributeSet attrs) {
35 super(context, attrs);
36 }
37
38 private void updateDrawable() {
39 if (isShown() && mDrawable != null) {
40 mDrawable.stop();
41 }
42 final Drawable drawable = getDrawable();
43 if (drawable instanceof AnimatedRotateDrawable) {
44 mDrawable = (AnimatedRotateDrawable) drawable;
45 // TODO: define in drawable xml once we have public attrs.
46 mDrawable.setFramesCount(56);
47 mDrawable.setFramesDuration(32);
48 if (isShown() && mAnimating) {
49 mDrawable.start();
50 }
51 } else {
52 mDrawable = null;
53 }
54 }
55
56 private void updateAnimating() {
57 if (mDrawable != null) {
58 if (isShown() && mAnimating) {
59 mDrawable.start();
60 } else {
61 mDrawable.stop();
62 }
63 }
64 }
65
66 @Override
67 public void setImageDrawable(Drawable drawable) {
68 super.setImageDrawable(drawable);
69 updateDrawable();
70 }
71
72 @Override
73 public void setImageResource(int resid) {
74 super.setImageResource(resid);
75 updateDrawable();
76 }
77
78 @Override
79 public void onAttachedToWindow() {
80 super.onAttachedToWindow();
81 updateAnimating();
82 }
83
84 @Override
85 public void onDetachedFromWindow() {
86 super.onDetachedFromWindow();
87 updateAnimating();
88 }
89
90 public void setAnimating(boolean animating) {
91 mAnimating = animating;
92 updateAnimating();
93 }
94
95 @Override
96 protected void onVisibilityChanged(View changedView, int vis) {
97 super.onVisibilityChanged(changedView, vis);
98 updateAnimating();
99 }
100}