FileDocCategorySizeDatePackage
ViewAnimator.javaAPI DocAndroid 5.1 API12170Thu Mar 12 22:22:10 GMT 2015android.widget

ViewAnimator

public class ViewAnimator extends FrameLayout
Base class for a {@link FrameLayout} container that will perform animations when switching between its views.
attr
ref android.R.styleable#ViewAnimator_inAnimation
attr
ref android.R.styleable#ViewAnimator_outAnimation
attr
ref android.R.styleable#ViewAnimator_animateFirstView

Fields Summary
int
mWhichChild
boolean
mFirstTime
boolean
mAnimateFirstTime
android.view.animation.Animation
mInAnimation
android.view.animation.Animation
mOutAnimation
Constructors Summary
public ViewAnimator(android.content.Context context)


       
        super(context);
        initViewAnimator(context, null);
    
public ViewAnimator(android.content.Context context, android.util.AttributeSet attrs)

        super(context, attrs);

        TypedArray a = context.obtainStyledAttributes(attrs, com.android.internal.R.styleable.ViewAnimator);
        int resource = a.getResourceId(com.android.internal.R.styleable.ViewAnimator_inAnimation, 0);
        if (resource > 0) {
            setInAnimation(context, resource);
        }

        resource = a.getResourceId(com.android.internal.R.styleable.ViewAnimator_outAnimation, 0);
        if (resource > 0) {
            setOutAnimation(context, resource);
        }

        boolean flag = a.getBoolean(com.android.internal.R.styleable.ViewAnimator_animateFirstView, true);
        setAnimateFirstView(flag);

        a.recycle();

        initViewAnimator(context, attrs);
    
Methods Summary
public voidaddView(android.view.View child, int index, ViewGroup.LayoutParams params)

        super.addView(child, index, params);
        if (getChildCount() == 1) {
            child.setVisibility(View.VISIBLE);
        } else {
            child.setVisibility(View.GONE);
        }
        if (index >= 0 && mWhichChild >= index) {
            // Added item above current one, increment the index of the displayed child
            setDisplayedChild(mWhichChild + 1);
        }
    
public booleangetAnimateFirstView()
Returns whether the current View should be animated the first time the ViewAnimator is displayed.

return
true if the current View will be animated the first time it is displayed, false otherwise.
see
#setAnimateFirstView(boolean)

        return mAnimateFirstTime;
    
public intgetBaseline()

        return (getCurrentView() != null) ? getCurrentView().getBaseline() : super.getBaseline();
    
public android.view.ViewgetCurrentView()
Returns the View corresponding to the currently displayed child.

return
The View currently displayed.
see
#getDisplayedChild()

        return getChildAt(mWhichChild);
    
public intgetDisplayedChild()
Returns the index of the currently displayed child view.

        return mWhichChild;
    
public android.view.animation.AnimationgetInAnimation()
Returns the current animation used to animate a View that enters the screen.

return
An Animation or null if none is set.
see
#setInAnimation(android.view.animation.Animation)
see
#setInAnimation(android.content.Context, int)

        return mInAnimation;
    
public android.view.animation.AnimationgetOutAnimation()
Returns the current animation used to animate a View that exits the screen.

return
An Animation or null if none is set.
see
#setOutAnimation(android.view.animation.Animation)
see
#setOutAnimation(android.content.Context, int)

        return mOutAnimation;
    
