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 17 package android.widget; 18 19 20 import android.content.Context; 21 import android.content.res.TypedArray; 22 import android.os.Handler; 23 import android.os.Message; 24 import android.util.AttributeSet; 25 import 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 */ 34 public class ViewFlipper extends ViewAnimator { 35 private int mFlipInterval = 3000; 36 private boolean mKeepFlipping = false; 37 ViewFlipper(Context context)38 public ViewFlipper(Context context) { 39 super(context); 40 } 41 ViewFlipper(Context context, AttributeSet attrs)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 setFlipInterval(int milliseconds)59 public void setFlipInterval(int milliseconds) { 60 mFlipInterval = milliseconds; 61 } 62 63 /** 64 * Start a timer to cycle through child views 65 */ startFlipping()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 */ stopFlipping()78 public void stopFlipping() { 79 mKeepFlipping = false; 80 } 81 82 /** 83 * Returns true if the child views are flipping. 84 */ isFlipping()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