Android官方开发文档Training系列课程中文版:手势处理之ViewGroup的事件管理

原文地址:https://developer.android.com/training/gestures/viewgroup.html

在ViewGroup中处理触摸事件要格外小心,因为在ViewGroup中有很多子View,而这些子View对于不同的触摸事件来说是不同的目标。要确保每个View都正确的接收了相应的触摸事件。

在ViewGroup中拦截触摸事件

onInterceptTouchEvent()方法会在触摸事件到达ViewGroup的表面时调用,这包括内部的子View。如果onInterceptTouchEvent()返回了true,那么MotionEvent对象就会被拦截,这意味着该次事件不会传给子View,而是会传给ViewGroup本身的onTouchEvent()方法。

onInterceptTouchEvent()给了ViewGroup本身一个机会:在子View获得任何事件之前一个拦截该事件的机会。如果onInterceptTouchEvent()返回了true,那么原先处理该次事件的子View就会收到一个ACTION_CANCEL的事件,并且原先事件的剩余事件都会被传到该ViewGroup的onTouchEvent()方法中做常规处理。onInterceptTouchEvent()还可以返回false,这样的话,该次事件则会通过View树继续向下传递,直到到达目标View为止,目标View会在自己的onTouchEvent()方法中处理该次事件。

在下面的示例代码中,类MyViewGroup继承了ViewGroup,并包含了多个View,这些View我们在这里称之为子View,而MyViewGroup称为父容器View。如果你在水平方向上滑动手指,那么子View皆不会收到触摸事件。MyViewGroup会通过滚动它的内部来实现触摸事件的处理。不管如何,如果你按下了子View中的按钮,或者在垂直方向上滑动,那么ViewGroup则不会去拦截这些事件,因为子View是该次事件的目标View。在这些情况下,onInterceptTouchEvent()应该返回false,且MyViewGroup的onTouchEvent()方法也不会被调用。

public class MyViewGroup extends ViewGroup {

    private int mTouchSlop;

    ...

    ViewConfiguration vc = ViewConfiguration.get(view.getContext());
    mTouchSlop = vc.getScaledTouchSlop();

    ...

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        /*
         * This method JUST determines whether we want to intercept the motion.
         * If we return true, onTouchEvent will be called and we do the actual
         * scrolling there.
         */


        final int action = MotionEventCompat.getActionMasked(ev);

        // Always handle the case of the touch gesture being complete.
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            // Release the scroll.
            mIsScrolling = false;
            return false; // Do not intercept touch event, let the child handle it
        }

        switch (action) {
            case MotionEvent.ACTION_MOVE: {
                if (mIsScrolling) {
                    // We're currently scrolling, so yes, intercept the
                    // touch event!
                    return true;
                }

                // If the user has dragged her finger horizontally more than
                // the touch slop, start the scroll

                // left as an exercise for the reader
                final int xDiff = calculateDistanceX(ev);

                // Touch slop should be calculated using ViewConfiguration
                // constants.
                if (xDiff > mTouchSlop) {
                    // Start scrolling!
                    mIsScrolling = true;
                    return true;
                }
                break;
            }
            ...
        }

        // In general, we don't want to intercept touch events. They should be
        // handled by the child view.
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Here we actually handle the touch event (e.g. if the action is ACTION_MOVE,
        // scroll this container).
        // This method will only be called if the touch event was intercepted in
        // onInterceptTouchEvent
        ...
    }
}

这里要注意,ViewGroup还提供了requestDisallowInterceptTouchEvent()方法。当子View不希望它的父容器及祖先容器拦截触摸事件时,ViewGroup会在 onInterceptTouchEvent()方法中对其进行调用,从而判断是否要拦截本次事件。

使用ViewConfiguration常量

在上面的代码中使用了ViewConfiguration来初始化一个名为mTouchSlop的变量。你可以使用ViewConfiguration来访问Android系统所使用的常用距离、速度及时间。

“mTouchSlop”引用了触摸事件在被拦截之前手指移动的以像素为单位的距离。Touch slop经常被用来在用户在执行触摸操作时防止产生意外滚动。

ViewConfiguration的另外两个常用方法是getScaledMinimumFlingVelocity()getScaledMaximumFlingVelocity()。这两个方法分别返回了用于初始化滚动的最小、最大的速度值。以每秒几像素为单位:

ViewConfiguration vc = ViewConfiguration.get(view.getContext());
private int mSlop = vc.getScaledTouchSlop();
private int mMinFlingVelocity = vc.getScaledMinimumFlingVelocity();
private int mMaxFlingVelocity = vc.getScaledMaximumFlingVelocity();

...

case MotionEvent.ACTION_MOVE: {
    ...
    float deltaX = motionEvent.getRawX() - mDownX;
    if (Math.abs(deltaX) > mSlop) {
        // A swipe occurred, do something
    }

...

case MotionEvent.ACTION_UP: {
    ...
    } if (mMinFlingVelocity <= velocityX && velocityX <= mMaxFlingVelocity
            && velocityY < velocityX) {
        // The criteria have been satisfied, do something
    }
}

