Example usage for android.view ViewGroup getLocationOnScreen

List of usage examples for android.view ViewGroup getLocationOnScreen

Introduction

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

Prototype

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

Source Link

Document

Computes the coordinates of this view on the screen.

Usage

From source file:com.cocosw.accessory.views.adapter.AdapterViewAnimator.java

public void animate() {
    if (animateCalled) {
        throw new RuntimeException("animate must only be called once");
    }/*from  ww w  .  ja  v  a  2 s.  co  m*/
    animateCalled = true;

    final ViewTreeObserver observer = adapterView.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            observer.removeOnPreDrawListener(this);

            Adapter adapter = adapterView.getAdapter();
            final int firstVisiblePosition = adapterView.getFirstVisiblePosition();
            for (int i = 0, childCount = adapterView.getChildCount(); i < childCount; i++) {
                final int position = firstVisiblePosition + i;
                final long id = adapter.getItemId(position);
                idToViewMap.remove(id);
                final View child = adapterView.getChildAt(i);

                final Rect bounds = viewBounds.get(id);
                Runnable endAction = new Runnable() {
                    @Override
                    public void run() {
                        ViewCompat.setHasTransientState(child, false);
                    }
                };
                if (bounds != null) {
                    if (callback == null
                            || !callback.onMoveView(adapterView, child, position, id, bounds, endAction)) {
                        final int dx = bounds.left - child.getLeft();
                        final int dy = bounds.top - child.getTop();
                        ViewCompat.setTranslationX(child, dx);
                        ViewCompat.setTranslationY(child, dy);
                        ViewCompat.animate(child).setDuration(DURATION_MOVE).translationX(0.0f)
                                .translationY(0.0f).withEndAction(endAction);
                    }
                } else {
                    if (callback == null || !callback.onAddView(adapterView, child, position, id)) {
                        ViewCompat.setAlpha(child, 0.0f);
                        ViewCompat.animate(child).setDuration(DURATION_ADD).alpha(1.0f);
                    }
                }
            }

            int[] adapterViewLocation = new int[2];
            int[] hostViewLocation = new int[2];
            final int size = idToViewMap.size();
            for (int i = 0; i < size; i++) {
                final long id = idToViewMap.keyAt(i);
                final View child = idToViewMap.get(id);
                ViewCompat.setHasTransientState(child, false);
                final View viewCopy = new ViewCopy(child);
                Rect bounds = viewBounds.get(id);

                if (overlay == null) {
                    ViewGroup parent = (ViewGroup) adapterView.getParent();
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2)
                        overlay = parent.getOverlay();
                    adapterView.getLocationOnScreen(adapterViewLocation);
                    parent.getLocationOnScreen(hostViewLocation);
                }

                overlay.add(viewCopy);
                viewCopy.offsetLeftAndRight(adapterViewLocation[0] - hostViewLocation[0]);
                viewCopy.offsetTopAndBottom(adapterViewLocation[1] - hostViewLocation[1]);

                if (callback == null || !callback.onRemoveView(adapterView, viewCopy, id, bounds)) {
                    ViewCompat.animate(viewCopy).setDuration(DURATION_REMOVE).alpha(0.0f)
                            .withEndAction(new Runnable() {
                                @Override
                                public void run() {
                                    overlay.remove(viewCopy);
                                }
                            });
                }
            }

            return true;
        }
    });
}

From source file:de.mrapp.android.bottomsheet.view.DraggableView.java

/**
 * Returns, whether a touch event at a specific position targets a view, which can be scrolled
 * up./*from  w  ww.j  av a  2 s .  c om*/
 *
 * @param x
 *         The horizontal position of the touch event in pixels as a {@link Float} value
 * @param y
 *         The vertical position of the touch event in pixels as a {@link Float} value
 * @param viewGroup
 *         The view group, which should be used to search for scrollable child views, as an
 *         instance of the class {@link ViewGroup}. The view group may not be null
 * @return True, if the touch event targets a view, which can be scrolled up, false otherwise
 */
