Example usage for android.view MotionEvent ACTION_MASK

List of usage examples for android.view MotionEvent ACTION_MASK

Introduction

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

Prototype

int ACTION_MASK

To view the source code for android.view MotionEvent ACTION_MASK.

Click Source Link

Document

Bit mask of the parts of the action code that are the action itself.

Usage

From source file:com.bluetech.gallery5.ui.ImageDetailFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /*//w ww . j  a  v a 2 s  .  com
    // Inflate and locate the main ImageView
    final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
    mImageView = (ImageView) v.findViewById(R.id.imageRecycView);
    return v;
    */
    final View v = inflater.inflate(R.layout.image_detail_fragment, container, false);
    mImageView = (ImageView) v.findViewById(R.id.imageRecycView);
    //ImageView img = new ImageView(getActivity());

    mImageView.setPadding(6, 6, 6, 6);

    // mImageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)) ;
    //ImageViewHelperURL.setUrlDrawable((ImageView) img, url, R.drawable.no_image) ;

    mImageView.setOnTouchListener(new View.OnTouchListener() {
        private static final String TAG = "SlideImageView";
        // These matrices will be used to move and zoom image
        Matrix matrix = new Matrix();
        Matrix savedMatrix = new Matrix();

        // We can be in one of these 3 states
        static final int NONE = 0;
        static final int DRAG = 1;
        static final int ZOOM = 2;
        int mode = NONE;

        // Remember some things for zooming
        PointF start = new PointF();
        PointF mid = new PointF();
        float oldDist = 1f;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            ImageView view = (ImageView) v;

            // Dump touch event to log
            dumpEvent(event);

            // Handle touch events here...
            switch (event.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                savedMatrix.set(matrix);
                start.set(event.getX(), event.getY());
                Log.d(TAG, "mode=DRAG");
                mode = DRAG;
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                oldDist = spacing(event);
                Log.d(TAG, "oldDist=" + oldDist);
                if (oldDist > 10f) {
                    savedMatrix.set(matrix);
                    midPoint(mid, event);
                    mode = ZOOM;
                    Log.d(TAG, "mode=ZOOM");
                }
                break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
                mode = NONE;
                Log.d(TAG, "mode=NONE");
                break;
            case MotionEvent.ACTION_MOVE:
                if (mode == DRAG) {
                    // ...
                    matrix.set(savedMatrix);
                    matrix.postTranslate(event.getX() - start.x, event.getY() - start.y);
                } else if (mode == ZOOM) {
                    float newDist = spacing(event);
                    Log.d(TAG, "newDist=" + newDist);
                    if (newDist > 10f) {
                        matrix.set(savedMatrix);
                        float scale = newDist / oldDist;
                        Log.d(TAG, "ZOOOOOOOM: " + scale);
                        matrix.postScale(scale, scale, mid.x, mid.y);
                    }
                }
                break;
            }

            view.setImageMatrix(matrix);
            return true; // indicate event was handled
        }

        /** Show an event in the LogCat view, for debugging */
        private void dumpEvent(MotionEvent event) {
            String names[] = { "DOWN", "UP", "MOVE", "CANCEL", "OUTSIDE", "POINTER_DOWN", "POINTER_UP", "7?",
                    "8?", "9?" };
            StringBuilder sb = new StringBuilder();
            int action = event.getAction();
            int actionCode = action & MotionEvent.ACTION_MASK;
            sb.append("event ACTION_").append(names[actionCode]);
            if (actionCode == MotionEvent.ACTION_POINTER_DOWN || actionCode == MotionEvent.ACTION_POINTER_UP) {
                sb.append("(pid ").append(action >> MotionEvent.ACTION_POINTER_ID_SHIFT);
                sb.append(")");
            }
            sb.append("[");
            for (int i = 0; i < event.getPointerCount(); i++) {
                sb.append("#").append(i);
                sb.append("(pid ").append(event.getPointerId(i));
                sb.append(")=").append((int) event.getX(i));
                sb.append(",").append((int) event.getY(i));
                if (i + 1 < event.getPointerCount())
                    sb.append(";");
            }
            sb.append("]");
            Log.d(TAG, sb.toString());
        }

        /** Determine the space between the first two fingers */
        private float spacing(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return (float) Math.sqrt(x * x + y * y);
        }

        /** Calculate the mid point of the first two fingers */
        private void midPoint(PointF point, MotionEvent event) {
            float x = event.getX(0) + event.getX(1);
            float y = event.getY(0) + event.getY(1);
            point.set(x / 2, y / 2);
        }
    });

    return v;

}

