[douban] 01_ViewFipper和手势识别

Android 4.0

ViewFipper和手势识别
ViewFlipper
手势识别:GestureDetector

一、ViewFlipper
1、添加view
vf.addView(view);
2、显示前一个view
vf.showPrevious();
3、显示后一个view
vf.showNext();
4、显示动画
①进入动画
vf.setInAnimation(this, R.anim.vf_in);
②出去动画
vf.setOutAnimation(this, R.anim.vf_out);

二、手势识别
GestureDetector
1、 SimpleOnGestureListener
2、
onFling()滑动事件
3、在Activity中重写
onTouchEvent()将触摸事件传递给手势识别器
mGestureDetector.onTouchEvent(event);
三、完整代码
package cn.zengfansheng.viewflipper;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class MainActivity extends Activity {
    protected static final String TAG = "MainActivity";
    private ViewFlipper vf;
    private GestureDetector mGestureDetector;// /手势识别的帮助类
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        vf = (ViewFlipper) this.findViewById(R.id.vf);
        TextView tv1 = new TextView(this);
        tv1.setText("这是tv1");
        tv1.setBackgroundColor(Color.BLUE);
        ImageView iv2 = new ImageView(this);
        iv2.setImageResource(R.drawable.ic_launcher);
        TextView tv3 = new TextView(this);
        tv3.setText("这是tv3");
        tv3.setBackgroundColor(Color.YELLOW);
        // 添加到ViewFilpper中去
        vf.addView(tv1);
        vf.addView(iv2);
        vf.addView(tv3);
        // 手势识别
        mGestureDetector = new GestureDetector(this,
                new GestureDetector.SimpleOnGestureListener() {
                    @Override
                    public boolean onFling(MotionEvent e1, MotionEvent e2,
                            float velocityX, float velocityY) {
                        // e1:手按下事件
                        // e2:松手事件
                        // velocityX:x轴加速度
                        // velocityY:y轴加速度
                        if (Math.abs(e1.getY() - e2.getY()) > 100) {// //垂直方向变化过大,无效事件
                            Log.i(TAG"手势不对");
                            return false;
                        }
                        if (e1.getX() - e2.getX() > 100 && Math.abs(velocityX)>100) {// 向右滑动
                            showNext();
                            return false;
                        }
                        if (e2.getX() - e1.getX() > 100 && Math.abs(velocityX)>100) {// 向左滑动
                            showPre();
                            return false;
                        }
                        return super.onFling(e1, e2, velocityX, velocityY);//return false,可以继续向下传递
                    }
        });
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // 把当前屏幕触摸的事件 传递个手势识别器
        mGestureDetector.onTouchEvent(event);
        // 有的时候 消费掉 触摸事件,精确处理
        // 控制当前的事件 是否让系统的框架 默认处理
        return super.onTouchEvent(event);
    }
    public void pre(View view) {
        showPre();
    }
    private void showPre() {
        vf.showPrevious();
        vf.setInAnimation(this, R.anim.vf_in);
        vf.setOutAnimation(this, R.anim.vf_out);
    }
    public void next(View view) {
        showNext();
    }
    private void showNext() {
        vf.showNext();
        vf.setInAnimation(this, R.anim.vf_in);
        vf.setOutAnimation(this, R.anim.vf_out);
    }
}
结果: