Example usage for android.view ViewGroup getOverlay

List of usage examples for android.view ViewGroup getOverlay

Introduction

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

Prototype

@Override
public ViewGroupOverlay getOverlay() 

Source Link

Document

Returns the ViewGroupOverlay for this view group, creating it if it does not yet exist.

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  a2 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;
        }
    });
}