Java Code Examples for android.widget.ImageView#getViewTreeObserver()

The following examples show how to use android.widget.ImageView#getViewTreeObserver() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DeferredRequestCreator.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onPreDraw() {
    ImageView target = this.target.get();
    if (target == null) {
        return true;
    }
    ViewTreeObserver vto = target.getViewTreeObserver();
    if (!vto.isAlive()) {
        return true;
    }

    int width = target.getWidth();
    int height = target.getHeight();

    if (width <= 0 || height <= 0) {
        return true;
    }

    vto.removeOnPreDrawListener(this);

    this.creator.unfit().resize(width, height).into(target, callback);
    return true;
}
 
Example 2
Source File: DeferredRequestCreator.java    From DoraemonKit with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onPreDraw() {
    ImageView target = this.target.get();
    if (target == null) {
        return true;
    }
    ViewTreeObserver vto = target.getViewTreeObserver();
    if (!vto.isAlive()) {
        return true;
    }

    int width = target.getWidth();
    int height = target.getHeight();

    if (width <= 0 || height <= 0) {
        return true;
    }

    vto.removeOnPreDrawListener(this);

    this.creator.unfit().resize(width, height).into(target, callback);
    return true;
}
 
Example 3
Source File: DeferredRequestCreatorTest.java    From picasso with Apache License 2.0 6 votes vote down vote up
@Test public void initWhileDetachedAddsAttachListenerWhichDefersPreDrawListener() {
  ImageView target = mockFitImageViewTarget(true);
  when(target.getWindowToken()).thenReturn(null);
  ViewTreeObserver observer = target.getViewTreeObserver();
  DeferredRequestCreator request =
      new DeferredRequestCreator(mock(RequestCreator.class), target, null);
  verify(target).addOnAttachStateChangeListener(request);
  verifyNoMoreInteractions(observer);

  // Attach and ensure we defer to the pre-draw listener.
  request.onViewAttachedToWindow(target);
  verify(observer).addOnPreDrawListener(request);

  // Detach and ensure we remove the pre-draw listener from the original VTO.
  request.onViewDetachedFromWindow(target);
  verify(observer).removeOnPreDrawListener(request);
}
 
Example 4
Source File: PhotoViewAttacher.java    From PicturePicker with Apache License 2.0 5 votes vote down vote up
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
                    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);
    }
}
 
Example 5
Source File: PhotoViewAttacher.java    From MoeGallery with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link android.view.View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 6
Source File: PhotoViewAttacher.java    From AndroidPickPhotoDialog with Apache License 2.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 7
Source File: PhotoViewAttacher.java    From school_shop with MIT License 5 votes vote down vote up
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
					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);
	}
}
 
Example 8
Source File: PhotoViewAttacher.java    From UltimateAndroid with Apache License 2.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link android.view.View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link com.marshalchen.common.uimodule.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 9
Source File: PhotoViewAttacher.java    From Social with Apache License 2.0 5 votes vote down vote up
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
					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);
	}
}
 
Example 10
Source File: PhotoViewAttacher.java    From Tweetin with Apache License 2.0 5 votes vote down vote up
public PhotoViewAttacher(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;
    }
    // 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());
                    }
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));

    // Finally, update the UI so that we're zoomable
    setZoomable(true);
}
 
Example 11
Source File: CropPhotoViewAttacher.java    From ImageSelector with Apache License 2.0 5 votes vote down vote up
/**
* Clean-up the resources attached to this object. This needs to be called when the ImageView is
* no longer used. A good example is from {@link View#onDetachedFromWindow()} or
* from {@link android.app.Activity#onDestroy()}.
*/
  @SuppressWarnings("deprecation")
  public void cleanup() {
   if (null == mImageView) {
	   return; // cleanup already done
   }

   final ImageView imageView = mImageView.get();

   if (null != imageView) {
	   // Remove this as a global layout listener
	   ViewTreeObserver observer = imageView.getViewTreeObserver();
	   if (null != observer && observer.isAlive()) {
		   observer.removeGlobalOnLayoutListener(this);
	   }

	   // Remove the ImageView's reference to this
	   imageView.setOnTouchListener(null);

	   // make sure a pending fling runnable won't be run
	   cancelFling();
   }

   if (null != mGestureDetector) {
	   mGestureDetector.setOnDoubleTapListener(null);
   }

   // Clear listeners too
   mMatrixChangeListener = null;
   mPhotoTapListener = null;
   mViewTapListener = null;

   // Finally, clear ImageView
   mImageView = null;
  }
 
