Example usage for android.view View getWidth

List of usage examples for android.view View getWidth

Introduction

In this page you can find the example usage for android.view View getWidth.

Prototype

@ViewDebug.ExportedProperty(category = "layout")
public final int getWidth() 

Source Link

Document

Return the width of your view.

Usage

From source file:android.support.v7.widget.RecyclerViewEx.java

/***
 * Touchfling?view?/*from  w  ww .  j av a  2 s . c o m*/
 */
void adjustPosition(int spanX) {
    int childCount = getChildCount();
    int[] lvLocationOnScreen = new int[2];
    int[] childLocationOnScreen = new int[2];
    if (childCount > 0) {
        getLocationOnScreen(lvLocationOnScreen);

        for (int i = 0; i < childCount; i++) {
            View child = getChildAt(i);
            child.getLocationOnScreen(childLocationOnScreen);
            int xStart = childLocationOnScreen[0];
            int xEnd = xStart + child.getWidth();
            if (spanX > 0) {
                if (xEnd >= lvLocationOnScreen[0] + getWidth() / 4
                        && xStart <= lvLocationOnScreen[0] + getWidth() / 4) {
                    smoothScrollToPosition(getChildPosition(child));
                    return;
                }
            } else {
                if (xStart <= lvLocationOnScreen[0] + getWidth() * 3 / 4
                        && xEnd >= lvLocationOnScreen[0] + getWidth() * 3 / 4) {
                    smoothScrollToPosition(getChildPosition(child));
                    return;
                }
            }
        }
        smoothScrollToPosition(getCenterXChildPosition());
    } else {
        setScrollState(SCROLL_STATE_IDLE);
    }
}

From source file:android.support.v7.widget.RecyclerViewEx.java

public boolean isChildInCenterX(View view) {
    int childCount = getChildCount();
    int[] lvLocationOnScreen = new int[2];
    int[] vLocationOnScreen = new int[2];
    getLocationOnScreen(lvLocationOnScreen);
    int middleX = lvLocationOnScreen[0] + getWidth() / 2;
    if (childCount > 0) {
        view.getLocationOnScreen(vLocationOnScreen);
        if (vLocationOnScreen[0] <= middleX && vLocationOnScreen[0] + view.getWidth() >= middleX) {
            return true;
        }//from   w  w w  . j  a va2 s  .  c o m
    }
    return false;
}

From source file:android.support.v7.widget.RecyclerViewEx.java

/**
 * Does not perform bounds checking. Used by internal methods that have already validated input.
 *//*from  ww  w  .j av  a 2s . co  m*/
void scrollByInternal(int x, int y) {
    int overscrollX = 0, overscrollY = 0;
    int hresult = 0, vresult = 0;
    consumePendingUpdateOperations();
    if (mAdapter != null) {
        eatRequestLayout();
        mRunningLayoutOrScroll = true;
        if (x != 0) {
            hresult = mLayout.scrollHorizontallyBy(x, mRecycler, mState);
            overscrollX = x - hresult;
        }
        if (y != 0) {
            vresult = mLayout.scrollVerticallyBy(y, mRecycler, mState);
            overscrollY = y - vresult;
        }
        if (supportsChangeAnimations()) {
            // Fix up shadow views used by changing animations
            int count = mChildHelper.getChildCount();
            for (int i = 0; i < count; i++) {
                View view = mChildHelper.getChildAt(i);
                ViewHolder holder = getChildViewHolder(view);
                if (holder != null && holder.mShadowingHolder != null) {
                    ViewHolder shadowingHolder = holder.mShadowingHolder;
                    View shadowingView = shadowingHolder != null ? shadowingHolder.itemView : null;
                    if (shadowingView != null) {
                        int left = view.getLeft();
                        int top = view.getTop();
                        if (left != shadowingView.getLeft() || top != shadowingView.getTop()) {
                            shadowingView.layout(left, top, left + shadowingView.getWidth(),
                                    top + shadowingView.getHeight());
                        }
                    }
                }
            }
        }
        mRunningLayoutOrScroll = false;
        resumeRequestLayout(false);
    }
    if (!mItemDecorations.isEmpty()) {
        invalidate();
    }
    if (ViewCompat.getOverScrollMode(this) != ViewCompat.OVER_SCROLL_NEVER) {
        considerReleasingGlowsOnScroll(x, y);
        pullGlows(overscrollX, overscrollY);
    }
    if (hresult != 0 || vresult != 0) {
        onScrollChanged(0, 0, 0, 0); // dummy values, View's implementation does not use these.
        if (mScrollListener != null) {
            mScrollListener.onScrolled(this, hresult, vresult);
        }
    }
    if (!awakenScrollBars()) {
        invalidate();
    }
}

