Example usage for android.widget ImageView setOnTouchListener

List of usage examples for android.widget ImageView setOnTouchListener

Introduction

In this page you can find the example usage for android.widget ImageView setOnTouchListener.

Prototype

public void setOnTouchListener(OnTouchListener l) 

Source Link

Document

Register a callback to be invoked when a touch event is sent to this view.

Usage

From source file:com.frank.protean.photoview.PhotoViewAttacher.java

public PhotoViewAttacher(ImageView imageView) {
    mImageView = imageView;/*from  ww w . ja  v  a 2s.c  om*/
    imageView.setOnTouchListener(this);
    imageView.addOnLayoutChangeListener(this);

    if (imageView.isInEditMode()) {
        return;
    }

    mBaseRotation = 0.0f;

    // Create Gesture Detectors...
    mScaleDragDetector = new CustomGestureDetector(imageView.getContext(), onGestureListener);

    mGestureDetector = new GestureDetector(imageView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    if (mLongClickListener != null) {
                        mLongClickListener.onLongClick(mImageView);
                    }
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    if (mSingleFlingListener != null) {
                        if (getScale() > DEFAULT_MIN_SCALE) {
                            return false;
                        }

                        if (MotionEventCompat.getPointerCount(e1) > SINGLE_TOUCH
                                || MotionEventCompat.getPointerCount(e2) > SINGLE_TOUCH) {
                            return false;
                        }

                        return mSingleFlingListener.onFling(e1, e2, velocityX, velocityY);
                    }
                    return false;
                }
            });

    mGestureDetector.setOnDoubleTapListener(new GestureDetector.OnDoubleTapListener() {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if (mOnClickListener != null) {
                mOnClickListener.onClick(mImageView);
            }
            final RectF displayRect = getDisplayRect();

            final float x = e.getX(), y = e.getY();

            if (mViewTapListener != null) {
                mViewTapListener.onViewTap(mImageView, x, y);
            }

            if (displayRect != null) {

                // Check to see if the user tapped on the photo
                if (displayRect.contains(x, y)) {

                    float xResult = (x - displayRect.left) / displayRect.width();
                    float yResult = (y - displayRect.top) / displayRect.height();

                    if (mPhotoTapListener != null) {
                        mPhotoTapListener.onPhotoTap(mImageView, xResult, yResult);
                    }
                    return true;
                } else {
                    if (mOutsidePhotoTapListener != null) {
                        mOutsidePhotoTapListener.onOutsidePhotoTap(mImageView);
                    }
                }
            }
            return false;
        }

        @Override
        public boolean onDoubleTap(MotionEvent ev) {
            try {
                float scale = getScale();
                float x = ev.getX();
                float y = ev.getY();

                if (scale < getMediumScale()) {
                    setScale(getMediumScale(), x, y, true);
                } else if (scale >= getMediumScale() && scale < getMaximumScale()) {
                    setScale(getMaximumScale(), x, y, true);
                } else {
                    setScale(getMinimumScale(), x, y, true);
                }
            } catch (ArrayIndexOutOfBoundsException e) {
                // Can sometimes happen when getX() and getY() is called
            }

            return true;
        }

        @Override
        public boolean onDoubleTapEvent(MotionEvent e) {
            // Wait for the confirmed onDoubleTap() instead
            return false;
        }
    });
}

From source file:it.configure.imageloader.zoom.PhotoViewAttacher.java

public PhotoViewAttacher(ImageView imageView) {
    mImageView = new WeakReference<ImageView>(imageView);

    imageView.setOnTouchListener(this);

    mViewTreeObserver = imageView.getViewTreeObserver();
    mViewTreeObserver.addOnGlobalLayoutListener(this);

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (!imageView.isInEditMode()) {
        // Create Gesture Detectors...
        mScaleDragDetector = VersionedGestureDetector.newInstance(imageView.getContext(), this);

        mGestureDetector = new GestureDetector(imageView.getContext(),
                new GestureDetector.SimpleOnGestureListener() {

                    // forward long click listener
                    @Override/*from   ww  w. j av a2  s.  co  m*/
                    public void onLongPress(MotionEvent e) {
                        if (null != mLongClickListener) {
                            mLongClickListener.onLongClick(mImageView.get());
                        }
                    }
                });

        mGestureDetector.setOnDoubleTapListener(this);

        // Finally, update the UI so that we're zoomable
        setZoomable(true);
    }
}

