/** * Constructor that defaults {@code waitForLayout} to {@code false}. */ public ViewTarget(@NonNull T view) { this.view = Preconditions.checkNotNull(view); sizeDeterminer = new SizeDeterminer(view); }
/** * Determines the size of the view by first checking {@link android.view.View#getWidth()} and * {@link android.view.View#getHeight()}. If one or both are zero, it then checks the view's * {@link LayoutParams}. If one or both of the params width and height are less than or equal to * zero, it then adds an {@link android.view.ViewTreeObserver.OnPreDrawListener} which waits until * the view has been measured before calling the callback with the view's drawn width and height. * * @param cb {@inheritDoc} */ @CallSuper @Override public void getSize(@NonNull SizeReadyCallback cb) { sizeDeterminer.getSize(cb); }
@Override public boolean onPreDraw() { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "OnGlobalLayoutListener called attachStateListener=" + this); } SizeDeterminer sizeDeterminer = sizeDeterminerRef.get(); if (sizeDeterminer != null) { sizeDeterminer.checkCurrentDimens(); } return true; } }
+ " dimensions."); return getMaxDisplayLength(view.getContext());
void getSize(SizeReadyCallback cb) { int currentWidth = getTargetWidth(); int currentHeight = getTargetHeight(); if (isViewStateAndSizeValid(currentWidth, currentHeight)) { cb.onSizeReady(currentWidth, currentHeight); return; } // We want to notify callbacks in the order they were added and we only expect one or two // callbacks to be added a time, so a List is a reasonable choice. if (!cbs.contains(cb)) { cbs.add(cb); } if (layoutListener == null) { ViewTreeObserver observer = view.getViewTreeObserver(); layoutListener = new SizeDeterminerLayoutListener(this); observer.addOnPreDrawListener(layoutListener); } }
/** * @param waitForLayout If set to {@code true}, Glide will always wait for any pending layout pass * before checking for the size a View. If set to {@code false} Glide will only wait for a pending * layout pass if it's unable to resolve the size from layout parameters or an existing View size. * Because setting this parameter to {@code true} forces Glide to wait for the layout pass to * occur before starting the load, setting this parameter to {@code true} can cause flashing in * some cases and should be used sparingly. If layout parameters are set to fixed sizes, they will * still be used instead of the View's dimensions even if this parameter is set to {@code true}. * This parameter is a fallback only. */ public ViewTarget(T view, boolean waitForLayout) { this.view = Preconditions.checkNotNull(view); sizeDeterminer = new SizeDeterminer(view, waitForLayout); }
@Synthetic void checkCurrentDimens() { if (cbs.isEmpty()) { return; } int currentWidth = getTargetWidth(); int currentHeight = getTargetHeight(); if (!isViewStateAndSizeValid(currentWidth, currentHeight)) { return; } notifyCbs(currentWidth, currentHeight); clearCallbacksAndListener(); }
private boolean isViewStateAndSizeValid(int width, int height) { return isDimensionValid(width) && isDimensionValid(height); }
private int getTargetWidth() { int horizontalPadding = view.getPaddingLeft() + view.getPaddingRight(); LayoutParams layoutParams = view.getLayoutParams(); int layoutParamSize = layoutParams != null ? layoutParams.width : PENDING_SIZE; return getTargetDimen(view.getWidth(), layoutParamSize, horizontalPadding); }
@Override public void onLoadCleared(Drawable placeholder) { super.onLoadCleared(placeholder); sizeDeterminer.clearCallbacksAndListener(); }
/** * Determines the size of the view by first checking {@link android.view.View#getWidth()} and * {@link android.view.View#getHeight()}. If one or both are zero, it then checks the view's * {@link LayoutParams}. If one or both of the params width and height are less than or equal to * zero, it then adds an {@link android.view.ViewTreeObserver.OnPreDrawListener} which waits until * the view has been measured before calling the callback with the view's drawn width and height. * * @param cb {@inheritDoc} */ @Override public void getSize(SizeReadyCallback cb) { sizeDeterminer.getSize(cb); }
@Override public boolean onPreDraw() { if (Log.isLoggable(TAG, Log.VERBOSE)) { Log.v(TAG, "OnGlobalLayoutListener called listener=" + this); } SizeDeterminer sizeDeterminer = sizeDeterminerRef.get(); if (sizeDeterminer != null) { sizeDeterminer.checkCurrentDimens(); } return true; } }
private int getTargetHeight() { int verticalPadding = view.getPaddingTop() + view.getPaddingBottom(); LayoutParams layoutParams = view.getLayoutParams(); int layoutParamSize = layoutParams != null ? layoutParams.height : PENDING_SIZE; return getTargetDimen(view.getHeight(), layoutParamSize, verticalPadding); }
private void checkCurrentDimens() { if (cbs.isEmpty()) { return; } int currentWidth = getViewWidthOrParam(); int currentHeight = getViewHeightOrParam(); if (!isSizeValid(currentWidth) || !isSizeValid(currentHeight)) { return; } notifyCbs(currentWidth, currentHeight); // Keep a reference to the layout listener and remove it here // rather than having the observer remove itself because the observer // we add the listener to will be almost immediately merged into // another observer and will therefore never be alive. If we instead // keep a reference to the listener and remove it here, we get the // current view tree observer and should succeed. ViewTreeObserver observer = view.getViewTreeObserver(); if (observer.isAlive()) { observer.removeOnPreDrawListener(layoutListener); } layoutListener = null; }
/** * Determines the size of the view by first checking {@link View#getWidth()} and * {@link View#getHeight()}. If one or both are zero, it then checks the view's * {@link LayoutParams}. If one or both of the params width and height are less than or * equal to zero, it then adds an {@link ViewTreeObserver.OnPreDrawListener} which waits until the view * has been measured before calling the callback with the view's drawn width and height. * * @param cb {@inheritDoc} */ @Override public void getSize(SizeReadyCallback cb) { sizeDeterminer.getSize(cb); }
public ViewTarget(T view) { if (view == null) { throw new NullPointerException("View must not be null!"); } this.view = view; sizeDeterminer = new SizeDeterminer(view); }
+ " dimensions."); return getMaxDisplayLength(view.getContext());
void getSize(@NonNull SizeReadyCallback cb) { int currentWidth = getTargetWidth(); int currentHeight = getTargetHeight(); if (isViewStateAndSizeValid(currentWidth, currentHeight)) { cb.onSizeReady(currentWidth, currentHeight); return; } // We want to notify callbacks in the order they were added and we only expect one or two // callbacks to be added a time, so a List is a reasonable choice. if (!cbs.contains(cb)) { cbs.add(cb); } if (layoutListener == null) { ViewTreeObserver observer = view.getViewTreeObserver(); layoutListener = new SizeDeterminerLayoutListener(this); observer.addOnPreDrawListener(layoutListener); } }
private boolean isViewStateAndSizeValid(int width, int height) { return isDimensionValid(width) && isDimensionValid(height); }
private int getTargetWidth() { int horizontalPadding = view.getPaddingLeft() + view.getPaddingRight(); LayoutParams layoutParams = view.getLayoutParams(); int layoutParamSize = layoutParams != null ? layoutParams.width : PENDING_SIZE; return getTargetDimen(view.getWidth(), layoutParamSize, horizontalPadding); }