private boolean isScrollUpEvent(final float x, final float y, @NonNull final ViewGroup viewGroup) {
    int location[] = new int[2];
    viewGroup.getLocationOnScreen(location);

    if (x >= location[0] && x <= location[0] + viewGroup.getWidth() && y >= location[1]
            && y <= location[1] + viewGroup.getHeight()) {
        for (int i = 0; i < viewGroup.getChildCount(); i++) {
            View view = viewGroup.getChildAt(i);

            if (ViewCompat.canScrollVertically(view, -1)) {
                return true;
            } else if (view instanceof ViewGroup) {
                return isScrollUpEvent(x, y, (ViewGroup) view);
            }
        }
    }

    return false;
}

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

@Override
public long getStartDelay(ViewGroup sceneRoot, Transition transition, TransitionValues startValues,
        TransitionValues endValues) {/*from   w  w w  .  j  a va2 s. c  o  m*/
    if (startValues == null && endValues == null) {
        return 0;
    }
    int directionMultiplier = 1;
    Rect epicenter = transition.getEpicenter();
    TransitionValues positionValues;
    if (endValues == null || getViewVisibility(startValues) == View.VISIBLE) {
        positionValues = startValues;
        directionMultiplier = -1;
    } else {
        positionValues = endValues;
    }

    int viewCenterX = getViewX(positionValues);
    int viewCenterY = getViewY(positionValues);

    int[] loc = new int[2];
    sceneRoot.getLocationOnScreen(loc);
    int left = loc[0] + Math.round(sceneRoot.getTranslationX());
    int top = loc[1] + Math.round(sceneRoot.getTranslationY());
    int right = left + sceneRoot.getWidth();
    int bottom = top + sceneRoot.getHeight();

    int epicenterX;
    int epicenterY;
    if (epicenter != null) {
        epicenterX = epicenter.centerX();
        epicenterY = epicenter.centerY();
    } else {
        epicenterX = (left + right) / 2;
        epicenterY = (top + bottom) / 2;
    }

    float distance = distance(sceneRoot, viewCenterX, viewCenterY, epicenterX, epicenterY, left, top, right,
            bottom);
    float maxDistance = getMaxDistance(sceneRoot);
    float distanceFraction = distance / maxDistance;

    long duration = transition.getDuration();
    if (duration < 0) {
        duration = 300;
    }

    return Math.round(duration * directionMultiplier / mPropagationSpeed * distanceFraction);
}

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