扩展子View的触控区域

Android提供的TouchDelegate使扩展子View的触控区域成为了可能。这对于子View本身特别小,而它的触控区域需要很大时很有用。如果需要的话,你也可以使用这种方式来缩小子View的触控区域。

在下面的示例中,ImageButton作为我们的”delegate view”(这里的意思是需要父容器扩展触控区域的那个View)。下面是示例的布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/parent_layout"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:context=".MainActivity" >

     <ImageButton android:id="@+id/button"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:background="@null"
          android:src="@drawable/icon" />
</RelativeLayout>

下面的代码做了以下这些事情:

  • 获得父容器View,并Post一个Runnale对象到UI线程。这可以确保在调用getHitRect()方法之前父容器已经对子View完成了排布。getHitRect()会返回父容器坐标内当前View的点击矩阵(触控区域)。
  • 找到ImageButton,然后调用它的getHitRect()方法获得该View的触控边界。
  • 扩大ImageButton的触控区域。
  • 实例化一个TouchDelegate,将要扩展的触控区域矩阵与要扩展触控区域的ImageView作为参数传入。
  • TouchDelegate设置给父容器View,只有这样做,我们所触碰到的扩展区域才会被路由到子View上。

在TouchDelegate代理的范围内,父容器View将会接收所有的触摸事件。如果触摸事件发生在子View本身的触控区域内,那么父容器View会将所有的触摸事件传给子View处理:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Get the parent view
        View parentView = findViewById(R.id.parent_layout);

        parentView.post(new Runnable() {
            // Post in the parent's message queue to make sure the parent
            // lays out its children before you call getHitRect()
            @Override
            public void run() {
                // The bounds for the delegate view (an ImageButton
                // in this example)
                Rect delegateArea = new Rect();
                ImageButton myButton = (ImageButton) findViewById(R.id.button);
                myButton.setEnabled(true);
                myButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this,
                                "Touch occurred within ImageButton touch region.",
                                Toast.LENGTH_SHORT).show();
                    }
                });

                // The hit rectangle for the ImageButton
                myButton.getHitRect(delegateArea);

                // Extend the touch area of the ImageButton beyond its bounds
                // on the right and bottom.
                delegateArea.right += 100;
                delegateArea.bottom += 100;

                // Instantiate a TouchDelegate.
                // "delegateArea" is the bounds in local coordinates of
                // the containing view to be mapped to the delegate view.
                // "myButton" is the child view that should receive motion
                // events.
                TouchDelegate touchDelegate = new TouchDelegate(delegateArea,
                        myButton);

                // Sets the TouchDelegate on the parent view, such that touches
                // within the touch delegate bounds are routed to the child.
                if (View.class.isInstance(myButton.getParent())) {
                    ((View) myButton.getParent()).setTouchDelegate(touchDelegate);
                }
            }
        });
    }
}
优秀的个人博客,低调大师

微信关注我们

原文链接:https://yq.aliyun.com/articles/487410

转载内容版权归作者及来源网站所有!

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

相关文章

发表评论

资源下载

更多资源
Apache Tomcat7、8、9(Java Web服务器)

Apache Tomcat7、8、9(Java Web服务器)

Tomcat是Apache 软件基金会(Apache Software Foundation)的Jakarta 项目中的一个核心项目,由Apache、Sun 和其他一些公司及个人共同开发而成。因为Tomcat 技术先进、性能稳定,而且免费,因而深受Java 爱好者的喜爱并得到了部分软件开发商的认可,成为目前比较流行的Web 应用服务器。

Eclipse(集成开发环境)

Eclipse(集成开发环境)

Eclipse 是一个开放源代码的、基于Java的可扩展开发平台。就其本身而言,它只是一个框架和一组服务,用于通过插件组件构建开发环境。幸运的是,Eclipse 附带了一个标准的插件集,包括Java开发工具(Java Development Kit,JDK)。

Java Development Kit(Java开发工具)

Java Development Kit(Java开发工具)

JDK是 Java 语言的软件开发工具包,主要用于移动设备、嵌入式设备上的java应用程序。JDK是整个java开发的核心,它包含了JAVA的运行环境(JVM+Java系统类库)和JAVA工具。

Sublime Text 一个代码编辑器

Sublime Text 一个代码编辑器

Sublime Text具有漂亮的用户界面和强大的功能,例如代码缩略图,Python的插件,代码段等。还可自定义键绑定,菜单和工具栏。Sublime Text 的主要功能包括:拼写检查,书签,完整的 Python API , Goto 功能,即时项目切换,多选择,多窗口等等。Sublime Text 是一个跨平台的编辑器,同时支持Windows、Linux、Mac OS X等操作系统。