android 使用viewflipper实现左右滑动的效果



public
class MainActivity extends Activity implements OnGestureListener { private static final String TAG = "MainActivity"; private ViewFlipper viewFlipper; private GestureDetector detector; //手势检测 Animation leftInAnimation; Animation leftOutAnimation; Animation rightInAnimation; Animation rightOutAnimation; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.activity_main); viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper); detector = new GestureDetector(this); //往viewFlipper添加View viewFlipper.addView(getImageView(R.drawable.new_feature_1)); viewFlipper.addView(getImageView(R.drawable.new_feature_2)); viewFlipper.addView(getImageView(R.drawable.new_feature_3)); viewFlipper.addView(getImageView(R.drawable.new_feature_4)); viewFlipper.addView(getImageView(R.drawable.new_feature_5)); viewFlipper.addView(getImageView(R.drawable.new_feature_6)); //动画效果 leftInAnimation = AnimationUtils.loadAnimation(this, R.anim.left_in); leftOutAnimation = AnimationUtils.loadAnimation(this, R.anim.left_out); rightInAnimation = AnimationUtils.loadAnimation(this, R.anim.right_in); rightOutAnimation = AnimationUtils.loadAnimation(this, R.anim.right_out); } private ImageView getImageView(int id){ ImageView imageView = new ImageView(this); imageView.setImageResource(id); return imageView; } @Override public boolean onTouchEvent(MotionEvent event) { return this.detector.onTouchEvent(event); //touch事件交给手势处理。 } @Override public boolean onDown(MotionEvent e) { // TODO Auto-generated method stub return true; } @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Log.i(TAG, "e1="+e1.getX()+" e2="+e2.getX()+" e1-e2="+(e1.getX()-e2.getX())); if(e1.getX()-e2.getX()>120){ viewFlipper.setInAnimation(leftInAnimation); viewFlipper.setOutAnimation(leftOutAnimation); viewFlipper.showNext();//向右滑动 return true; }else if(e1.getX()-e2.getY()<-120){ viewFlipper.setInAnimation(rightInAnimation); viewFlipper.setOutAnimation(rightOutAnimation); viewFlipper.showPrevious();//向左滑动 return true; } return false; } @Override public void onLongPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { // TODO Auto-generated method stub return false; } @Override public void onShowPress(MotionEvent e) { // TODO Auto-generated method stub } @Override public boolean onSingleTapUp(MotionEvent e) { // TODO Auto-generated method stub return false; } }

布局文件如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

   <ViewFlipper 
       android:id="@+id/viewFlipper"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       ></ViewFlipper>

</RelativeLayout>

效果图如下:

android 使用viewflipper实现左右滑动的效果

android 使用viewflipper实现左右滑动的效果

总体上效果和viewpager一致,好处是不用考虑viewpager的预加载情况,不好的一点是没有viewpager页面切换过程中的动画效果

 

动画效果布局如下:

 left_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="100%p"
        android:toXDelta="0"
        android:duration="600"
        />
    <alpha 
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="600"
        />

</set>

left_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0"
        android:toXDelta="-100%p"
        android:duration="600"
        />
    <alpha 
        android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration="600"
        />

</set>

right_in.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="-100%p"
        android:toXDelta="0"
        android:duration="600"
        />
    <alpha 
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="600"
        />

</set>

right_out.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate 
        android:fromXDelta="0"
        android:toXDelta="100%p"
        android:duration="600"
        />
    <alpha 
        android:fromAlpha="1.0"
        android:toAlpha="0.1"
        android:duration="600"
        />

</set>

android 使用viewflipper实现左右滑动的效果,布布扣,bubuko.com

android 使用viewflipper实现左右滑动的效果

上一篇:iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)


下一篇:windows 下安装 composer