From source file:com.coincollection.ReorderAdapter.java

@Override
public void onBindViewHolder(final ReorderViewHolder holder, int position) {

    // TODO Copied from the adapter that handles mainActivity, refactor it
    // so that this code only lives in one place
    CollectionListInfo item = mItems.get(position);

    String tableName = item.getName();

    int total = item.getCollected();
    if (tableName != null) {

        //Get the image
        ImageView image = (ImageView) holder.view.findViewById(R.id.imageView1);
        if (image != null) {
            image.setBackgroundResource(item.getCoinImageIdentifier());
        }//from w  w  w . ja v a  2  s .c  o m

        TextView tt = (TextView) holder.view.findViewById(R.id.textView1);
        if (tt != null) {
            tt.setText(tableName);
        }

        TextView mt = (TextView) holder.view.findViewById(R.id.textView2);
        if (mt != null) {
            mt.setText(total + "/" + item.getMax());
        }

        TextView bt = (TextView) holder.view.findViewById(R.id.textView3);
        if (total >= item.getMax()) {
            // The collection is complete
            if (bt != null) {
                bt.setText("Collection Complete!");
            }
        } else {
            bt.setText("");
        }

        // Start a drag whenever the handle view is touched
        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {

                if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
                    mDragStartListener.onStartDrag(holder);
                }
                return false;
            }
        });
    }

}

From source file:com.ycl.framework.photoview.PhotoViewAttacher.java

public PhotoViewAttacher(final ImageView imageView) {
    mImageView = new WeakReference<ImageView>(imageView);

    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    ViewTreeObserver observer = imageView.getViewTreeObserver();
    if (null != observer)
        observer.addOnGlobalLayoutListener(this);

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (imageView.isInEditMode()) {
        return;/* w  w  w . j av a  2s .c o  m*/
    }
    // Create Gesture Detectors...
    mScaleDragDetector = com.ycl.framework.photoview.gestures.VersionedGestureDetector
            .newInstance(imageView.getContext(), this);

    mGestureDetector = new GestureDetector(imageView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    if (null != mLongClickListener) {
                        mLongClickListener.onLongClick(getImageView());
                    }
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));

    // Finally, update the UI so that we're zoomable
    setZoomable(true);
}

From source file:com.zirkler.czannotationviewsample.AnnotationView.PhotoViewAttacher.java

public PhotoViewAttacher(ImageView imageView, boolean zoomable) {
    mImageView = new WeakReference<>(imageView);

    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    ViewTreeObserver observer = imageView.getViewTreeObserver();
    if (null != observer)
        observer.addOnGlobalLayoutListener(this);

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (imageView.isInEditMode()) {
        return;//from   w  w w.  j  a  v a 2 s  .  c  o  m
    }
    // Create Gesture Detectors...
    mScaleDragDetector = VersionedGestureDetector.newInstance(imageView.getContext(), this);

    mGestureDetector = new GestureDetector(imageView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    if (null != mLongClickListener) {
                        mLongClickListener.onLongClick(getImageView(), e);
                    }
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    if (mSingleFlingListener != null) {
                        if (getScale() > DEFAULT_MIN_SCALE) {
                            return false;
                        }

                        if (MotionEventCompat.getPointerCount(e1) > SINGLE_TOUCH
                                || MotionEventCompat.getPointerCount(e2) > SINGLE_TOUCH) {
                            return false;
                        }

                        return mSingleFlingListener.onFling(e1, e2, velocityX, velocityY);
                    }
                    return false;
                }
            });

    mBaseRotation = 0.0f;

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}

From source file:com.sflib.CustomView.photoview.PhotoViewAttacher.java