@Override
public Animator onDisappear(ViewGroup sceneRoot, TransitionValues startValues, int startVisibility,
        TransitionValues endValues, int endVisibility) {
    if ((mFadingMode & OUT) != OUT) {
        return null;
    }/* w  w w. ja v a  2  s .  c  o  m*/
    View view = null;
    View startView = (startValues != null) ? startValues.view : null;
    View endView = (endValues != null) ? endValues.view : null;
    if (DBG) {
        Log.d(LOG_TAG, "Fade.onDisappear: startView, startVis, endView, endVis = " + startView + ", "
                + startVisibility + ", " + endView + ", " + endVisibility);
    }
    View overlayView = null;
    View viewToKeep = null;
    if (endView == null || endView.getParent() == null) {
        if (endView != null) {
            // endView was removed from its parent - add it to the overlay
            view = overlayView = endView;
        } else if (startView != null) {
            // endView does not exist. Use startView only under certain
            // conditions, because placing a view in an overlay necessitates
            // it being removed from its current parent
            if (startView.getParent() == null) {
                // no parent - safe to use
                view = overlayView = startView;
            } else if (startView.getParent() instanceof View && startView.getParent().getParent() == null) {
                View startParent = (View) startView.getParent();
                int id = startParent.getId();
                if (id != View.NO_ID && sceneRoot.findViewById(id) != null && mCanRemoveViews) {
                    // no parent, but its parent is unparented  but the parent
                    // hierarchy has been replaced by a new hierarchy with the same id
                    // and it is safe to un-parent startView
                    view = overlayView = startView;
                }
            }
        }
    } else {
        // visibility change
        if (endVisibility == View.INVISIBLE) {
            view = endView;
            viewToKeep = view;
        } else {
            // Becoming GONE
            if (startView == endView) {
                view = endView;
                viewToKeep = view;
            } else {
                view = startView;
                overlayView = view;
            }
        }
    }
    final int finalVisibility = endVisibility;
    // TODO: add automatic facility to Visibility superclass for keeping views around
    if (overlayView != null) {
        // TODO: Need to do this for general case of adding to overlay
        int screenX = (Integer) startValues.values.get(PROPNAME_SCREEN_X);
        int screenY = (Integer) startValues.values.get(PROPNAME_SCREEN_Y);
        int[] loc = new int[2];
        sceneRoot.getLocationOnScreen(loc);
        ViewCompat.offsetLeftAndRight(overlayView, (screenX - loc[0]) - overlayView.getLeft());
        ViewCompat.offsetTopAndBottom(overlayView, (screenY - loc[1]) - overlayView.getTop());
        ViewGroupOverlay.createFrom(sceneRoot).add(overlayView);
        //            sceneRoot.getOverlay().add(overlayView);
        // TODO: add automatic facility to Visibility superclass for keeping views around
        final float startAlpha = 1;
        float endAlpha = 0;
        final View finalView = view;
        final View finalOverlayView = overlayView;
        final View finalViewToKeep = viewToKeep;
        final ViewGroup finalSceneRoot = sceneRoot;
        final AnimatorListenerAdapter endListener = new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                finalView.setAlpha(startAlpha);
                // TODO: restore view offset from overlay repositioning
                if (finalViewToKeep != null) {
                    finalViewToKeep.setVisibility(finalVisibility);
                }
                if (finalOverlayView != null) {
                    ViewGroupOverlay.createFrom(finalSceneRoot).remove(finalOverlayView);
                    //                        finalSceneRoot.getOverlay().remove(finalOverlayView);
                }
            }
            //
            //                @Override
            //                public void onAnimationPause(Animator animation) {
            //                    if (finalOverlayView != null) {
            //                        finalSceneRoot.getOverlay().remove(finalOverlayView);
            //                    }
            //                }
            //
            //                @Override
            //                public void onAnimationResume(Animator animation) {
            //                    if (finalOverlayView != null) {
            //                        finalSceneRoot.getOverlay().add(finalOverlayView);
            //                    }
            //                }
        };
        return createAnimation(view, startAlpha, endAlpha, endListener);
    }
    if (viewToKeep != null) {
        // TODO: find a different way to do this, like just changing the view to be
        // VISIBLE for the duration of the transition
        viewToKeep.setVisibility((View.VISIBLE));
        // TODO: add automatic facility to Visibility superclass for keeping views around
        final float startAlpha = 1;
        float endAlpha = 0;
        final View finalView = view;
        final View finalOverlayView = overlayView;
        final View finalViewToKeep = viewToKeep;
        final ViewGroup finalSceneRoot = sceneRoot;
        final AnimatorListenerAdapter endListener = new AnimatorListenerAdapter() {
            boolean mCanceled = false;

            float mPausedAlpha = -1;

            //                @Override
            //                public void onAnimationPause(Animator animation) {
            //                    if (finalViewToKeep != null && !mCanceled) {
            //                        finalViewToKeep.setVisibility(finalVisibility);
            //                    }
            //                    mPausedAlpha = finalView.getAlpha();
            //                    finalView.setAlpha(startAlpha);
            //                }
            //
            //                @Override
            //                public void onAnimationResume(Animator animation) {
            //                    if (finalViewToKeep != null && !mCanceled) {
            //                        finalViewToKeep.setVisibility(View.VISIBLE);
            //                    }
            //                    finalView.setAlpha(mPausedAlpha);
            //                }

            @Override
            public void onAnimationCancel(Animator animation) {
                mCanceled = true;
                if (mPausedAlpha >= 0) {
                    finalView.setAlpha(mPausedAlpha);
                }
            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (!mCanceled) {
                    finalView.setAlpha(startAlpha);
                }
                // TODO: restore view offset from overlay repositioning
                if (finalViewToKeep != null && !mCanceled) {
                    finalViewToKeep.setVisibility(finalVisibility);
                }
                if (finalOverlayView != null) {
                    ViewGroupOverlay.createFrom(finalSceneRoot).add(finalOverlayView);
                    //                        finalSceneRoot.getOverlay().remove(finalOverlayView);
                }
            }
        };
        return createAnimation(view, startAlpha, endAlpha, endListener);
    }
    return null;
}

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

