Android开发经验谈Android开发Android技术知识

缩放手势 ScaleGestureDetector 源码解析,

2019-06-28  本文已影响7人  圆号本昊

其实在我们日常的编程中,对于缩放手势的使用并不是很经常,这一手势主要是用在图片浏览方面,比如下方例子。但是(敲重点),作为 Android 入门的基础来说,学习 ScaleGestureDetector 的使用,算是不得不过的一道坎,好在 ScaleGestureDetector 使用起来非常简单,就是源码分析上得花些功夫。

本文首先将简单的介绍下 ScaleGestureDetector 的使用,在重点给大家分析下源码(由于源码方面是我自己的理解,可能有偏差,希望各位大佬能在评论区指出,万分感谢~)

<img src="https://user-gold-cdn.xitu.io/2019/6/27/16b996688722ec94?w=1280&h=904&f=jpeg&s=235091" width="500" height="300" align=center />


ScaleGestureDetector 使用

ScaleGestureDetector 包括一个监听器,以及它所有方法的空实现:

名称 用途
ScaleGestureDetector 缩放手势的监听器
SimpleOnScaleGestureListener 该监听器的空实现,在其中重写方法

ScaleGestureDetector 方法

名称 用途
onScaleBegin 当 >= 2 个手指碰触屏幕时调用,若返回 false 则忽略改事件调用
onScale 滑动(缩放)过程中调用,若成功处理,则用户返回 true,监听器继续记录下一个缩放等动作,若为 false 表明数据未处理,则监听器继续积累
onScaleEnd 全部手指离开屏幕,结束监听

通常情况下,手势监听会结合自定义 View 来讲,这里我给出一个最简单的使用,具体的使用实例,以后再结合自定义 View 讲讲。

    private void iniScaleGestureListener(){
        mListener = new ScaleGestureDetector.SimpleOnScaleGestureListener(){
            @Override
            public boolean onScaleBegin(ScaleGestureDetector detector) {
                return super.onScaleBegin(detector);
            }

            @Override
            public boolean onScale(ScaleGestureDetector detector) {
                MyLog.d("X:" + detector.getFocusX());
                MyLog.d("Y:" + detector.getFocusY());
                MyLog.d("scale:" + detector.getScaleFactor());
                return super.onScale(detector);
            }

            @Override
            public void onScaleEnd(ScaleGestureDetector detector) {
                super.onScaleEnd(detector);
            }
        };

        detector = new ScaleGestureDetector(getContext(), mListener);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        detector.onTouchEvent(event);
        return true;
    }

ScaleGestureDetector 的使用

ScaleGestureDetector 在具体项目的使用有点复杂,我打算过段时间结合自定义 View 写一篇用来总结,所以这篇我们就先了解下 ScaleGestureDetector 的基本使用。


ScaleGestureDetector 源码分析

好了,现在我们进入本章重点,ScaleGestureDetector 源码分析,敲黑板敲黑板。首先,我们打开 ScaleGestureDetector 的源码可以看到,几乎所有的代码都集中在了 onTouchEvent 这个方法上,所以在这里,我就主要给大家介绍这个方法的实现。

第一部分:前期准备

        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onTouchEvent(event, 0);
        }

        mCurrTime = event.getEventTime();

        final int action = event.getActionMasked();

        // Forward the event to check for double tap gesture
        if (mQuickScaleEnabled) {
            mGestureDetector.onTouchEvent(event);
        }

        final int count = event.getPointerCount();
        final boolean isStylusButtonDown =
                (event.getButtonState() & MotionEvent.BUTTON_STYLUS_PRIMARY) != 0;

mInputEventConsistencyVerifier

        if (mInputEventConsistencyVerifier != null) {
            mInputEventConsistencyVerifier.onTouchEvent(event, 0);
        }

mCurrTime

        mCurrTime = event.getEventTime();

action

        final int action = event.getActionMasked();

mQuickScaleEnabled

        if (mQuickScaleEnabled) {
            mGestureDetector.onTouchEvent(event);
        }

count

        final int count = event.getPointerCount();

isStylusButtonDown

这个主要是由于判断手写笔是否按下
由于我们很少处理手写笔,所以这里不做过多说明

        final boolean isStylusButtonDown =
               (event.getButtonState() & MotionEvent.BUTTON_STYLUS_PRIMARY) != 0;

第二部分:处理与手势变化

