Example usage for android.view MotionEvent getPointerCount

List of usage examples for android.view MotionEvent getPointerCount

Introduction

In this page you can find the example usage for android.view MotionEvent getPointerCount.

Prototype

public final int getPointerCount() 

Source Link

Document

The number of pointers of data contained in this event.

Usage

From source file:ch.jeda.platform.android.CanvasFragment.java

@Override
public boolean onTouch(final View view, final MotionEvent event) {
    int index;//from ww  w . j  a  va  2  s  . co m
    switch (event.getActionMasked()) {
    case MotionEvent.ACTION_DOWN:
    case MotionEvent.ACTION_POINTER_DOWN:
        index = event.getActionIndex();
        this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_DOWN, event.getPointerId(index),
                (int) event.getX(index), (int) event.getY(index)));
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_POINTER_UP:
        index = event.getActionIndex();
        this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_UP, event.getPointerId(index),
                (int) event.getX(index), (int) event.getY(index)));
        break;
    case MotionEvent.ACTION_MOVE:
        for (index = 0; index < event.getPointerCount(); ++index) {
            this.postEvent(new PointerEvent(mapDevice(event), EventType.POINTER_MOVED,
                    event.getPointerId(index), (int) event.getX(index), (int) event.getY(index)));
        }

        break;
    }
    return true;
}

From source file:org.godotengine.godot.Godot.java

public boolean gotTouchEvent(final MotionEvent event) {

    final int evcount = event.getPointerCount();
    if (evcount == 0)
        return true;

    if (mView != null) {
        final int[] arr = new int[event.getPointerCount() * 3];

        for (int i = 0; i < event.getPointerCount(); i++) {

            arr[i * 3 + 0] = (int) event.getPointerId(i);
            arr[i * 3 + 1] = (int) event.getX(i);
            arr[i * 3 + 2] = (int) event.getY(i);
        }/* ww w  .ja  v a 2s .  c  o m*/
        final int pointer_idx = event.getPointerId(event.getActionIndex());

        //System.out.printf("gaction: %d\n",event.getAction());
        final int action = event.getAction() & MotionEvent.ACTION_MASK;
        mView.queueEvent(new Runnable() {
            @Override
            public void run() {
                switch (action) {
                case MotionEvent.ACTION_DOWN: {
                    GodotLib.touch(0, 0, evcount, arr);
                    //System.out.printf("action down at: %f,%f\n", event.getX(),event.getY());
                }
                    break;
                case MotionEvent.ACTION_MOVE: {
                    GodotLib.touch(1, 0, evcount, arr);
                    /*
                    for(int i=0;i<event.getPointerCount();i++) {
                       System.out.printf("%d - moved to: %f,%f\n",i, event.getX(i),event.getY(i));
                    }
                    */
                }
                    break;
                case MotionEvent.ACTION_POINTER_UP: {
                    GodotLib.touch(4, pointer_idx, evcount, arr);
                    //System.out.printf("%d - s.up at: %f,%f\n",pointer_idx, event.getX(pointer_idx),event.getY(pointer_idx));
                }
                    break;
                case MotionEvent.ACTION_POINTER_DOWN: {
                    GodotLib.touch(3, pointer_idx, evcount, arr);
                    //System.out.printf("%d - s.down at: %f,%f\n",pointer_idx, event.getX(pointer_idx),event.getY(pointer_idx));
                }
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP: {
                    GodotLib.touch(2, 0, evcount, arr);
                    /*
                    for(int i=0;i<event.getPointerCount();i++) {
                       System.out.printf("%d - up! %f,%f\n",i, event.getX(i),event.getY(i));
                    }
                    */
                }
                    break;
                }
            }
        });
    }
    return true;
}

From source file:com.csounds.examples.tests.SynthActivity.java