public PhotoViewAttacher(ImageView imageView, boolean zoomable) {
    mImageView = new WeakReference<ImageView>(imageView);

    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    ViewTreeObserver observer = imageView.getViewTreeObserver();
    if (null != observer)
        observer.addOnGlobalLayoutListener(this);

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (imageView.isInEditMode()) {
        return;//from w  w  w. ja  v  a2 s.  com
    }
    // Create Gesture Detectors...
    mScaleDragDetector = VersionedGestureDetector.newInstance(imageView.getContext(), this);

    mGestureDetector = new GestureDetector(imageView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    if (null != mLongClickListener) {
                        mLongClickListener.onLongClick(getImageView());
                    }
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    if (mSingleFlingListener != null) {
                        if (getScale() > DEFAULT_MIN_SCALE) {
                            return false;
                        }

                        if (MotionEventCompat.getPointerCount(e1) > SINGLE_TOUCH
                                || MotionEventCompat.getPointerCount(e2) > SINGLE_TOUCH) {
                            return false;
                        }

                        return mSingleFlingListener.onFling(e1, e2, velocityX, velocityY);
                    }
                    return false;
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));
    mBaseRotation = 0.0f;

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}

From source file:com.github.lakeshire.photoview.PhotoViewAttacher.java

public PhotoViewAttacher(ImageView imageView, boolean zoomable) {
    mImageView = new WeakReference<>(imageView);

    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    ViewTreeObserver observer = imageView.getViewTreeObserver();
    if (null != observer) {
        observer.addOnGlobalLayoutListener(this);
    }/*from w w w.ja v a2 s .  c  o  m*/

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (imageView.isInEditMode()) {
        return;
    }
    // Create Gesture Detectors...
    mScaleDragDetector = VersionedGestureDetector.newInstance(imageView.getContext(), this);

    mGestureDetector = new GestureDetector(imageView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    if (null != mLongClickListener) {
                        mLongClickListener.onLongClick(getImageView());
                    }
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    if (mSingleFlingListener != null) {
                        if (getScale() > DEFAULT_MIN_SCALE) {
                            return false;
                        }

                        if (MotionEventCompat.getPointerCount(e1) > SINGLE_TOUCH
                                || MotionEventCompat.getPointerCount(e2) > SINGLE_TOUCH) {
                            return false;
                        }

                        return mSingleFlingListener.onFling(e1, e2, velocityX, velocityY);
                    }
                    return false;
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));
    mBaseRotation = 0.0f;

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}

From source file:cn.golden.pinchzoomcanvasview.PinchZoomCanvasViewAttacher.java

public PinchZoomCanvasViewAttacher(ImageView imageView, boolean zoomable) {
    mImageView = new WeakReference<>(imageView);

    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    ViewTreeObserver observer = imageView.getViewTreeObserver();
    if (null != observer)
        observer.addOnGlobalLayoutListener(this);

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (imageView.isInEditMode()) {
        return;/*from w  ww  .  jav  a  2  s.  c o m*/
    }
    // Create Gesture Detectors...
    mScaleDragDetector = VersionedGestureDetector.newInstance(imageView.getContext(), this);

    mGestureDetector = new GestureDetector(imageView.getContext(),
            new GestureDetector.SimpleOnGestureListener() {

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    if (null != mLongClickListener) {
                        mLongClickListener.onLongClick(getImageView());
                    }
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

                    if (mSingleFlingListener != null) {
                        if (getScale() > DEFAULT_MIN_SCALE) {
                            return false;
                        }

                        if (MotionEventCompat.getPointerCount(e1) > SINGLE_TOUCH
                                || MotionEventCompat.getPointerCount(e2) > SINGLE_TOUCH) {
                            return false;
                        }
                        return mSingleFlingListener.onFling(e1, e2, velocityX, velocityY);
                    }
                    return false;
                }
            });

    //        mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));
    mBaseRotation = 0.0f;

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}

From source file:com.orbar.pxdemo.ImageView.FivePXImageFragment.java

