常用的一些方法和属性
- setInAnimation:设置View进入屏幕时使用的动画 -- android:inAnimation
- setOutAnimation:设置View退出屏幕时使用的动画 -- android:outAnimation
- showNext:调用该方法来显示ViewFlipper里的下一个View
- showPrevious:调用该方法来显示ViewFlipper的上一个View
- setFilpInterval:设置View之间切换的时间间隔 -- android:flipInterval
- startFlipping:使用上面设置的时间间隔来开始切换所有的View,切换会循环进行
- stopFlipping:停止View切换
效果如下(手势滑动):
1、四个切换动画效果xml(在res目录下创建anim文件夹,用来放动画效果xml)
right_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="2000" android:fromXDelta="100%p" android:toXDelta="0" /> </set>
right_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="2000" android:fromXDelta="0" android:toXDelta="-100%p" /> </set>
left_in.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="2000" android:fromXDelta="-100%p" android:toXDelta="0" /> </set>
left_out.xml
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <translate android:duration="2000" android:fromXDelta="0" android:toXDelta="100%p" /> </set>
2、activity_main.xml布局如下:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <ViewFlipper android:id="@+id/main_vf" android:layout_width="match_parent" android:layout_height="match_parent" android:inAnimation="@anim/right_in" android:outAnimation="@anim/right_out" android:flipInterval="3000"></ViewFlipper> </LinearLayout>
3、MainActivity.java代码如下:
备注:主要是自定义一个手势类接口来继承GestureListener接口,并实现其中的onFling( )方法,然后重写onTouchEvent触发MyGestureListener里的方法。
package com.crazyboy.viewflipperdemo; import android.content.Context; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.LayoutInflater; import android.view.MotionEvent; import android.view.View; import android.widget.ImageView; import android.widget.ViewFlipper; public class MainActivity extends AppCompatActivity { private Context mContext; private ViewFlipper vflp_help; private int[] resId = {R.mipmap.ic_help_view_1,R.mipmap.ic_help_view_2, R.mipmap.ic_help_view_3,R.mipmap.ic_help_view_4}; private final static int MIN_MOVE = 200; //最小距离 private MyGestureListener mgListener; private GestureDetector mDetector; //手势检测类 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = MainActivity.this; //实例化SimpleOnGestureListener与GestureDetector对象 mgListener = new MyGestureListener(); mDetector = new GestureDetector(this, mgListener); vflp_help = findViewById(R.id.main_vf); //动态导入添加子View for(int i = 0;i < resId.length;i++){ vflp_help.addView(getImageView(resId[i])); } } @Override public boolean onTouchEvent(MotionEvent event) { return mDetector.onTouchEvent(event); } private ImageView getImageView(int resId){ ImageView img = new ImageView(this); img.setBackgroundResource(resId); return img; } //自定义一个GestureListener,这个是View类下的,别写错哦!!! private class MyGestureListener extends GestureDetector.SimpleOnGestureListener { @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float v, float v1) { if(e1.getX() - e2.getX() > MIN_MOVE){ vflp_help.setInAnimation(mContext,R.anim.right_in); vflp_help.setOutAnimation(mContext, R.anim.right_out); vflp_help.showNext(); }else if(e2.getX() - e1.getX() > MIN_MOVE){ vflp_help.setInAnimation(mContext,R.anim.left_in); vflp_help.setOutAnimation(mContext, R.anim.left_out); vflp_help.showPrevious(); } return true; } } //这个方法用于显示自动切换的效果 public void viewFlipperNormalShow() { ViewFlipper viewFlipper = findViewById(R.id.main_vf); View viewOne = LayoutInflater.from(this).inflate(R.layout.viewflipper_view_one,null); View viewTwo = LayoutInflater.from(this).inflate(R.layout.viewflipper_view_two,null); View viewThree = LayoutInflater.from(this).inflate(R.layout.viewflipper_view_three,null); View viewFour = LayoutInflater.from(this).inflate(R.layout.viewflipper_view_four,null); viewFlipper.addView(viewOne); viewFlipper.addView(viewTwo); viewFlipper.addView(viewThree); viewFlipper.addView(viewFour); viewFlipper.startFlipping(); } }