Pergunta

I am using a GestureDetector.OnGestureListener in order to implement pinch-to-zoom in Android. I am extending a TextView class and therefore the method setTextSize() is already implemented. The following is my code for onScroll().

 @Override
 public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
     if(LOGGING) Log.v(MODULE_NAME, "onScroll()");

     float x, y;
     float oldDist, newDist;

     if (e2.getPointerCount() == 2 && 
         ((e2.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_MOVE)) {
         x = e2.getHistoricalX(0, 0) - e2.getHistoricalX(1, 0);
         y = e2.getHistoricalY(0, 0) - e2.getHistoricalY(1, 0);
         oldDist = FloatMath.sqrt(x * x + y * y);
         if (oldDist > 10) {
             if (LOGGING) Log.d(MODULE_NAME, "Starting ZOOM mode");                    
             x = e2.getX(0) - e2.getX(1);
             y = e2.getY(0) - e2.getY(1);
             newDist = FloatMath.sqrt(x * x + y * y);

             if (newDist > 30) {
                    float scaleFactor = (newDist > oldDist) ? oldDist : newDist;
                    float scale = (newDist - oldDist) / scaleFactor;
                    // Callback to be processed in main thread
                    setTextSize(scale * currentTextSize);
                    oldDist = newDist;
             }      
             scrollDetected = true;
             return true;
         }
     }
     return false;            
 }

My problem is that occasionally, not always, getHistoricalX() throws an exception. I am attaching the LogCat result.

07-31 16:53:30.358: V/ZoomTextView(19540): onScroll()
07-31 16:53:30.358: E/InputEventReceiver(19540): Exception dispatching input event.
07-31 16:53:30.358: E/MessageQueue-JNI(19540): Exception in MessageQueue callback: handleReceiveCallback
07-31 16:53:30.358: E/MessageQueue-JNI(19540): java.lang.IllegalArgumentException: historyPos out of range
07-31 16:53:30.358: E/MessageQueue-JNI(19540):  at android.view.MotionEvent.nativeGetAxisValue(Native Method)
07-31 16:53:30.358: E/MessageQueue-JNI(19540):  at android.view.MotionEvent.getHistoricalX(MotionEvent.java:2739)

Since I know that there are 2 pointers and that the history is at least the size of 0, It is not clear to me how or why this exception occurs. Can anyone please assist?

I saw this similar question, and it was of no help to me.

Foi útil?

Solução

Before using getHistoricalX() or getHistoricalY(), you should check the size with getHistorySize().

If it returns zero, you know there are no historical events. Then you just have to process the current events, using getX() and getY().

Outras dicas

Hi have the same problem and it was resolved thanks to Geobits answer. Just check getHistorySize() is not returning 0:

            if (event.getHistorySize() > 0) {

                float lastX = event.getHistoricalX(0);

            }
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top