private voidinitViewAnimator(android.content.Context context, android.util.AttributeSet attrs)
Initialize this {@link ViewAnimator}, possibly setting {@link #setMeasureAllChildren(boolean)} based on {@link FrameLayout} flags.

        if (attrs == null) {
            // For compatibility, always measure children when undefined.
            mMeasureAllChildren = true;
            return;
        }

        // For compatibility, default to measure children, but allow XML
        // attribute to override.
        final TypedArray a = context.obtainStyledAttributes(attrs,
                com.android.internal.R.styleable.FrameLayout);
        final boolean measureAllChildren = a.getBoolean(
                com.android.internal.R.styleable.FrameLayout_measureAllChildren, true);
        setMeasureAllChildren(measureAllChildren);
        a.recycle();
    
public voidonInitializeAccessibilityEvent(android.view.accessibility.AccessibilityEvent event)

        super.onInitializeAccessibilityEvent(event);
        event.setClassName(ViewAnimator.class.getName());
    
public voidonInitializeAccessibilityNodeInfo(android.view.accessibility.AccessibilityNodeInfo info)

        super.onInitializeAccessibilityNodeInfo(info);
        info.setClassName(ViewAnimator.class.getName());
    
public voidremoveAllViews()

        super.removeAllViews();
        mWhichChild = 0;
        mFirstTime = true;
    
public voidremoveView(android.view.View view)

        final int index = indexOfChild(view);
        if (index >= 0) {
            removeViewAt(index);
        }
    
public voidremoveViewAt(int index)

        super.removeViewAt(index);
        final int childCount = getChildCount();
        if (childCount == 0) {
            mWhichChild = 0;
            mFirstTime = true;
        } else if (mWhichChild >= childCount) {
            // Displayed is above child count, so float down to top of stack
            setDisplayedChild(childCount - 1);
        } else if (mWhichChild == index) {
            // Displayed was removed, so show the new child living in its place
            setDisplayedChild(mWhichChild);
        }
    
public voidremoveViewInLayout(android.view.View view)

        removeView(view);
    
public voidremoveViews(int start, int count)

        super.removeViews(start, count);
        if (getChildCount() == 0) {
            mWhichChild = 0;
            mFirstTime = true;
        } else if (mWhichChild >= start && mWhichChild < start + count) {
            // Try showing new displayed child, wrapping if needed
            setDisplayedChild(mWhichChild);
        }
    
public voidremoveViewsInLayout(int start, int count)

        removeViews(start, count);
    
public voidsetAnimateFirstView(boolean animate)
Indicates whether the current View should be animated the first time the ViewAnimator is displayed.

param
animate True to animate the current View the first time it is displayed, false otherwise.

        mAnimateFirstTime = animate;
    
public voidsetDisplayedChild(int whichChild)
Sets which child view will be displayed.

param
whichChild the index of the child view to display

        mWhichChild = whichChild;
        if (whichChild >= getChildCount()) {
            mWhichChild = 0;
        } else if (whichChild < 0) {
            mWhichChild = getChildCount() - 1;
        }
        boolean hasFocus = getFocusedChild() != null;
        // This will clear old focus if we had it
        showOnly(mWhichChild);
        if (hasFocus) {
            // Try to retake focus if we had it
            requestFocus(FOCUS_FORWARD);
        }
    
public voidsetInAnimation(android.view.animation.Animation inAnimation)
Specifies the animation used to animate a View that enters the screen.

param
inAnimation The animation started when a View enters the screen.
see
#getInAnimation()
see
#setInAnimation(android.content.Context, int)

        mInAnimation = inAnimation;
    
public voidsetInAnimation(android.content.Context context, int resourceID)
Specifies the animation used to animate a View that enters the screen.

param
context The application's environment.
param
resourceID The resource id of the animation.
see
#getInAnimation()
see
#setInAnimation(android.view.animation.Animation)

        setInAnimation(AnimationUtils.loadAnimation(context, resourceID));
    
public voidsetOutAnimation(android.view.animation.Animation outAnimation)
Specifies the animation used to animate a View that exit the screen.

param
outAnimation The animation started when a View exit the screen.
see
#getOutAnimation()
see
#setOutAnimation(android.content.Context, int)

        mOutAnimation = outAnimation;
    
public voidsetOutAnimation(android.content.Context context, int resourceID)
Specifies the animation used to animate a View that exit the screen.

param
context The application's environment.
param
resourceID The resource id of the animation.
see
#getOutAnimation()
see
#setOutAnimation(android.view.animation.Animation)

        setOutAnimation(AnimationUtils.loadAnimation(context, resourceID));
    
public voidshowNext()
Manually shows the next child.

        setDisplayedChild(mWhichChild + 1);
    
voidshowOnly(int childIndex, boolean animate)
Shows only the specified child. The other displays Views exit the screen, optionally with the with the {@link #getOutAnimation() out animation} and the specified child enters the screen, optionally with the {@link #getInAnimation() in animation}.

param
childIndex The index of the child to be shown.
param
animate Whether or not to use the in and out animations, defaults to true.

        final int count = getChildCount();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (i == childIndex) {
                if (animate && mInAnimation != null) {
                    child.startAnimation(mInAnimation);
                }
                child.setVisibility(View.VISIBLE);
                mFirstTime = false;
            } else {
                if (animate && mOutAnimation != null && child.getVisibility() == View.VISIBLE) {
                    child.startAnimation(mOutAnimation);
                } else if (child.getAnimation() == mInAnimation)
                    child.clearAnimation();
                child.setVisibility(View.GONE);
            }
        }
    
voidshowOnly(int childIndex)
Shows only the specified child. The other displays Views exit the screen with the {@link #getOutAnimation() out animation} and the specified child enters the screen with the {@link #getInAnimation() in animation}.

param
childIndex The index of the child to be shown.

        final boolean animate = (!mFirstTime || mAnimateFirstTime);
        showOnly(childIndex, animate);
    
public voidshowPrevious()
Manually shows the previous child.

        setDisplayedChild(mWhichChild - 1);