关闭

Android高级UI GestureDetector监听各种手势

标签: android
231人阅读 评论(0) 收藏 举报
分类:

内容

第一部分:介绍GestureDetector
第二部分:改造之前用ViewFlipper实现的实例,使用GestureDetector实现图片切换功能。

要点

  1. 实现接口OnTouchListener,在onTouch()方法中由GestureDetector接管事件
  2. 接口OnGestureListener
  3. 接口OnDoubleTapListener
  4. 静态类SimpleOnGestureListener
    实现了2、3两个接口,却没有做具体事情。可以继承该类,需要哪个就override哪个部分。

xml

一般的布局即可

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/gesture_detector_linearlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.feather.androiddemos.GestureDetectorActivity">

</LinearLayout>

Java中使用GestureDetector

推荐使用SimpleOnGestureListener

//实现OnTouchListener
public class GestureDetectorActivity extends AppCompatActivity implements View.OnTouchListener{

    private LinearLayout linearLayout;
    private GestureDetector gestureDetector;

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

        linearLayout = (LinearLayout) findViewById(R.id.gesture_detector_linearlayout);//获取该布局
        linearLayout.setOnTouchListener(this);//设置监听器

        //获取GestureDetector
        gestureDetector = new GestureDetector(this,
                new SimpleOnGestureListener() //下面实现的class(类)
        );
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        gestureDetector.onTouchEvent(event); //由GestureDetector进行接管,消耗掉event
        return false;
    }

    //继承静态类,重写相应方法
    class SimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener{

         @Override//该方法就是按下时触发,此外还有近十种方法
        public boolean onDown(MotionEvent e) {
            Toast.makeText(GestureDetectorActivity.this, "onDown", Toast.LENGTH_SHORT).show();
            return super.onDown(e);
        }
    }
}

ViewFliper和GestureDetector实现图片切换

之前ViewFliper教程链接如下:http://blog.csdn.net/feather_wch/article/details/51535660

我们将在本教程基础上进行修改:
ViewFlipperActivity中置换为如下内容即可

public class ViewFlipperActivity extends AppCompatActivity implements View.OnTouchListener{

    ViewFlipper viewFlipper = null; //ViewFlipper控件
    GestureDetector gestureDetector = null;

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

        viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper);//获取自动切换
        viewFlipper.setOnTouchListener(this);

        gestureDetector = new GestureDetector(this, new SimpleOnGestureListener()); //初始化GestureDetector
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) { //触屏
        //使用GestureDetector

        gestureDetector.onTouchEvent(event); //GestureDetector接管触屏按键
        return false;
    }

    //继承静态类,重写相应方法
    class SimpleOnGestureListener extends GestureDetector.SimpleOnGestureListener{

        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            if(e2.getX() - e1.getX() > 100)//从左向右
            {
                //设置切换动画
                viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ViewFlipperActivity.this, R.anim.push_right_in));
                viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ViewFlipperActivity.this, R.anim.push_right_out));
                //显示上一个View
                viewFlipper.showPrevious(); //从左向右,显示上一幅图片
            }else if(e1.getX() - e2.getX() > 100)
            {
                //设置切换动画
                viewFlipper.setInAnimation(AnimationUtils.loadAnimation(ViewFlipperActivity.this, R.anim.push_left_in));
                viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(ViewFlipperActivity.this, R.anim.push_left_out));

                viewFlipper.showNext(); //从左向右,显示下一幅图片
            }
            return true;
        }

        public SimpleOnGestureListener() {
            super();
        }
    }
}
0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:120809次
    • 积分:4303
    • 等级:
    • 排名:第7001名
    • 原创:252篇
    • 转载:62篇
    • 译文:2篇
    • 评论:10条
    最新评论