From source file:android.support.v7.widget.RecyclerViewEx.java

private void animateDisappearance(ItemHolderInfo disappearingItem) {
    View disappearingItemView = disappearingItem.holder.itemView;
    addAnimatingView(disappearingItem.holder);
    int oldLeft = disappearingItem.left;
    int oldTop = disappearingItem.top;
    int newLeft = disappearingItemView.getLeft();
    int newTop = disappearingItemView.getTop();
    if (oldLeft != newLeft || oldTop != newTop) {
        disappearingItem.holder.setIsRecyclable(false);
        disappearingItemView.layout(newLeft, newTop, newLeft + disappearingItemView.getWidth(),
                newTop + disappearingItemView.getHeight());
        if (DEBUG) {
            Log.d(TAG, "DISAPPEARING: " + disappearingItem.holder + " with view " + disappearingItemView);
        }//from ww w.  j  a v a 2 s . c  om
        if (mItemAnimator.animateMove(disappearingItem.holder, oldLeft, oldTop, newLeft, newTop)) {
            postAnimationRunner();
        }
    } else {
        if (DEBUG) {
            Log.d(TAG, "REMOVED: " + disappearingItem.holder + " with view " + disappearingItemView);
        }
        disappearingItem.holder.setIsRecyclable(false);
        if (mItemAnimator.animateRemove(disappearingItem.holder)) {
            postAnimationRunner();
        }
    }
}

From source file:android.support.transition.ChangeBounds.java

