feather(猎羽)

只有疯狂到认为自己能改变世界的人、才能真正的改变世界

Android高级UI GestureDetector监听各种手势

内容

第一部分:介绍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();
        }
    }
}
阅读更多
版权声明:本文为博主原创文章,可以转载,但恳请标注上转载自(猎羽)的博客并且附上链接,谢谢! https://blog.csdn.net/feather_wch/article/details/51537027
文章标签: android
个人分类: Android
上一篇Android高级UI ViewFlipper左右滑屏切换页面
下一篇Android UI
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