/**
 * The default implementation of this method does nothing. Subclasses
 * should override if they need to create an Animator when targets disappear.
 * The method should only be called by the Visibility class; it is
 * not intended to be called from external classes.
 *
 * @param sceneRoot       The root of the transition hierarchy
 * @param startValues     The target values in the start scene
 * @param startVisibility The target visibility in the start scene
 * @param endValues       The target values in the end scene
 * @param endVisibility   The target visibility in the end scene
 * @return An Animator to be started at the appropriate time in the
 * overall transition for this scene change. A null value means no animation
 * should be run.//from   w  ww.  ja v a  2 s .c  o  m
 */
@SuppressWarnings("UnusedParameters")
public Animator onDisappear(ViewGroup sceneRoot, TransitionValues startValues, int startVisibility,
        TransitionValues endValues, int endVisibility) {
    if ((mMode & MODE_OUT) != MODE_OUT) {
        return null;
    }

    View startView = (startValues != null) ? startValues.view : null;
    View endView = (endValues != null) ? endValues.view : null;
    View overlayView = null;
    View viewToKeep = null;
    if (endView == null || endView.getParent() == null) {
        if (endView != null) {
            // endView was removed from its parent - add it to the overlay
            overlayView = endView;
        } else if (startView != null) {
            // endView does not exist. Use startView only under certain
            // conditions, because placing a view in an overlay necessitates
            // it being removed from its current parent
            if (startView.getParent() == null) {
                // no parent - safe to use
                overlayView = startView;
            } else if (startView.getParent() instanceof View) {
                View startParent = (View) startView.getParent();
                TransitionValues startParentValues = getTransitionValues(startParent, true);
                TransitionValues endParentValues = getMatchedTransitionValues(startParent, true);
                VisibilityInfo parentVisibilityInfo = getVisibilityChangeInfo(startParentValues,
                        endParentValues);
                if (!parentVisibilityInfo.mVisibilityChange) {
                    overlayView = TransitionUtils.copyViewImage(sceneRoot, startView, startParent);
                } else if (startParent.getParent() == null) {
                    int id = startParent.getId();
                    if (id != View.NO_ID && sceneRoot.findViewById(id) != null && mCanRemoveViews) {
                        // no parent, but its parent is unparented  but the parent
                        // hierarchy has been replaced by a new hierarchy with the same id
                        // and it is safe to un-parent startView
                        overlayView = startView;
                    }
                }
            }
        }
    } else {
        // visibility change
        if (endVisibility == View.INVISIBLE) {
            viewToKeep = endView;
        } else {
            // Becoming GONE
            if (startView == endView) {
                viewToKeep = endView;
            } else {
                overlayView = startView;
            }
        }
    }
    final int finalVisibility = endVisibility;

    if (overlayView != null && startValues != null) {
        // TODO: Need to do this for general case of adding to overlay
        int[] screenLoc = (int[]) startValues.values.get(PROPNAME_SCREEN_LOCATION);
        int screenX = screenLoc[0];
        int screenY = screenLoc[1];
        int[] loc = new int[2];
        sceneRoot.getLocationOnScreen(loc);
        overlayView.offsetLeftAndRight((screenX - loc[0]) - overlayView.getLeft());
        overlayView.offsetTopAndBottom((screenY - loc[1]) - overlayView.getTop());
        final ViewGroupOverlayImpl overlay = ViewGroupUtils.getOverlay(sceneRoot);
        overlay.add(overlayView);
        Animator animator = onDisappear(sceneRoot, overlayView, startValues, endValues);
        if (animator == null) {
            overlay.remove(overlayView);
        } else {
            final View finalOverlayView = overlayView;
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    overlay.remove(finalOverlayView);
                }
            });
        }
        return animator;
    }

    if (viewToKeep != null) {
        int originalVisibility = viewToKeep.getVisibility();
        ViewUtils.setTransitionVisibility(viewToKeep, View.VISIBLE);
        Animator animator = onDisappear(sceneRoot, viewToKeep, startValues, endValues);
        if (animator != null) {
            DisappearListener disappearListener = new DisappearListener(viewToKeep, finalVisibility, true);
            animator.addListener(disappearListener);
            AnimatorUtils.addPauseListener(animator, disappearListener);
            addListener(disappearListener);
        } else {
            ViewUtils.setTransitionVisibility(viewToKeep, originalVisibility);
        }
        return animator;
    }
    return null;
}