@Override
@Nullable//from   ww w . j a  v a  2s  .com
public Animator createAnimator(@NonNull final ViewGroup sceneRoot, @Nullable TransitionValues startValues,
        @Nullable TransitionValues endValues) {
    if (startValues == null || endValues == null) {
        return null;
    }
    Map<String, Object> startParentVals = startValues.values;
    Map<String, Object> endParentVals = endValues.values;
    ViewGroup startParent = (ViewGroup) startParentVals.get(PROPNAME_PARENT);
    ViewGroup endParent = (ViewGroup) endParentVals.get(PROPNAME_PARENT);
    if (startParent == null || endParent == null) {
        return null;
    }
    final View view = endValues.view;
    if (parentMatches(startParent, endParent)) {
        Rect startBounds = (Rect) startValues.values.get(PROPNAME_BOUNDS);
        Rect endBounds = (Rect) endValues.values.get(PROPNAME_BOUNDS);
        final int startLeft = startBounds.left;
        final int endLeft = endBounds.left;
        final int startTop = startBounds.top;
        final int endTop = endBounds.top;
        final int startRight = startBounds.right;
        final int endRight = endBounds.right;
        final int startBottom = startBounds.bottom;
        final int endBottom = endBounds.bottom;
        final int startWidth = startRight - startLeft;
        final int startHeight = startBottom - startTop;
        final int endWidth = endRight - endLeft;
        final int endHeight = endBottom - endTop;
        Rect startClip = (Rect) startValues.values.get(PROPNAME_CLIP);
        Rect endClip = (Rect) endValues.values.get(PROPNAME_CLIP);
        int numChanges = 0;
        if ((startWidth != 0 && startHeight != 0) || (endWidth != 0 && endHeight != 0)) {
            if (startLeft != endLeft || startTop != endTop)
                ++numChanges;
            if (startRight != endRight || startBottom != endBottom)
                ++numChanges;
        }
        if ((startClip != null && !startClip.equals(endClip)) || (startClip == null && endClip != null)) {
            ++numChanges;
        }
        if (numChanges > 0) {
            Animator anim;
            if (!mResizeClip) {
                ViewUtils.setLeftTopRightBottom(view, startLeft, startTop, startRight, startBottom);
                if (numChanges == 2) {
                    if (startWidth == endWidth && startHeight == endHeight) {
                        Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
                        anim = ObjectAnimatorUtils.ofPointF(view, POSITION_PROPERTY, topLeftPath);
                    } else {
                        final ViewBounds viewBounds = new ViewBounds(view);
                        Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
                        ObjectAnimator topLeftAnimator = ObjectAnimatorUtils.ofPointF(viewBounds,
                                TOP_LEFT_PROPERTY, topLeftPath);

                        Path bottomRightPath = getPathMotion().getPath(startRight, startBottom, endRight,
                                endBottom);
                        ObjectAnimator bottomRightAnimator = ObjectAnimatorUtils.ofPointF(viewBounds,
                                BOTTOM_RIGHT_PROPERTY, bottomRightPath);
                        AnimatorSet set = new AnimatorSet();
                        set.playTogether(topLeftAnimator, bottomRightAnimator);
                        anim = set;
                        set.addListener(new AnimatorListenerAdapter() {
                            // We need a strong reference to viewBounds until the
                            // animator ends (The ObjectAnimator holds only a weak reference).
                            @SuppressWarnings("unused")
                            private ViewBounds mViewBounds = viewBounds;
                        });
                    }
                } else if (startLeft != endLeft || startTop != endTop) {
                    Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
                    anim = ObjectAnimatorUtils.ofPointF(view, TOP_LEFT_ONLY_PROPERTY, topLeftPath);
                } else {
                    Path bottomRight = getPathMotion().getPath(startRight, startBottom, endRight, endBottom);
                    anim = ObjectAnimatorUtils.ofPointF(view, BOTTOM_RIGHT_ONLY_PROPERTY, bottomRight);
                }
            } else {
                int maxWidth = Math.max(startWidth, endWidth);
                int maxHeight = Math.max(startHeight, endHeight);

                ViewUtils.setLeftTopRightBottom(view, startLeft, startTop, startLeft + maxWidth,
                        startTop + maxHeight);

                ObjectAnimator positionAnimator = null;
                if (startLeft != endLeft || startTop != endTop) {
                    Path topLeftPath = getPathMotion().getPath(startLeft, startTop, endLeft, endTop);
                    positionAnimator = ObjectAnimatorUtils.ofPointF(view, POSITION_PROPERTY, topLeftPath);
                }
                final Rect finalClip = endClip;
                if (startClip == null) {
                    startClip = new Rect(0, 0, startWidth, startHeight);
                }
                if (endClip == null) {
                    endClip = new Rect(0, 0, endWidth, endHeight);
                }
                ObjectAnimator clipAnimator = null;
                if (!startClip.equals(endClip)) {
                    ViewCompat.setClipBounds(view, startClip);
                    clipAnimator = ObjectAnimator.ofObject(view, "clipBounds", sRectEvaluator, startClip,
                            endClip);
                    clipAnimator.addListener(new AnimatorListenerAdapter() {
                        private boolean mIsCanceled;

                        @Override
                        public void onAnimationCancel(Animator animation) {
                            mIsCanceled = true;
                        }

                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if (!mIsCanceled) {
                                ViewCompat.setClipBounds(view, finalClip);
                                ViewUtils.setLeftTopRightBottom(view, endLeft, endTop, endRight, endBottom);
                            }
                        }
                    });
                }
                anim = TransitionUtils.mergeAnimators(positionAnimator, clipAnimator);
            }
            if (view.getParent() instanceof ViewGroup) {
                final ViewGroup parent = (ViewGroup) view.getParent();
                ViewGroupUtils.suppressLayout(parent, true);
                TransitionListener transitionListener = new TransitionListenerAdapter() {
                    boolean mCanceled = false;

                    @Override
                    public void onTransitionCancel(@NonNull Transition transition) {
                        ViewGroupUtils.suppressLayout(parent, false);
                        mCanceled = true;
                    }

                    @Override
                    public void onTransitionEnd(@NonNull Transition transition) {
                        if (!mCanceled) {
                            ViewGroupUtils.suppressLayout(parent, false);
                        }
                        transition.removeListener(this);
                    }

                    @Override
                    public void onTransitionPause(@NonNull Transition transition) {
                        ViewGroupUtils.suppressLayout(parent, false);
                    }

                    @Override
                    public void onTransitionResume(@NonNull Transition transition) {
                        ViewGroupUtils.suppressLayout(parent, true);
                    }
                };
                addListener(transitionListener);
            }
            return anim;
        }
    } else {
        int startX = (Integer) startValues.values.get(PROPNAME_WINDOW_X);
        int startY = (Integer) startValues.values.get(PROPNAME_WINDOW_Y);
        int endX = (Integer) endValues.values.get(PROPNAME_WINDOW_X);
        int endY = (Integer) endValues.values.get(PROPNAME_WINDOW_Y);
        // TODO: also handle size changes: check bounds and animate size changes
        if (startX != endX || startY != endY) {
            sceneRoot.getLocationInWindow(mTempLocation);
            Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            view.draw(canvas);
            @SuppressWarnings("deprecation")
            final BitmapDrawable drawable = new BitmapDrawable(bitmap);
            final float transitionAlpha = ViewUtils.getTransitionAlpha(view);
            ViewUtils.setTransitionAlpha(view, 0);
            ViewUtils.getOverlay(sceneRoot).add(drawable);
            Path topLeftPath = getPathMotion().getPath(startX - mTempLocation[0], startY - mTempLocation[1],
                    endX - mTempLocation[0], endY - mTempLocation[1]);
            PropertyValuesHolder origin = PropertyValuesHolderUtils.ofPointF(DRAWABLE_ORIGIN_PROPERTY,
                    topLeftPath);
            ObjectAnimator anim = ObjectAnimator.ofPropertyValuesHolder(drawable, origin);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    ViewUtils.getOverlay(sceneRoot).remove(drawable);
                    ViewUtils.setTransitionAlpha(view, transitionAlpha);
                }
            });
            return anim;
        }
    }
    return null;
}