/** Called when the activity is first created. */
@Override//from www  . ja  va 2  s .co m
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    for (int i = 0; i < touchIds.length; i++) {
        touchIds[i] = -1;
        touchX[i] = -1;
        touchY[i] = -1;
    }
    multiTouchView = new View(this);

    setContentView(R.layout.synth2);
    multiTouchView = (RelativeLayout) findViewById(R.id.synth);
    octPlusButton = (Button) findViewById(R.id.plusoctavebutton);
    octMinusButton = (Button) findViewById(R.id.minusoctavebutton);
    diode4 = (ToggleButton) findViewById(R.id.diode4);
    diode3 = (ToggleButton) findViewById(R.id.diode3);
    LcdScreenView lcd = (LcdScreenView) findViewById(R.id.lcd_screen);
    new DrumMachineLcdUpdater(lcd.getModel());
    this.createLoadListener((View) lcd);
    keyboardLayout = (RelativeLayout) findViewById(R.id.keyboard);
    keyboardLayout.setOnTouchListener(new OnTouchListener() {

        /* (non-Javadoc)
         * @see android.view.View.OnTouchListener#onTouch(android.view.View, android.view.MotionEvent)
         */
        public boolean onTouch(View v, MotionEvent event) {
            final int action = event.getAction() & MotionEvent.ACTION_MASK;
            float[] touchArrayDown = new float[2];
            switch (action) {
            case MotionEvent.ACTION_DOWN:
            case MotionEvent.ACTION_POINTER_DOWN:

                for (int i = 0; i < event.getPointerCount(); i++) {
                    int pointerId = event.getPointerId(i);
                    int id = getTouchId(pointerId);

                    if (id == -1) {

                        id = getTouchIdAssignment();

                        if (id != -1) {
                            touchIds[id] = pointerId;
                            touchX[id] = event.getX(i) / keyboardLayout.getWidth();
                            touchY[id] = 1 - (event.getY(i) / keyboardLayout.getHeight());
                            //TODO calculte interval Y

                            if (touchXPtr[id] != null) {
                                //TODO swtich touchid
                                //                           Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id] + " multitouchview width " + multiTouchView.getWidth() + " multitouchview width " + multiTouchView.getHeight());
                                //                           Log.d("touchXtouchY","keyboardLayout width " + keyboardLayout.getWidth() + " keyboardLayout width " + keyboardLayout.getHeight());
                                // get the key from the touch coordinates
                                float[] touchArray = new float[2];
                                touchArray = evaluateTouchKey(touchX[id], touchY[id]);
                                touchArrayDown[0] = touchX[id];
                                touchArrayDown[1] = touchY[id];
                                touchX[id] = touchArray[0];
                                touchY[id] = touchArray[1];

                                //                           Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]);
                                touchXPtr[id].SetValue(0, touchX[id]);
                                touchYPtr[id].SetValue(0, touchY[id]);
                                csoundObj.sendScore(String.format("i1.%d 0 -2 %d", id, id));
                            }

                        }
                    }

                }

                break;
            case MotionEvent.ACTION_MOVE:

                for (int i = 0; i < event.getPointerCount(); i++) {
                    int pointerId = event.getPointerId(i);
                    int id = getTouchId(pointerId);

                    if (id != -1) {
                        touchIds[id] = pointerId;
                        touchX[id] = event.getX(i) / keyboardLayout.getWidth();
                        touchY[id] = 1 - (event.getY(i) / keyboardLayout.getHeight());
                        //TODO calculte interval Y

                        if (touchXPtr[id] != null) {
                            //TODO swtich touchid
                            //                           Log.d("aaa","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id] + " multitouchview width " + multiTouchView.getWidth() + " multitouchview width " + multiTouchView.getHeight());
                            //                           Log.d("aaa","keyboardLayout width " + keyboardLayout.getWidth() + " keyboardLayout width " + keyboardLayout.getHeight());
                            // get the key from the touch coordinates
                            float[] touchArray = new float[2];

                            touchArray = evaluateTouchKey(touchX[id], touchY[id]);

                            touchX[id] = touchArray[0];
                            touchY[id] = touchArray[1];

                            //                           Log.d("touchXtouchY","touchX[id]) " + touchX[id] + " touchY[id]) " + touchY[id]);
                            touchXPtr[id].SetValue(0, touchX[id]);
                            touchYPtr[id].SetValue(0, touchY[id]);
                            csoundObj.sendScore(String.format("i1.%d 0 -2 %d", id, id));
                        }

                    }

                }
                break;

            case MotionEvent.ACTION_POINTER_UP:
            case MotionEvent.ACTION_UP: {
                int activePointerIndex = event.getActionIndex();
                int pointerId = event.getPointerId(activePointerIndex);

                int id = getTouchId(pointerId);
                if (id != -1) {
                    touchIds[id] = -1;
                    csoundObj.sendScore(String.format("i-1.%d 0 0 %d", id, id));
                }
                //write file
                /*         System.out.println(String.format(
                      "i1.%d 0 -2 %d", id, id));
                writeToFile(String.format(
                      "i1.%d 0 -2 %d", id, id));
                 */
            }
                break;
            }

            return true;
        }

    });

    OnTouchListener octBtnOk = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (octNbr < 12)
                    octNbr += 12;
                break;
            }
            return false;
        }
    };
    octPlusButton.setOnTouchListener(octBtnOk);

    OnTouchListener octBtnMinus = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (octNbr > -36)
                    octNbr -= 12;

                break;
            }
            return false;
        }
    };

    octMinusButton.setOnTouchListener(octBtnMinus);

    String csd = getResourceFileAsString(R.raw.multitouch_xy_kx);
    File f = createTempFile(csd);

    csoundObj.addBinding(this);

    csoundObj.startCsound(f);
    //   initknobs();

    initSeekBar(diode3, diode4);

}