Example 12
Source File: ImageViewScaler.java    From MultiView with Apache License 2.0 5 votes vote down vote up
public ImageViewScaler(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;
    }
    // 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());
                    }
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}
 
Example 13
Source File: PhotoViewAttacher.java    From GalleryFinal with Apache License 2.0 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called when the ImageView is
 * no longer used. A good example is from {@link View#onDetachedFromWindow()} or
 * from {@link android.app.Activity#onDestroy()}. This is automatically called if you are using
 * {@link cn.finalteam.galleryfinal.widget.zoonview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 14
Source File: PhotoViewAttacher.java    From Dashboard with MIT License 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called
 * when the ImageView is no longer used. A good example is from
 * {@link android.view.View#onDetachedFromWindow()} or from
 * {@link android.app.Activity#onDestroy()}. This is automatically called if
 * you are using {@link com.antonioleiva.materialeverywhere.uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public final void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 15
Source File: DeferredRequestCreatorTest.java    From picasso with Apache License 2.0 5 votes vote down vote up
@Test public void onLayoutSkipsIfViewIsAttachedAndViewTreeObserverIsDead() {
  ImageView target = mockFitImageViewTarget(false);
  RequestCreator creator = mock(RequestCreator.class);
  DeferredRequestCreator request = new DeferredRequestCreator(creator, target, null);
  ViewTreeObserver viewTreeObserver = target.getViewTreeObserver();
  request.onPreDraw();
  verify(viewTreeObserver).addOnPreDrawListener(request);
  verify(viewTreeObserver).isAlive();
  verifyNoMoreInteractions(viewTreeObserver);
  verifyZeroInteractions(creator);
}
 
Example 16
Source File: PhotoViewAttacher.java    From BlackLight with GNU General Public License v3.0 5 votes vote down vote up
public PhotoViewAttacher(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;
    }
    // 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());
                    }
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));

    // Finally, update the UI so that we're zoomable
    setZoomable(true);
}
 
Example 17
Source File: PhotoViewAttacher.java    From Dashboard with MIT License 5 votes vote down vote up
/**
 * Clean-up the resources attached to this object. This needs to be called
 * when the ImageView is no longer used. A good example is from
 * {@link android.view.View#onDetachedFromWindow()} or from
 * {@link android.app.Activity#onDestroy()}. This is automatically called if
 * you are using {@link com.antonioleiva.materialeverywhere.uk.co.senab.photoview.PhotoView}.
 */
@SuppressWarnings("deprecation")
public final void cleanup() {
    if (null == mImageView) {
        return; // cleanup already done
    }

    final ImageView imageView = mImageView.get();

    if (null != imageView) {
        // Remove this as a global layout listener
        ViewTreeObserver observer = imageView.getViewTreeObserver();
        if (null != observer && observer.isAlive()) {
            observer.removeGlobalOnLayoutListener(this);
        }

        // Remove the ImageView's reference to this
        imageView.setOnTouchListener(null);

        // make sure a pending fling runnable won't be run
        cancelFling();
    }

    if (null != mGestureDetector) {
        mGestureDetector.setOnDoubleTapListener(null);
    }

    // Clear listeners too
    mMatrixChangeListener = null;
    mPhotoTapListener = null;
    mViewTapListener = null;

    // Finally, clear ImageView
    mImageView = null;
}
 
Example 18
Source File: DeferredRequestCreator.java    From DoraemonKit with Apache License 2.0 5 votes vote down vote up
void cancel() {
    callback = null;
    ImageView target = this.target.get();
    if (target == null) {
        return;
    }
    ViewTreeObserver vto = target.getViewTreeObserver();
    if (!vto.isAlive()) {
        return;
    }
    vto.removeOnPreDrawListener(this);
}
 
Example 19
Source File: PhotoViewAttacher.java    From MoeGallery with GNU General Public License v3.0 5 votes vote down vote up
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;
    }
    // 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());
                    }
                }
            });

    mGestureDetector.setOnDoubleTapListener(new DefaultOnDoubleTapListener(this));

    // Finally, update the UI so that we're zoomable
    setZoomable(zoomable);
}
 
Example 20
Source File: PhotoViewAttacher.java    From OmniList with GNU Affero General Public License v3.0 4 votes vote down vote up
private 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;
    }
    // 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);
}