private void setupMap(String latitude, String longitude, String imageName, String imageDescription) {

    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    // get references to transparent image and ScrollView
    final ScrollView mainScrollView = (ScrollView) rootView.findViewById(R.id.image_details_scrollView);
    ImageView transparentImageView = (ImageView) rootView.findViewById(R.id.transparent_image);

    // allows scrolling on the map independently of the scrollView
    transparentImageView.setOnTouchListener(new View.OnTouchListener() {

        @Override/*from   w  ww.  java2 s. c om*/
        public boolean onTouch(View v, MotionEvent event) {
            int action = event.getAction();

            switch (action) {
            case MotionEvent.ACTION_DOWN:
                // Disallow ScrollView to intercept touch events.
                mainScrollView.requestDisallowInterceptTouchEvent(true);
                // Disable touch on transparent view
                return false;

            case MotionEvent.ACTION_UP:
                // Allow ScrollView to intercept touch events.
                mainScrollView.requestDisallowInterceptTouchEvent(false);
                return true;

            case MotionEvent.ACTION_MOVE:
                mainScrollView.requestDisallowInterceptTouchEvent(true);
                return false;

            default:
                return true;
            }
        }

    });

    LatLng imageLocation = new LatLng(Double.valueOf(latitude), Double.valueOf(longitude));
    LatLng mapCenter = new LatLng(Double.valueOf(latitude) - 1, Double.valueOf(longitude));

    map.addMarker(new MarkerOptions().position(imageLocation).snippet(imageDescription).title(imageName));

    // Move the camera instantly to position with a zoom of 6.
    map.moveCamera(CameraUpdateFactory.newLatLngZoom(mapCenter, 6));

}

From source file:com.example.g40_70m.framedemo.view.photoview.PhotoViewAttacher.java

public PhotoViewAttacher(ImageView imageView, boolean zoomable) {
    mImageView = new WeakReference<>(imageView);

    imageView.setDrawingCacheEnabled(true);
    imageView.setOnTouchListener(this);

    ViewTreeObserver observer = imageView.getViewTreeObserver();
    if (null != observer)
        observer.addOnGlobalLayoutListener(this);

    // Make sure we using MATRIX Scale Type
    setImageViewScaleTypeMatrix(imageView);

    if (imageView.isInEditMode()) {
        return;//w  w  w .  j av a2s .  c  o  m
    }
    // Create Gesture Detectors...
    mScaleDragDetector = VersionedGestureDetector.newInstance(imageView.getContext(), this);
    mGestureDetector = new GestureDetector(imageView.getContext().getApplicationContext(),
            new GestureDetector.SimpleOnGestureListener() {

                @Override
                public boolean onDown(MotionEvent e) {
                    downTime = System.currentTimeMillis();
                    singeClick = !singeClick;
                    return super.onDown(e);
                }

                @Override
                public boolean onSingleTapUp(MotionEvent e) {
                    if (System.currentTimeMillis() - downTime < 200) {
                        getImageView().postDelayed(new Runnable() {//300? ?
                            @Override
                            public void run() {
                                if (singeClick) {
                                    onClickListener.onClick(getImageView());
                                }
                            }
                        }, 300);
                    }
                    downTime = 0;
                    return super.onSingleTapUp(e);
                }

                // forward long click listener
                @Override
                public void onLongPress(MotionEvent e) {
                    singeClick = false;
                    if (null != mLongClickListener) {
                        mLongClickListener.onLongClick(getImageView());
                    }
                }

                @Override
                public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
                    if (mSingleFlingListener != null) {
                        if (getScale() > DEFAULT_MIN_SCALE) {
                            return false;
                        }
                        if (MotionEventCompat.getPointerCount(e1) > SINGLE_TOUCH
                                || MotionEventCompat.getPointerCount(e2) > SINGLE_TOUCH) {
                            return false;
                        }

                        return mSingleFlingListener.onFling(e1, e2, velocityX, velocityY);
                    }
                    return false;
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));
    mBaseRotation = 0.0f;

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}