From source file:cn.iotguard.GalleryWidget.GalleryViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
        super.onInterceptTouchEvent(event);
    }//  ww w  .  ja va  2  s .  co m

    float[] difference = handleMotionEvent(event);

    if (mCurrentView.pagerCanScroll()) {
        return super.onInterceptTouchEvent(event);
    } else {
        if (difference != null && mCurrentView.onRightSide && difference[0] < 0) //move right
        {
            return super.onInterceptTouchEvent(event);
        }
        if (difference != null && mCurrentView.onLeftSide && difference[0] > 0) //move left
        {
            return super.onInterceptTouchEvent(event);
        }
        if (difference == null && (mCurrentView.onLeftSide || mCurrentView.onRightSide)) {
            return super.onInterceptTouchEvent(event);
        }
    }
    return false;
}

From source file:com.tearoffcalendar.activities.FaceDownCardFragment.java

public boolean onTouch(View view, MotionEvent event) {
    // We need to register click here
    final float y = event.getRawY();
    switch (event.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        deltaY = 0;//from   w w  w .ja va  2  s. c o  m
        clickStartedOnY = y;
        Log.v(TAG, "Click started on:" + String.valueOf(clickStartedOnY));
        break;
    case MotionEvent.ACTION_UP:
        Log.v(TAG, "Resulted deltaY:" + String.valueOf(deltaY));
        // This one is for when finger is up, not a rude gesture bro!
        if (deltaY >= DELTA_Y_MAX) {
            Log.v(TAG, "Greater than max of delta y - not a click!");
        } else {
            Log.v(TAG, "Smaller than max of delta y - a click!");
            if (null != onFaceDownCardClickListener) {
                Log.v(TAG, "Calling on face down card click listener");
                onFaceDownCardClickListener.onFaceDownCardClick();
            } else {
                Log.e(TAG, "On face down card click listener is missing!");
            }
        }
        deltaY = clickStartedOnY = 0;
        break;
    case MotionEvent.ACTION_POINTER_DOWN:
        break;
    case MotionEvent.ACTION_POINTER_UP:
        break;
    case MotionEvent.ACTION_MOVE:
        deltaY += Math.abs(y - clickStartedOnY);
        Log.v(TAG, "Current delta: " + String.valueOf(deltaY));
        break;
    }
    return false;
}

From source file:co.dift.ui.SwipeToAction.java