From source file:android.support.v71.widget.RecyclerView.java

@Override
public void requestChildFocus(View child, View focused) {
    if (!mLayout.onRequestChildFocus(this, mState, child, focused) && focused != null) {
        mTempRect.set(0, 0, focused.getWidth(), focused.getHeight());

        // get item decor offsets w/o refreshing. If they are invalid, there will be another
        // layout pass to fix them, then it is LayoutManager's responsibility to keep focused
        // View in viewport.
        final ViewGroup.LayoutParams focusedLayoutParams = focused.getLayoutParams();
        if (focusedLayoutParams instanceof LayoutParams) {
            // if focused child has item decors, use them. Otherwise, ignore.
            final LayoutParams lp = (LayoutParams) focusedLayoutParams;
            if (!lp.mInsetsDirty) {
                final Rect insets = lp.mDecorInsets;
                mTempRect.left -= insets.left;
                mTempRect.right += insets.right;
                mTempRect.top -= insets.top;
                mTempRect.bottom += insets.bottom;
            }//from   www  . j a va  2 s.  c o  m
        }

        offsetDescendantRectToMyCoords(focused, mTempRect);
        offsetRectIntoDescendantCoords(child, mTempRect);
        requestChildRectangleOnScreen(child, mTempRect, !mFirstLayoutComplete);
    }
    super.requestChildFocus(child, focused);
}

From source file:android.support.v71.widget.RecyclerView.java

private void repositionShadowingViews() {
    // Fix up shadow views used by change animations
    int count = mChildHelper.getChildCount();
    for (int i = 0; i < count; i++) {
        View view = mChildHelper.getChildAt(i);
        ViewHolder holder = getChildViewHolder(view);
        if (holder != null && holder.mShadowingHolder != null) {
            View shadowingView = holder.mShadowingHolder.itemView;
            int left = view.getLeft();
            int top = view.getTop();
            if (left != shadowingView.getLeft() || top != shadowingView.getTop()) {
                shadowingView.layout(left, top, left + shadowingView.getWidth(),
                        top + shadowingView.getHeight());
            }//  w ww  .j a v a2  s  .  c o m
        }
    }
}

From source file:cn.ismartv.tvrecyclerview.widget.RecyclerView.java

/**
 * Logic taken from FocusSearch#isCandidate
 *//*from  w w  w  . j  av  a  2 s.  c om*/
private boolean isPreferredNextFocusAbsolute(View focused, View next, int direction) {
    mTempRect.set(0, 0, focused.getWidth(), focused.getHeight());
    mTempRect2.set(0, 0, next.getWidth(), next.getHeight());
    offsetDescendantRectToMyCoords(focused, mTempRect);
    offsetDescendantRectToMyCoords(next, mTempRect2);
    switch (direction) {
    case View.FOCUS_LEFT:
        return (mTempRect.right > mTempRect2.right || mTempRect.left >= mTempRect2.right)
                && mTempRect.left > mTempRect2.left;
    case View.FOCUS_RIGHT:
        return (mTempRect.left < mTempRect2.left || mTempRect.right <= mTempRect2.left)
                && mTempRect.right < mTempRect2.right;
    case View.FOCUS_UP:
        return (mTempRect.bottom > mTempRect2.bottom || mTempRect.top >= mTempRect2.bottom)
                && mTempRect.top > mTempRect2.top;
    case View.FOCUS_DOWN:
        return (mTempRect.top < mTempRect2.top || mTempRect.bottom <= mTempRect2.top)
                && mTempRect.bottom < mTempRect2.bottom;
    }
    throw new IllegalArgumentException("direction must be absolute. received:" + direction);
}

From source file:com.ferdi2005.secondgram.support.widget.RecyclerView.java

void repositionShadowingViews() {
    // Fix up shadow views used by change animations
    int count = mChildHelper.getChildCount();
    for (int i = 0; i < count; i++) {
        View view = mChildHelper.getChildAt(i);
        ViewHolder holder = getChildViewHolder(view);
        if (holder != null && holder.mShadowingHolder != null) {
            View shadowingView = holder.mShadowingHolder.itemView;
            int left = view.getLeft();
            int top = view.getTop();
            if (left != shadowingView.getLeft() || top != shadowingView.getTop()) {
                shadowingView.layout(left, top, left + shadowingView.getWidth(),
                        top + shadowingView.getHeight());
            }/*from  www  . j a va2  s  .c o  m*/
        }
    }
}