用户的缩放手势不总是一定的,就是说对于用户而言,随时可能有手指碰触或离开屏幕,这就使得缩放中心的(焦点)随时可能发生变化,这部分主要是用来处理这一变化,并做出响应。

        final boolean anchoredScaleCancelled =
               mAnchoredScaleMode == ANCHORED_SCALE_MODE_STYLUS && !isStylusButtonDown;
       
       final boolean streamComplete = action == MotionEvent.ACTION_UP ||
               action == MotionEvent.ACTION_CANCEL || anchoredScaleCancelled;

       // 如果发生了上面这种小动作,或者说有一手指离开了屏幕,进行调用
       if (action == MotionEvent.ACTION_DOWN || streamComplete) {
           // Reset any scale in progress with the listener.
           // If it's an ACTION_DOWN we're beginning a new event stream.
           // This means the app probably didn't give us all the events. Shame on it.

           if (mInProgress) {
               mListener.onScaleEnd(this);
               mInProgress = false;
               mInitialSpan = 0;
               mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE;
           } else if (inAnchoredScaleMode() && streamComplete) {
               mInProgress = false;
               mInitialSpan = 0;
               mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE;
           }

           if (streamComplete) {
               return true;
           }
       }

anchoredScaleCancelled

        final boolean anchoredScaleCancelled =
                mAnchoredScaleMode == ANCHORED_SCALE_MODE_STYLUS && !isStylusButtonDown;

streamComplete

        final boolean streamComplete = action == MotionEvent.ACTION_UP ||
                action == MotionEvent.ACTION_CANCEL || anchoredScaleCancelled;

action == MotionEvent.ACTION_DOWN || streamComplete

if (action == MotionEvent.ACTION_DOWN || streamComplete) {...}

if (mInProgress)

            if (mInProgress) {
                mListener.onScaleEnd(this);
                mInProgress = false;
                mInitialSpan = 0;
                mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE;
            }

else if (inAnchoredScaleMode() && streamComplete)

            else if (inAnchoredScaleMode() && streamComplete) {
                mInProgress = false;
                mInitialSpan = 0;
                mAnchoredScaleMode = ANCHORED_SCALE_MODE_NONE;
            }

if (streamComplete)

            if (streamComplete) {
                return true;
            }

总结: 可以看到,当触发 down 或者触发 up,cancel 时,如果之前处于缩放计算的状态,会将其状态重置, 并调用 onScaleEnd 方法。


进入锚定比例模式

        if (!mInProgress && mStylusScaleEnabled && !inAnchoredScaleMode()
                && !streamComplete && isStylusButtonDown) {
            // Start of a button scale gesture
            mAnchoredScaleStartX = event.getX();
            mAnchoredScaleStartY = event.getY();
            mAnchoredScaleMode = ANCHORED_SCALE_MODE_STYLUS;
            mInitialSpan = 0;
        }

mAnchoredScaleStartX & mAnchoredScaleStartY

            mAnchoredScaleStartX = event.getX();
            mAnchoredScaleStartY = event.getY();

mAnchoredScaleMode

            mAnchoredScaleMode = ANCHORED_SCALE_MODE_STYLUS;

计算缩放中心

        final boolean configChanged = action == MotionEvent.ACTION_DOWN ||
                action == MotionEvent.ACTION_POINTER_UP ||
                action == MotionEvent.ACTION_POINTER_DOWN || anchoredScaleCancelled;

        final boolean pointerUp = action == MotionEvent.ACTION_POINTER_UP;
        final int skipIndex = pointerUp ? event.getActionIndex() : -1;

        // Determine focal point
        float sumX = 0, sumY = 0;
        final int div = pointerUp ? count - 1 : count;
        final float focusX;
        final float focusY;
        if (inAnchoredScaleMode()) {
            // In anchored scale mode, the focal pt is always where the double tap
            // or button down gesture started
            focusX = mAnchoredScaleStartX;
            focusY = mAnchoredScaleStartY;
            if (event.getY() < focusY) {
                mEventBeforeOrAboveStartingGestureEvent = true;
            } else {
                mEventBeforeOrAboveStartingGestureEvent = false;
            }
        } else {
            for (int i = 0; i < count; i++) {
                if (skipIndex == i) continue;
                sumX += event.getX(i);
                sumY += event.getY(i);
            }

            focusX = sumX / div;
            focusY = sumY / div;
        }

configChanged

        final boolean configChanged = action == MotionEvent.ACTION_DOWN ||
                action == MotionEvent.ACTION_POINTER_UP ||
                action == MotionEvent.ACTION_POINTER_DOWN || anchoredScaleCancelled;

pointerUp

        final boolean pointerUp = action == MotionEvent.ACTION_POINTER_UP;

skipIndex

        final int skipIndex = pointerUp ? event.getActionIndex() : -1;

