ViewFlipper实现翻页效果

package org.hjw.flipper;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;

public class Flipper extends Activity implements OnGestureListener {

    ViewFlipper flipper;
    GestureDetector detector;
    Animation[] animations = new Animation[4];
    final int FLIP_DISTANCE = 50;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_flipper);

        detector = new GestureDetector(this, this);
        flipper = (ViewFlipper) findViewById(R.id.flipper);
        flipper.addView(addImageView(R.drawable.cat));
        flipper.addView(addImageView(R.drawable.crazy));
        flipper.addView(addImageView(R.drawable.gz));

        animations[0] = AnimationUtils.loadAnimation(this, R.anim.left_in);
        animations[1] = AnimationUtils.loadAnimation(this, R.anim.left_out);
        animations[2] = AnimationUtils.loadAnimation(this, R.anim.right_in);
        animations[3] = AnimationUtils.loadAnimation(this, R.anim.right_out);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.flipper, menu);
        return true;
    }

    private View addImageView(int resid) {
        ImageView imageView = new ImageView(this);
        imageView.setImageResource(resid);
        imageView.setScaleType(ImageView.ScaleType.CENTER);

        return imageView;
    }

    public boolean onTouchEvent(MotionEvent event) {
        return detector.onTouchEvent(event);

    }

    @Override
    public boolean onDown(MotionEvent e) {

        return false;
    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {
        if (e1.getX() - e2.getX() > FLIP_DISTANCE) {
            flipper.setInAnimation(animations[0]);
            flipper.setOutAnimation(animations[1]);
            flipper.showPrevious();
            return true;
        } else if (e1.getX() - e2.getX() <FLIP_DISTANCE) {
            flipper.setInAnimation(animations[2]);
            flipper.setOutAnimation(animations[2]);
            flipper.showNext();
            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;
    }

}

ps.1.实现OnGestureListener接口,将OnTouch事件交给GestureDetector处理

     2.判断滑动事件的关键就是,判断第一个触点事件与第二个触点事件的x距离是否大于或小于距离事件,从而判断是向右滑还是向左滑

 

你可能感兴趣的:(viewflipper)