ViewFipper 使用

android.widget.ViewAnimator类继承至FrameLayout,ViewAnimator类的作用是为FrameLayout里面的View切换提供动画效果。该类有如下几个和动画相关的函数:

l setInAnimation:设置View进入屏幕时候使用的动画,该函数有两个版本,一个接受单个参数,类型为android.view.animation.Animation;一个接受两个参数,类型为Context和int,分别为Context对象和定义Animation的resourceID。

  • setOutAnimation: 设置View退出屏幕时候使用的动画,参数setInAnimation函数一样。
  • showNext: 调用该函数来显示FrameLayout里面的下一个View。
  • showPrevious: 调用该函数来显示FrameLayout里面的上一个View。

一般不直接使用ViewAnimator而是使用它的两个子类ViewFlipper和ViewSwitcher。ViewFlipper可以用来指定FrameLayout内多个View之间的切换效果,可以一次指定也可以每次切换的时候都指定单独的效果。该类额外提供了如下几个函数:

  • isFlipping: 用来判断View切换是否正在进行
  • setFilpInterval:设置View之间切换的时间间隔
  • startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行
  • stopFlipping: 停止View切换

ViewSwitcher 顾名思义Switcher特指在两个View之间切换。可以通过该类指定一个ViewSwitcher.ViewFactory 工程类来创建这两个View。该类也具有两个子类ImageSwitcher、TextSwitcher分别用于图片和文本切换。

开始写个例子:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:text="@string/hello" />
    <ViewFlipper android:id="@+id/viewflipper"
        android:layout_width="fill_parent" android:layout_height="wrap_content">
    </ViewFlipper>
 
 
    <ViewSwitcher android:layout_height="wrap_content"
        android:layout_width="fill_parent" android:id="@+id/ButtonViewSwitcher">
        <LinearLayout android:layout_width="fill_parent"
            android:layout_height="wrap_content" android:gravity="center"
            android:padding="2dp" android:background="@android:color/darker_gray"
            android:id="@+id/ButtonLayout">
            
            <Button android:id="@+id/button1" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="上一个"
                android:layout_width="fill_parent" />
                
            <Button android:id="@+id/button2" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="btn2"
                android:layout_width="fill_parent" />
                
            <Button android:id="@+id/button3" android:layout_height="wrap_content"
                android:layout_weight="1" android:text="下一个"
                android:layout_width="fill_parent" />
 
        </LinearLayout>    
    </ViewSwitcher>
</LinearLayout>

public class ViewFipperTest extends Activity implements OnClickListener {
 
    private Button btn1, btn2, btn3;
 
    private ViewFlipper flipper;
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
 
        _initUICtrls();
 
    }
 
    private void _initUICtrls() {
        btn1 = (Button) findViewById(R.id.button1);
        btn1.setOnClickListener(this);
 
        btn2 = (Button) findViewById(R.id.button2);
        btn2.setOnClickListener(this);
 
        btn3 = (Button) findViewById(R.id.button3);
        btn3.setOnClickListener(this);
 
        flipper = (ViewFlipper) findViewById(R.id.viewflipper);
 
        flipper.addView(addButtonByText("Start"), new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        flipper.addView(addTextByText("gangwangchina"));
        flipper.addView(addImageById(R.drawable.icon));
        flipper.addView(addTextByText("Fushan Liaoli"));
        flipper.addView(addImageById(R.drawable.icon));
        flipper.addView(addButtonByText("End"), new LayoutParams(
                LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
        
//        flipper.setInAnimation(AnimationUtils.loadAnimation(this,  
//                android.R.anim.fade_in));  
//        flipper.setOutAnimation(AnimationUtils.loadAnimation(this,  
//                android.R.anim.fade_out));
        
       
    }
 
    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){  
        case R.id.button1:  
            flipper.setInAnimation(AnimationHelper.inFromLeftAnimation());
            flipper.setOutAnimation(AnimationHelper.outToRightAnimation());
             
            flipper.showPrevious();  
            
            break;  
              
        case R.id.button3:  
            
            
            flipper.setInAnimation(AnimationHelper.inFromRightAnimation());
            flipper.setOutAnimation(AnimationHelper.outToLeftAnimation());
            
            flipper.showNext();  
            break;  
        }  
 
    }
 
    public View addTextByText(String text) {
        TextView tv = new TextView(this);
        tv.setText(text);
        tv.setGravity(1);
        return tv;
    }
 
    public View addImageById(int id) {
        ImageView iv = new ImageView(this);
        iv.setImageResource(id);
 
        return iv;
    }
 
    public View addButtonByText(String text) {
        Button btn = new Button(this);
        btn.setText(text);
        return btn;
    }
    
    private static class AnimationHelper {
        public static Animation inFromRightAnimation() {
          Animation inFromRight = new TranslateAnimation(
          Animation.RELATIVE_TO_PARENT, +1.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f);
          inFromRight.setDuration(350);
          inFromRight.setInterpolator(new AccelerateInterpolator());
          return inFromRight;
        }
 
        public static Animation outToLeftAnimation() {
          Animation outtoLeft = new TranslateAnimation(
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, -1.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f);
          outtoLeft.setDuration(350);
          outtoLeft.setInterpolator(new AccelerateInterpolator());
          return outtoLeft;
        }
 
        public static Animation inFromLeftAnimation() {
          Animation inFromLeft = new TranslateAnimation(
          Animation.RELATIVE_TO_PARENT, -1.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f);
          inFromLeft.setDuration(350);
          inFromLeft.setInterpolator(new AccelerateInterpolator());
          return inFromLeft;
        }
 
        public static Animation outToRightAnimation() {
          Animation outtoRight = new TranslateAnimation(
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, +1.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f,
          Animation.RELATIVE_TO_PARENT, 0.0f);
          outtoRight.setDuration(350);
          outtoRight.setInterpolator(new AccelerateInterpolator());
          return outtoRight;
        }
      }
}

里面的AnimationHelper 类是用来处理切换效果的。

实际效果如下:

device

device1

原文地址:https://www.cnblogs.com/GnagWang/p/1966837.html