1、ViewFlipper
1)View切换的控件—ViewFlipper介绍
ViewFilpper类继承于ViewAnimator类。而ViewAnimator类继承于FrameLayout。
查看ViewAnimator类的源码可以看出此类的作用主要是为其中的View切换提供动画效果。该类有如下几个和动画相关的方法。
setInAnimation:设置View进入屏幕时候使用的动画。该方法有两个重载方法,即可以直接传入Animation对象,也可以传入定义的Animation文件的resourceID。
setOutAnimation:设置View退出屏幕时候使用的动画。使用方法和setInAnimation方法一样。
showNext:调用该方法可以显示FrameLayout里面的下一个View。
showPrevious:调用该方法可以来显示FrameLayout里面的上一个View。
查看ViewFlipper的源码可以看到,ViewFlipper主要用来实现View的自动切换。该类提供了如下几个主要的方法。
setFilpInterval:设置View切换的时间间隔。参数为毫秒。
startFlipping:开始进行View的切换,时间间隔是上述方法设置的间隔数。切换会循环进行。
stopFlipping:停止View切换。
setAutoStart:设置是否自动开始。如果设置为“true”,当ViewFlipper显示的时候View的切换会自动开始。
一般情况下,我们都会使用ViewFilpper类实现View的切换,而不使用它的父类ViewAnimator类。
左右滑动效果图
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.zking.administrator.g160628_android20_gesture.MainActivity"> <ViewFlipper android:layout_width="wrap_content" android:layout_height="match_parent" android:id="@+id/vf_main_image"></ViewFlipper> </LinearLayout> package com.zking.administrator.g160628_android20_gesture; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.GestureDetector; import android.view.MotionEvent; import android.widget.ImageView; import android.widget.Toast; import android.widget.ViewFlipper; public class MainActivity extends AppCompatActivity { private ViewFlipper vf_main_image; private int images[]={R.drawable.t01445,R.drawable.t01a60d77ab08b7ea64,R.drawable.t01aab52acba3061a45}; private GestureDetector gd; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); vf_main_image = (ViewFlipper) findViewById(R.id.vf_main_image); for (int i = 0; i < images.length; i++) { ImageView iv=new ImageView(this); iv.setImageResource(images[i]); //给容器加图片 vf_main_image.addView(iv); } gd = new GestureDetector(this, new GestureDetector.OnGestureListener() { //按下 @Override public boolean onDown(MotionEvent e) { return false; } //按下,但是还未抬起 @Override public void onShowPress(MotionEvent e) { } //轻按,按一下,立刻抬起 @Override public boolean onSingleTapUp(MotionEvent e) { return false; } //滚动 @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { return false; } //长按 @Override public void onLongPress(MotionEvent e) { } //拖动 @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if(e2.getX()-e1.getX()>200){ Toast.makeText(MainActivity.this, "右滑,上一张", Toast.LENGTH_SHORT).show(); vf_main_image.showPrevious();//上一张 //左进右出 vf_main_image.setInAnimation(MainActivity.this, R.anim.left_in); vf_main_image.setOutAnimation(MainActivity.this,R.anim.right_out); } if(e1.getX()-e2.getX()>200){ Toast.makeText(MainActivity.this, "左滑,下一张", Toast.LENGTH_SHORT).show(); vf_main_image.showNext();//下一张 //左出右进 vf_main_image.setInAnimation(MainActivity.this,R.anim.right_in); vf_main_image.setOutAnimation(MainActivity.this,R.anim.left_out); } return false; } }); } //触摸 @Override public boolean onTouchEvent(MotionEvent event) { return gd.onTouchEvent(event); } } <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:duration="1000"> <!--fillAfter="true"保留结束的状态--> <!--从-100%p到0(-100%p就是移到屏幕外面去了)--> <!--左进--> <translate android:fromXDelta="-100%p" android:toXDelta="0" ></translate> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:duration="1000"> <!--fillAfter="true"保留结束的状态--> <!--从0到-100%p(-100%p就是移到屏幕外面去了)--> <!--左出--> <translate android:fromXDelta="0" android:toXDelta="-100%p" ></translate> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:duration="1000"> <!--fillAfter="true"保留结束的状态--> <!--从100%p到0(-100%p就是移到屏幕外面去了)--> <!--右进--> <translate android:fromXDelta="100%p" android:toXDelta="0" ></translate> </set> <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:fillAfter="true" android:duration="1000"> <!--fillAfter="true"保留结束的状态--> <!--从0到100%p(-100%p就是移到屏幕外面去了)--> <!--右出--> <translate android:fromXDelta="0" android:toXDelta="100%p" ></translate> </set>