From source file:org.mozilla.gecko.ui.PanZoomController.java

private boolean onTouchEnd(MotionEvent event) {
    switch (mState) {
    case NOTHING:
    case FLING://from w  w w .ja v a 2  s.  c o  m
        // should never happen
        Log.e(LOG_NAME, "Received impossible touch end while in " + mState);
        return false;
    case TOUCHING:
        mState = PanZoomState.NOTHING;
        // TODO: send click to gecko
        return false;
    case PANNING:
    case PANNING_HOLD:
        mState = PanZoomState.FLING;
        fling(System.currentTimeMillis());
        return true;
    case PINCHING:
        int points = event.getPointerCount();
        if (points == 1) {
            // last touch up
            mState = PanZoomState.NOTHING;
        } else if (points == 2) {
            int pointRemovedIndex = event.getActionIndex();
            int pointRemainingIndex = 1 - pointRemovedIndex; // kind of a hack
            mState = PanZoomState.TOUCHING;
            mX.touchPos = event.getX(pointRemainingIndex);
            mY.touchPos = event.getY(pointRemainingIndex);
        } else {
            // still pinching, do nothing
        }
        return true;
    }
    Log.e(LOG_NAME, "Unhandled case " + mState + " in onTouchEnd");
    return false;
}

From source file:org.protocoderrunner.apprunner.AppRunnerActivity.java

@Override
public boolean onGenericMotionEvent(MotionEvent event) {

    int action = event.getAction();
    int actionCode = event.getActionMasked();

    ArrayList<PPadView.TouchEvent> t = new ArrayList<PPadView.TouchEvent>();

    // check finger if down or up
    if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {

    }/* w ww  . ja  v  a  2s.c o m*/

    if (event.getSource() == MotionEvent.TOOL_TYPE_MOUSE) {
        // if (event.getButtonState() == ac) {

        // }
    }

    // get positions per finger
    for (int i = 0; i < event.getPointerCount(); i++) {
        // TouchEvent o = new TouchEvent("finger", event.getPointerId(i),
        // action, (int) event.getX(),
        // (int) event.getY());
        // t.add(o);
    }

    //
    // FINGER 1 UP x y
    // FINGER 2 MOVE x y
    // MOUSE 3 MOVE x y

    // return touching;
    return super.onGenericMotionEvent(event);
}

From source file:com.guodong.sun.guodong.widget.ZoomImageView.java