/** Private methods **/
private void init() {
    gestureDetector = new GestureDetectorCompat(recyclerView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {
                @Override//from  ww  w .  ja  va2 s.  c o m
                public void onLongPress(MotionEvent e) {
                    logger.d("onLongPress called!");
                    if (touchedViewHolder != null) {
                        swipeListener.onLongClick(touchedViewHolder.getItemData());
                    }
                }

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    logger.d("onSingleTapUp called!");
                    if (touchedViewHolder != null) {
                        swipeListener.onClick(touchedViewHolder.getItemData());
                        return true;
                    }
                    return super.onSingleTapUp(e);
                }
            });

    recyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() {

        @Override
        public boolean onInterceptTouchEvent(RecyclerView rv, MotionEvent ev) {
            gestureDetector.onTouchEvent(ev);

            switch (ev.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN: {
                // starting point
                downX = ev.getX();
                downY = ev.getY();

                downTime = new Date().getTime();

                // which item are we touching
                resolveItem(downX, downY);

                break;
            }

            case MotionEvent.ACTION_UP: {
                upX = ev.getX();
                upY = ev.getY();
                upTime = new Date().getTime();

                resolveState();
                break;
            }

            case MotionEvent.ACTION_MOVE: {
                final float x = ev.getX();
                final float dx = x - downX;

                if (!shouldMove(dx))
                    break;

                // current position. moving only over x-axis
                frontViewLastX = frontViewX + dx + (dx > 0 ? -getRevealThreshold() : getRevealThreshold());
                //                        logger.d("dx: %f, frontViewLastX: %f", dx, frontViewLastX);
                if (maxSwipeXPosition != null) {
                    if (frontViewLastX > 0 && frontViewLastX > maxSwipeXPosition) {
                        frontViewLastX = maxSwipeXPosition;
                    } else if (frontViewLastX <= -maxSwipeXPosition) {
                        frontViewLastX = -maxSwipeXPosition;
                    }
                }
                frontView.setX(frontViewLastX);

                if (frontViewLastX > 0) {
                    revealRight();
                } else {
                    revealLeft();
                }

                break;
            }

            case MotionEvent.ACTION_CANCEL: {
                resolveState();

                break;
            }
            }

            return false;
        }

        @Override
        public void onTouchEvent(RecyclerView rv, MotionEvent e) {
            logger.d("onTouchEvent called");
        }

        @Override
        public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) {
            logger.d("onRequestDisallowInterceptTouchEvent called");
        }
    });
}

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

/** Called when the activity is first created. */
@Override/*from w  w  w .  j a  v  a  2 s  .  c  om*/
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;
    }
    /*      File outputFile = new File(DIR_NAME);
    File outputDir = this.getCacheDir(); // context being the Activity pointer
    try {
       outputFile = File.createTempFile("temp", "txt", outputDir);
    } catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
    }
            
    ContextWrapper cw = new ContextWrapper(this);
    File directory = cw.getExternalFilesDir(null);
    txtfile = new File(directory,"temp.txt");
     */
    multiTouchView = new View(this);
    setContentView(R.layout.multitouchxy);
    multiTouchView = (RelativeLayout) findViewById(R.id.multitouchxy);
    Button loadPresetButton = (Button) findViewById(R.id.load_button);
    this.createLoadListener(loadPresetButton);
    multiTouchView.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;
            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) / multiTouchView.getWidth();
                            touchY[id] = 1 - (event.getY(i) / multiTouchView.getHeight());
                            //TODO calculte interval Y
                            int a = Math.round(id / 440);

                            System.out.println("y " + a);

                            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());

                                // 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));
                            }
                            //write file
                            /*            System.out.println(String.format(
                                  "i1.%d 0 -2 %d", id, a));
                            writeToFile(String.format(
                                  "i1.%d 0 -2 %d", id, a));
                             */
                        }
                    }

                }

                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) {
                        touchX[id] = event.getX(i) / multiTouchView.getWidth();
                        touchY[id] = 1 - (event.getY(i) / multiTouchView.getHeight());

                    }

                    //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;
            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;
        }

    });

    setContentView(multiTouchView);

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

    csoundObj.addBinding(this);

    csoundObj.startCsound(f);
}

From source file:com.jifeng.touchgallery.GalleryWidget.GalleryViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    try {/*  w w  w.  j a v  a  2 s  .  c  o m*/
        float endX = 0;
        float endY = 0;
        if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
            // super.onInterceptTouchEvent(event);

            endX = event.getX();
            endY = event.getY();

            if (isAClick(startX, endX, startY, endY)) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClicked(mCurrentView, getCurrentItem());
                }
            } else {
                super.onInterceptTouchEvent(event);
            }
        }

        if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
            startX = event.getX();
            startY = event.getY();
        }

        float[] difference = handleMotionEvent(event);

        if (mCurrentView.pagerCanScroll()) {

            return super.onInterceptTouchEvent(event);

        } else {
            if (difference != null && mCurrentView.onRightSide && difference[0] < 0) // move right
            {
                return super.onInterceptTouchEvent(event);
            }
            if (difference != null && mCurrentView.onLeftSide && difference[0] > 0) // move left
            {
                return super.onInterceptTouchEvent(event);
            }
            if (difference == null && (mCurrentView.onLeftSide || mCurrentView.onRightSide)) {
                return super.onInterceptTouchEvent(event);
            }
        }
    } catch (IllegalArgumentException e) {
        // TODO Auto-generated catch block
        //   Log.i("11111", e.toString());
        e.printStackTrace();
    }
    return false;
}

