ViewFlipper.java revision 9066cfe9886ac131c34d59ed0e2d287b0e3c0087
1/*
2 * Copyright (C) 2006 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.widget;
18
19
20import android.content.Context;
21import android.content.res.TypedArray;
22import android.os.Handler;
23import android.os.Message;
24import android.util.AttributeSet;
25import android.widget.RemoteViews.RemoteView;
26
27/**
28 * Simple {@link ViewAnimator} that will animate between two or more views
29 * that have been added to it.  Only one child is shown at a time.  If
30 * requested, can automatically flip between each child at a regular interval.
31 *
32 * @attr ref android.R.styleable#ViewFlipper_flipInterval
33 */
34public class ViewFlipper extends ViewAnimator {
35    private int mFlipInterval = 3000;
36    private boolean mKeepFlipping = false;
37
38    public ViewFlipper(Context context) {
39        super(context);
40    }
41
42    public ViewFlipper(Context context, AttributeSet attrs) {
43        super(context, attrs);
44
45        TypedArray a = context.obtainStyledAttributes(attrs,
46                com.android.internal.R.styleable.ViewFlipper);
47        mFlipInterval = a.getInt(com.android.internal.R.styleable.ViewFlipper_flipInterval,
48                3000);
49        a.recycle();
50    }
51
52    /**
53     * How long to wait before flipping to the next view
54     *
55     * @param milliseconds
56     *            time in milliseconds
57     */
58    @android.view.RemotableViewMethod
59    public void setFlipInterval(int milliseconds) {
60        mFlipInterval = milliseconds;
61    }
62
63    /**
64     * Start a timer to cycle through child views
65     */
66    public void startFlipping() {
67        if (!mKeepFlipping) {
68            mKeepFlipping = true;
69            showOnly(mWhichChild);
70            Message msg = mHandler.obtainMessage(FLIP_MSG);
71            mHandler.sendMessageDelayed(msg, mFlipInterval);
72        }
73    }
74
75    /**
76     * No more flips
77     */
78    public void stopFlipping() {
79        mKeepFlipping = false;
80    }
81
82    /**
83     * Returns true if the child views are flipping.
84     */
85    public boolean isFlipping() {
86        return mKeepFlipping;
87    }
88
89    private final int FLIP_MSG = 1;
90
91    private final Handler mHandler = new Handler() {
92        @Override
93        public void handleMessage(Message msg) {
94            if (msg.what == FLIP_MSG) {
95                if (mKeepFlipping) {
96                    showNext();
97                    msg = obtainMessage(FLIP_MSG);
98                    sendMessageDelayed(msg, mFlipInterval);
99                }
100            }
101        }
102    };
103}
104