@Override
public boolean onTouch(View v, MotionEvent event) {

    if (mGestureDetector.onTouchEvent(event))
        return true;

    mScaleGestureDetector.onTouchEvent(event);

    float x = 0;/*from w  w w  . j  a  v  a2  s.  c om*/
    float y = 0;
    // ?
    final int mPointerCount = event.getPointerCount();
    for (int i = 0; i < mPointerCount; i++) {

        x += event.getX(i);
        y += event.getY(i);
    }

    x = x / mPointerCount;
    y = y / mPointerCount;

    /**
     * ????mLasX , mLastY
     */
    if (mLastPointerCount != mPointerCount) {
        isCanDrag = false;
        mLastX = x;
        mLastY = y;
    }

    mLastPointerCount = mPointerCount;

    RectF rectF = getMatrixRectF();

    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:

        if (rectF.width() > getWidth() + 0.01 || rectF.height() > getHeight() + 0.01) {
            if (getParent() instanceof ViewPager)
                getParent().requestDisallowInterceptTouchEvent(true);
        }
        break;
    case MotionEvent.ACTION_MOVE:

        if (rectF.width() > getWidth() + 0.01 || rectF.height() > getHeight() + 0.01) {
            if (getParent() instanceof ViewPager)
                getParent().requestDisallowInterceptTouchEvent(true);
        }

        float dX = x - mLastX;
        float dY = y - mLastY;

        if (!isCanDrag) {
            isCanDrag = isMoveAction(dX, dY);
        }

        if (isCanDrag) {
            if (getDrawable() != null) {

                isCheckLeftAndRight = isCheckTopAndBottom = true;

                // ????
                if (rectF.width() < getWidth()) {
                    dX = 0;
                    isCheckLeftAndRight = false;
                }
                // ????
                if (rectF.height() < getHeight()) {
                    dY = 0;
                    isCheckTopAndBottom = false;
                }

                mScaleMatrix.postTranslate(dX, dY);

                checkBorderWhenTranslate();

                setImageMatrix(mScaleMatrix);

            }
        }
        mLastX = x;
        mLastY = y;
        break;

    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mLastPointerCount = 0;
        break;
    }
    return true;
}

From source file:com.android.mail.browse.ConversationContainer.java

private void forwardFakeMotionEvent(MotionEvent original, int newAction) {
    MotionEvent newEvent = MotionEvent.obtain(original);
    newEvent.setAction(newAction);/*from w  w  w .  ja  va 2 s .  co  m*/
    mWebView.onTouchEvent(newEvent);
    LogUtils.v(TAG, "in Container.OnTouch. fake: action=%d x/y=%f/%f pointers=%d", newEvent.getActionMasked(),
            newEvent.getX(), newEvent.getY(), newEvent.getPointerCount());
}

From source file:com.mylikes.likes.etchasketch.Slate.java

double getSpan(MotionEvent event) {
    int P = event.getPointerCount();
    if (P < 2)
        return 0;
    final int zero[] = { 0, 0 };
    getLocationOnScreen(zero);// w  w w. ja v a  2  s. c o  m
    final double x0 = event.getX(0) + zero[0];
    final double x1 = event.getX(1) + zero[0];
    final double y0 = event.getY(0) + zero[1];
    final double y1 = event.getY(1) + zero[1];
    final double span = Math.hypot(event.getX(0) - event.getX(1), event.getY(0) - event.getY(1));
    Log.v(TAG, String.format("zoom: p0=(%g,%g) p1=(%g,%g) span=%g", x0, y0, x1, y1, span));
    return span;
}

From source file:com.kircherelectronics.accelerationfilter.activity.AccelerationPlotActivity.java

/**
 * Pinch to zoom.//from   ww w  .j  a va2  s  . c o  m
 */
@Override
public boolean onTouch(View v, MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls.
    float newDist = 0;

    switch (e.getAction()) {

    case MotionEvent.ACTION_MOVE:

        // pinch to zoom
        if (e.getPointerCount() == 2) {
            if (distance == 0) {
                distance = fingerDist(e);
            }

            newDist = fingerDist(e);

            zoom *= distance / newDist;

            dynamicPlot.setMaxRange(zoom * Math.log(zoom));
            dynamicPlot.setMinRange(-zoom * Math.log(zoom));

            distance = newDist;
        }
    }

    return false;
}

From source file:com.mylikes.likes.etchasketch.Slate.java

PointF getCenter(MotionEvent event, PointF out) {
    int P = event.getPointerCount();
    PointF pt = ((out == null) ? new PointF() : out);
    pt.set(event.getX(0), event.getY(0));
    final int zero[] = { 0, 0 };
    getLocationOnScreen(zero);/*from  ww w.j av a  2  s  . c o m*/
    for (int j = 1; j < P; j++) {
        pt.x += event.getX(j) + zero[0];
        pt.y += event.getY(j) + zero[1];
    }
    pt.x /= P;
    pt.y /= P;
    return pt;
}