ViewSwitcher usage example

The role of ViewSwitcher is simply: show animation when transitioning between two views 

Its two subclasses should be familiar, ImageSwitcher : add animation effects when converting pictures TextSwitcher : add animation effects when converting text  

 

 

API translation

1. Structure

 

public class ViewSwitcher extends ViewAnimator

        

java.lang.Object

android.view.View

         android.view.ViewGroup

                   android.widget.FrameLayout

                            android.widget.ViewAnimator

                                     android.widget.ViewSwitcher

known direct subclasses

ImageSwitcherTextSwitcher

 

 

  2. Overview

     To display animations when transitioning between two views, there is a factory class that can create these views. You can use factories to create these views, or you can create them yourself. A ViewSwitcher is only allowed to contain two subviews , and only one can be displayed at a time.

  (Translator's Note: Similar to the ViewFlipper class, but this class is not commonly used, and its two subclasses are commonly used : ImageSwitcher : add animation effects when converting picturesTextSwitcher : add animation effects when converting text )

 

  Third, the inner class

    interface          ViewSwitcher.ViewFactory     

    Create Views in a ViewSwitcher

 

  4. Constructor

public ViewSwitcher (Context context)

    Constructs a new empty ViewSwitcher .

    parameter context   context

public ViewSwitcher (Context context, AttributeSet attrs)

    Constructs an empty ViewSwitcher that specifies the context and property set .

    parameter

    context     context

    attrs         attribute collection

 

5. Public methods

 

public void addView(View child, int index, ViewGroup.LayoutParams params)

   Add a subview specifying layout parameters

            parameter

  child          view added by child

  index        of the added subview

  params     layout parameters of the subview

  abnormal

  IllegalStateException        if the switcher already contains two views.

   

  public View getNextView ()

  Return to the next view to be displayed

  Return value - the next view to be displayed after the view switch

 

  public void reset ()

  Reset the ViewSwitcher to hide all existing views and bring the switcher to a state where one animation has not yet played.

 

  public void setFactory (ViewSwitcher.ViewFactory factory)

  Sets the factory used to generate the two views that will be switched in the view converter. You can also call  addView(android.view.View, int, android.view.ViewGroup.LayoutParams) twice instead of using the factory method.

  The parameter  factory    is used to generate the view factory of the converter content

The following uses a part of the OSChina open source code to demonstrate its specific use

"User login"

layout xml:


 <ViewSwitcher 
        android:id="@+id/logindialog_view_switcher" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content">

        <RelativeLayout 

        layout. . . .

        />

        <View 
           android:id="@+id/login_loading" 
           android:layout_width="135.0dip" 
           android:layout_height="135.0dip"
           android:layout_gravity="center" 
           android:background="@anim/login_loading"/>
</ViewSwitcher>
动画类中:

 

01 Animation 1 11 Animation 2

Use layer overlay (because there are two animation effects )

<!-- Animation frame collection object -->

<?xml version="1.0" encoding="UTF-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  android:oneshot="false">

<!--The animation frame object android:duration represents the time of each frame of animation display, and the animation picture placed under the drawable should not be too large, otherwise the memory will explode-->    
     <item android:duration="100">
        <layer- list>
            <item android:drawable="@drawable/login_loading_00" />
            <item android:drawable="@drawable/login_loading_10" />
        </layer-list>
    </item>

<item android:duration="100">
        <layer-list>
            <item android:drawable="@drawable/login_loading_01" />
            <item android:drawable="@drawable/login_loading_11" />
        </layer-list>
    </item>

    

    <item android:duration="100">
        <layer-list>
            <item android:drawable="@drawable/login_loading_02" />
            <item android:drawable="@drawable/login_loading_12" />
        </layer-list>
    </item>

</animation-list>

in entity class

initialization

private ViewSwitchermViewSwitcher; 

private View loginLoading;

 

loginLoading = (View)findViewById(R.id.login_loading);

mViewSwitcher = (ViewSwitcher)findViewById(R.id.logindialog_view_switcher);

 

loadingAnimation = (AnimationDrawable)loginLoading.getBackground();
       loadingAnimation.start();
       mViewSwitcher .showNext();//When the login is successful

       mViewSwitcher.showPrevious();//When login fails

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=327000570&siteId=291194637