初始化计算所需临时变量

        // Determine focal point
        float sumX = 0, sumY = 0;
        // 如果是抬起手指,则当前手指数减1,否则不变
        final int div = pointerUp ? count - 1 : count;
        final float focusX;
        final float focusY;

判断是否为锚定比例模式

        if (inAnchoredScaleMode()) {
            // In anchored scale mode, the focal pt is always where the double tap
            // or button down gesture started
            // 在锚定比例模式中,焦点pt始终是双击的位置,或按下手势开始
            focusX = mAnchoredScaleStartX;
            focusY = mAnchoredScaleStartY;
            if (event.getY() < focusY) {
                mEventBeforeOrAboveStartingGestureEvent = true;
            } else {
                mEventBeforeOrAboveStartingGestureEvent = false;
            }
        } else {
            for (int i = 0; i < count; i++) {
                if (skipIndex == i) continue;
                sumX += event.getX(i);
                sumY += event.getY(i);
            }

            focusX = sumX / div;
            focusY = sumY / div;
        }

算缩放比例

        // Determine average deviation from focal point @Google translate 
        float devSumX = 0, devSumY = 0;
        for (int i = 0; i < count; i++) {
            if (skipIndex == i) continue;

            // Convert the resulting diameter into a radius.
            devSumX += Math.abs(event.getX(i) - focusX);
            devSumY += Math.abs(event.getY(i) - focusY);
        }
        final float devX = devSumX / div;
        final float devY = devSumY / div;

        // Span is the average distance between touch points through the focal point;
        // i.e. the diameter of the circle with a radius of the average deviation from
        // the focal point.
        final float spanX = devX * 2;
        final float spanY = devY * 2;
        final float span;
        if (inAnchoredScaleMode()) {
            span = spanY;
        } else {
            span = (float) Math.hypot(spanX, spanY);
        }

计算平均偏差

        float devSumX = 0, devSumY = 0;
        for (int i = 0; i < count; i++) {
            if (skipIndex == i) continue;

            // Convert the resulting diameter into a radius.
            devSumX += Math.abs(event.getX(i) - focusX);
            devSumY += Math.abs(event.getY(i) - focusY);
        }
        final float devX = devSumX / div;
        final float devY = devSumY / div;

计算缩放比例

        final float spanX = devX * 2;
        final float spanY = devY * 2;
        final float span;
        if (inAnchoredScaleMode()) {
            span = spanY;
        } else {
            span = (float) Math.hypot(spanX, spanY);
        }

结束缩放事件

        final boolean wasInProgress = mInProgress;
        mFocusX = focusX;
        mFocusY = focusY;
        if (!inAnchoredScaleMode() && mInProgress && (span < mMinSpan || configChanged)) {
            mListener.onScaleEnd(this);
            mInProgress = false;
            mInitialSpan = span;
        }
        if (configChanged) {
            mPrevSpanX = mCurrSpanX = spanX;
            mPrevSpanY = mCurrSpanY = spanY;
            mInitialSpan = mPrevSpan = mCurrSpan = span;
        }

触发 onScaleBegin 开始缩放

        final int minSpan = inAnchoredScaleMode() ? mSpanSlop : mMinSpan;
        if (!mInProgress && span >= minSpan &&
                (wasInProgress || Math.abs(span - mInitialSpan) > mSpanSlop)) {
            mPrevSpanX = mCurrSpanX = spanX;
            mPrevSpanY = mCurrSpanY = spanY;
            mPrevSpan = mCurrSpan = span;
            mPrevTime = mCurrTime;
            mInProgress = mListener.onScaleBegin(this);
        }

通知用户进行缩放处理

        if (action == MotionEvent.ACTION_MOVE) {
            mCurrSpanX = spanX;
            mCurrSpanY = spanY;
            mCurrSpan = span;

            boolean updatePrev = true;

            if (mInProgress) {
                updatePrev = mListener.onScale(this);
            }

            if (updatePrev) {
                mPrevSpanX = mCurrSpanX;
                mPrevSpanY = mCurrSpanY;
                mPrevSpan = mCurrSpan;
                mPrevTime = mCurrTime;
            }
        }

updatePrev

            if (mInProgress) {
                updatePrev = mListener.onScale(this);
            }

            if (updatePrev) {
                mPrevSpanX = mCurrSpanX;
                mPrevSpanY = mCurrSpanY;
                mPrevSpan = mCurrSpan;
                mPrevTime = mCurrTime;
            }

结语

我要讲的所有内容,到这里就完全结束了

由于源码是按照我自己的理解来讲的,所以难免会有一些出入

希望大家能在评论区中帮我指出,谢谢~ 🙏

上一篇下一篇

猜你喜欢

热点阅读