ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() 方法未命中 (ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() method not being hit)


問題描述

ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() 方法未命中 (ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() method not being hit)

Update: I figured out what is happening.  See comments.

I am trying to write a ViewSwitcher that passes all gestures to its first child until it receives a scale gesture; it then passes them to its second child until that child is zoomed out again completely, when it reverts to the first child.  My subclass has a ScaleGestureDetector and I made a very simple listener:

    protected class OnScaleModeSwitcher implements ScaleGestureDetector.OnScaleGestureListener
    {
        protected PageFlipSwitcher owner;

        public OnScaleModeSwitcher(PageFlipSwitcher newOwner)
        {
            super();
            owner = newOwner;
        }

        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            return false;
        }

        @Override
        public boolean onScaleBegin(ScaleGestureDetector detector) {
            owner.onScaleBegin();
//returning false here causes the rest of the gesture to be ignored.
            return false;
        }

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

As you can see all it does is take a reference to the owner object on construction, then pass some events to methods within the owner class.  However, onScaleEnd() is not being reached by the code.

I'm aware that onInterceptTouchEvent can be a little dicey; I followed the suggestions in the Android docs for it as closely as possible and have

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev)
    {
        onTouchEvent(ev);
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
//mode is the name of the ScaleGestureDetector
        mode.onTouchEvent(ev);

//this code just passes events to the children
//it seems to work OK
        if(zoomActive)
        {
            //ZoomSwitcher
            getChildAt(1).onTouchEvent(ev);
        }
        else
        {
            //Gallery
            getChildAt(0).onTouchEvent(ev);
        }
        return true;
    }

I read somewhere else that a GestureDetector may not receive the ACTION_UP event:

Android: How to detect when a scroll has ended

Is that what is happening here?  If so what is the point of the onScaleEnd() method?

EDIT:

I have worked this out: it is because my methods return false.  Eclipse auto‑implemented some stubs and I didn't change the return values when I filled them in.

‑‑‑‑‑

參考解法

方法 1:

If a ScaleGestureDetector is set up that returns false from onScaleBegin(...), none of the subsequent methods will be hit.  Generally methods that consume a MotionEvent but return false do not get subsequent MotionEvents until after ACTION_UP, when the listeners are reset.

(by Andrew WyldAndrew Wyld)

參考文件

  1. ScaleGestureDetector.OnScaleGestureListener.onScaleEnd() method not being hit (CC BY‑SA 3.0/4.0)

#Callback #scale #Android #gesturedetector






相關問題

將 C++ 函數指針設置為 C# 委託 (set C++ function pointer as C# delegate)

YouTube(注入)視頻結束回調 (YouTube (injected) video ends callback)

我如何在 Rails 中“驗證”銷毀 (How do I 'validate' on destroy in rails)

Як я магу рэалізаваць зваротныя выклікі на іншы сервер у C# (How can I implement callbacks to another server in c#)

顯示等待通知,直到回調從線程返回 (Show waiting notification till callback returns from thread)

應用程序未通過文本服務框架 DLL 檢測輸入語言更改 (Application not detecting input language changes via Text Service Framework DLL)

回調參數名稱 (Callback parameters name)

我是否以正確的方式考慮回調? (Am I thinking about Callbacks the right way?)

MSMQ 異步異常行為 - .NET 4.0 與 .NET 2.0 (MSMQ asynchronous exception behavior - .NET 4.0 vs .NET 2.0)

參數連接問題。我該如何處理 (parameter connection problem. How can I deal with this)

如何在 JavaScript 中實現動態回調參數 (How can I achieve dynamic callback arguments in JavaScript)

回調警告:回調錯誤創建破折號數據表 (Callback warning: Callback error creating dash' DataTable)







留言討論