Example usage for android.view ViewGroup getLocationInWindow

List of usage examples for android.view ViewGroup getLocationInWindow

Introduction

In this page you can find the example usage for android.view ViewGroup getLocationInWindow.

Prototype

public void getLocationInWindow(@Size(2) int[] outLocation) 

Source Link

Document

Computes the coordinates of this view in its window.

Usage

From source file:vc908.stickerfactory.ui.advancedrecyclerview.swipeable.ItemSlidingAnimator.java

private boolean slideToOutsideOfWindowInternal(RecyclerView.ViewHolder holder, boolean toLeft,
        boolean shouldAnimate, long duration) {
    if (!(holder instanceof SwipeableItemViewHolder)) {
        return false;
    }// w  w  w .jav a2 s. com

    final View containerView = ((SwipeableItemViewHolder) holder).getSwipeableContainerView();
    final ViewGroup parent = (ViewGroup) containerView.getParent();

    if (parent == null) {
        return false;
    }

    final int left = containerView.getLeft();
    final int right = containerView.getRight();
    final int width = right - left;
    final boolean parentIsShown = parent.isShown();

    parent.getWindowVisibleDisplayFrame(mTmpRect);

    final int translateX;
    if ((width == 0) || !parentIsShown) {
        // not measured yet or not shown
        translateX = (toLeft) ? (-mTmpRect.width()) : (mTmpRect.width());
        shouldAnimate = false;
    } else {
        parent.getLocationInWindow(mTmpLocation);

        if (toLeft) {
            translateX = -(mTmpLocation[0] + width);
        } else {
            translateX = mTmpRect.width() - (mTmpLocation[0] - left);
        }
    }

    if (shouldAnimate) {
        shouldAnimate = containerView.isShown();
    }

    duration = (shouldAnimate) ? duration : 0;

    return animateSlideInternalCompat(holder, translateX, duration,
            mSlideToOutsideOfWindowAnimationInterpolator);
}

From source file:com.example.carlitos.swipeitemrecycler.view.animation.swipe_item.swipeable.ItemSlidingAnimator.java

private boolean slideToOutsideOfWindowInternal(RecyclerView.ViewHolder holder, int dir, boolean shouldAnimate,
        long duration, SwipeFinishInfo swipeFinish) {

    if (!(holder instanceof SwipeableItemViewHolder)) {
        return false;
    }/* ww w .j  av  a2 s . co m*/

    final View containerView = ((SwipeableItemViewHolder) holder).getSwipeableContainerView();
    final ViewGroup parent = (ViewGroup) containerView.getParent();

    if (parent == null) {
        return false;
    }

    final int left = containerView.getLeft();
    final int right = containerView.getRight();
    final int top = containerView.getTop();
    final int bottom = containerView.getBottom();
    final int width = right - left;
    final int height = bottom - top;
    final boolean parentIsShown = parent.isShown();

    parent.getWindowVisibleDisplayFrame(mTmpRect);
    final int windowWidth = mTmpRect.width();
    final int windowHeight = mTmpRect.height();

    int translateX = 0;
    int translateY = 0;

    if ((width == 0) || (height == 0) || !parentIsShown) {
        // not measured yet or not shown
        switch (dir) {
        case DIR_LEFT:
            translateX = -windowWidth;
            break;
        case DIR_UP:
            translateY = -windowHeight;
            break;
        case DIR_RIGHT:
            translateX = windowWidth;
            break;
        case DIR_DOWN:
            translateY = windowHeight;
            break;
        default:
            break;
        }
        shouldAnimate = false;
    } else {
        parent.getLocationInWindow(mTmpLocation);
        final int x = mTmpLocation[0];
        final int y = mTmpLocation[1];

        switch (dir) {
        case DIR_LEFT:
            translateX = -(x + width);
            break;
        case DIR_UP:
            translateY = -(y + height);
            break;
        case DIR_RIGHT:
            translateX = windowWidth - (x - left);
            break;
        case DIR_DOWN:
            translateY = windowHeight - (y - top);
            break;
        default:
            break;
        }
    }

    if (shouldAnimate) {
        shouldAnimate = containerView.isShown();
    }

    duration = (shouldAnimate) ? duration : 0;

    boolean horizontal = (dir == DIR_LEFT || dir == DIR_RIGHT);
    return animateSlideInternalCompat(holder, horizontal, translateX, translateY, duration,
            mSlideToOutsideOfWindowAnimationInterpolator, swipeFinish);
}

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

@Override
@Nullable/*w  w w . j a va 2  s .c  om*/
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;
}