From source file:com.liangxun.yuejiula.galleryweidget.GalleryViewPager.java

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    try {//from   w  w w. j  a v  a 2 s.c  om
        if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_UP) {
            float endX = event.getX();
            float endY = event.getY();
            if (isAClick(startX, endX, startY, endY)) {
                if (mOnItemClickListener != null) {
                    mOnItemClickListener.onItemClicked(mCurrentView, getCurrentItem());
                }
            } else {
                super.onInterceptTouchEvent(event);
            }
        }

        if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
            startX = event.getX();
            startY = event.getY();
        }

        float[] difference = handleMotionEvent(event);

        if (mCurrentView.pagerCanScroll()) {
            return super.onInterceptTouchEvent(event);
        } else {
            if (difference != null && mCurrentView.onRightSide && difference[0] < 0) //move right
            {
                return super.onInterceptTouchEvent(event);
            }
            if (difference != null && mCurrentView.onLeftSide && difference[0] > 0) //move left
            {
                return super.onInterceptTouchEvent(event);
            }
            if (difference == null && (mCurrentView.onLeftSide || mCurrentView.onRightSide)) {
                return super.onInterceptTouchEvent(event);
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
    return false;
}

From source file:org.skia.viewer.ViewerActivity.java

@Override
public boolean onTouch(View v, MotionEvent event) {
    int count = event.getPointerCount();
    for (int i = 0; i < count; i++) {
        final float x = event.getX(i);
        final float y = event.getY(i);
        final int owner = event.getPointerId(i);
        int action = event.getAction() & MotionEvent.ACTION_MASK;
        onTouched(mApplication.getNativeHandle(), owner, action, x, y);
    }//from   ww  w  .ja v  a 2  s .  co m
    return true;
}

From source file:us.shandian.blacklight.ui.statuses.TimeLineFragment.java

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

    switch (ev.getAction() & MotionEvent.ACTION_MASK) {
    case MotionEvent.ACTION_DOWN:
        mLastY = ev.getY();/*  ww w  .jav a  2s  .c o  m*/
        break;
    case MotionEvent.ACTION_MOVE:
        if (mLastY == -1.0f)
            break;

        float y = ev.getY();

        if (!mNewHidden && y < mLastY) {
            mNew.clearAnimation();

            TranslateAnimation anim = new TranslateAnimation(0, 0, 0, mList.getHeight() - mNew.getTop());
            anim.setFillAfter(true);
            anim.setDuration(400);

            mNew.setAnimation(anim);
            anim.startNow();

            mNewHidden = true;
        } else if (mNewHidden && y > mLastY) {
            mNew.clearAnimation();

            TranslateAnimation anim = new TranslateAnimation(0, 0, mList.getHeight() - mNew.getTop(), 0);
            anim.setFillAfter(true);
            anim.setDuration(400);

            mNew.setAnimation(anim);
            anim.startNow();

            mNewHidden = false;
        }

        mLastY = y;
        break;
    case MotionEvent.ACTION_UP:
    case MotionEvent.ACTION_CANCEL:
        mLastY = -1.0f;
        break;
    }

    return false;
}

From source file:com.github.omadahealth.slidepager.lib.SlidePager.java

/**
 * Fixes for "java.lang.IndexOutOfBoundsException Invalid index 0, size is 0"
 * on "android.support.v4.view.ViewPager.performDrag"
 *///from ww  w  .  java  2 s. c  o  m
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    Log.i(TAG, "SlidePager click :" + (ev.getAction() & MotionEvent.ACTION_MASK));
    if (getAdapter() == null || getAdapter().getCount() == 0) {
        Log.i(TAG, "SlidePager onInterceptTouchEvent returned false because adapter is null or empty:");
        return false;
    }
    onTouchEvent(ev);
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (Exception e) {
        Log.e(TAG, e.toString());
    }
    return false;
}