Search in sources :

Example 1 with OnPreDrawListener

use of android.view.ViewTreeObserver.OnPreDrawListener in project KJFrameForAndroid by kymjs.

the class KJDragGridView method onSwapItem.

/**
     * 交换item,并且控制item之间的显示与隐藏效果
     * 
     * @param moveX
     * @param moveY
     */
private void onSwapItem(int moveX, int moveY) {
    // 获取我们手指移动到的那个item的position
    final int tempPosition = pointToPosition(moveX, moveY);
    if (tempPosition != mDragPosition && tempPosition != AdapterView.INVALID_POSITION && mAnimationEnd) {
        mDragAdapter.reorderItems(mDragPosition, tempPosition);
        mDragAdapter.setHideItem(tempPosition);
        final ViewTreeObserver observer = getViewTreeObserver();
        observer.addOnPreDrawListener(new OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                observer.removeOnPreDrawListener(this);
                animateReorder(mDragPosition, tempPosition);
                mDragPosition = tempPosition;
                return true;
            }
        });
    }
}
Also used : ViewTreeObserver(android.view.ViewTreeObserver) OnPreDrawListener(android.view.ViewTreeObserver.OnPreDrawListener)

Example 2 with OnPreDrawListener

use of android.view.ViewTreeObserver.OnPreDrawListener in project android-tooltips by rharter.

the class ToolTipLayout method addTooltip.

public void addTooltip(ToolTip tooltip, boolean animate) {
    mToolTips.add(tooltip);
    View v = tooltip.getView();
    if (tooltip.isDismissOnTouch()) {
        v.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
    }
    addView(v);
    requestLayout();
    if (animate) {
        // We use a two pass preDrawListener so we can get the post animation
        // position of the item and then animate it.
        getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                if (!mShouldRemoveObserver) {
                    mShouldRemoveObserver = true;
                    mFinalPositions.clear();
                    for (ToolTip t : mToolTips) {
                        float position = 0;
                        if (Gravity.isVertical(t.getGravity())) {
                            position = t.getView().getY();
                        } else {
                            position = t.getView().getX();
                        }
                        mFinalPositions.put(t, position);
                    }
                    return false;
                }
                mShouldRemoveObserver = false;
                getViewTreeObserver().removeOnPreDrawListener(this);
                List<Animator> animators = new ArrayList<>();
                for (ToolTip t : mToolTips) {
                    t.getView().setAlpha(0);
                    animators.add(ObjectAnimator.ofFloat(t.getView(), View.ALPHA, 0, 1));
                // TODO Restore this once I can fix the flicker
                //                        final float finalPosition = mFinalPositions.get(t);
                //                        if ((Gravity.VERTICAL_GRAVITY_MASK & t.getGravity()) == Gravity.TOP) {
                //                            float start = finalPosition - t.getView().getHeight() / 2;
                //                            t.getView().setY(start);
                //
                //                            animators.add(ObjectAnimator
                //                                    .ofFloat(t.getView(), View.TRANSLATION_Y,
                //                                            start,
                //                                            finalPosition));
                //                        } else if ((Gravity.VERTICAL_GRAVITY_MASK & t.getGravity())
                //                                == Gravity.BOTTOM) {
                //                            float start = finalPosition + t.getView().getHeight() / 2;
                //                            t.getView().setY(start);
                //
                //                            animators.add(ObjectAnimator
                //                                    .ofFloat(t.getView(), View.TRANSLATION_Y,
                //                                            start,
                //                                            finalPosition));
                //                        } else if ((Gravity.HORIZONTAL_GRAVITY_MASK & t.getGravity())
                //                                == Gravity.LEFT) {
                //                            float start = finalPosition - t.getView().getWidth() / 2;
                //                            t.getView().setX(start);
                //
                //                            animators.add(ObjectAnimator
                //                                    .ofFloat(t.getView(), View.TRANSLATION_X,
                //                                            start,
                //                                            finalPosition));
                //                        } else if ((Gravity.HORIZONTAL_GRAVITY_MASK & t.getGravity())
                //                                == Gravity.RIGHT) {
                //                            float start = finalPosition + t.getView().getWidth() / 2;
                //                            t.getView().setX(start);
                //
                //                            animators.add(ObjectAnimator
                //                                    .ofFloat(t.getView(), View.TRANSLATION_X,
                //                                            start,
                //                                            finalPosition));
                //                        }
                }
                AnimatorSet s = new AnimatorSet();
                s.playTogether(animators);
                s.start();
                return true;
            }
        });
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) AnimatorSet(android.animation.AnimatorSet) View(android.view.View) OnPreDrawListener(android.view.ViewTreeObserver.OnPreDrawListener)

Example 3 with OnPreDrawListener

use of android.view.ViewTreeObserver.OnPreDrawListener in project Conductor by bluelinelabs.

the class SharedElementDelayingChangeHandler method prepareForTransition.

@Override
public void prepareForTransition(@NonNull final ViewGroup container, @Nullable View from, @Nullable final View to, @NonNull Transition transition, boolean isPush, @NonNull final OnTransitionPreparedListener onTransitionPreparedListener) {
    if (to != null && to.getParent() == null && waitForTransitionNames.size() > 0) {
        onPreDrawListener = new OnPreDrawListener() {

            boolean addedSubviewListeners;

            @Override
            public boolean onPreDraw() {
                List<View> foundViews = new ArrayList<>();
                for (String transitionName : waitForTransitionNames) {
                    foundViews.add(getViewWithTransitionName(to, transitionName));
                }
                if (!foundViews.contains(null) && !addedSubviewListeners) {
                    addedSubviewListeners = true;
                    for (final View view : foundViews) {
                        view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

                            @Override
                            public boolean onPreDraw() {
                                view.getViewTreeObserver().removeOnPreDrawListener(this);
                                waitForTransitionNames.remove(view.getTransitionName());
                                ViewGroup parent = (ViewGroup) view.getParent();
                                removedViews.add(new ViewParentPair(view, parent));
                                parent.removeView(view);
                                if (waitForTransitionNames.size() == 0) {
                                    to.getViewTreeObserver().removeOnPreDrawListener(onPreDrawListener);
                                    to.setVisibility(View.INVISIBLE);
                                    onTransitionPreparedListener.onPrepared();
                                }
                                return true;
                            }
                        });
                    }
                }
                return false;
            }
        };
        to.getViewTreeObserver().addOnPreDrawListener(onPreDrawListener);
        container.addView(to);
    } else {
        onTransitionPreparedListener.onPrepared();
    }
}
Also used : ViewGroup(android.view.ViewGroup) ArrayList(java.util.ArrayList) List(java.util.List) OnPreDrawListener(android.view.ViewTreeObserver.OnPreDrawListener) View(android.view.View)

Example 4 with OnPreDrawListener

use of android.view.ViewTreeObserver.OnPreDrawListener in project Conductor by bluelinelabs.

the class FabTransform method createAnimator.

@Override
public Animator createAnimator(final ViewGroup sceneRoot, final TransitionValues startValues, final TransitionValues endValues) {
    if (startValues == null || endValues == null)
        return null;
    final Rect startBounds = (Rect) startValues.values.get(PROP_BOUNDS);
    final Rect endBounds = (Rect) endValues.values.get(PROP_BOUNDS);
    final boolean fromFab = endBounds.width() > startBounds.width();
    final View view = endValues.view;
    final Rect dialogBounds = fromFab ? endBounds : startBounds;
    final Interpolator fastOutSlowInInterpolator = AnimUtils.getFastOutSlowInInterpolator();
    final long duration = getDuration();
    final long halfDuration = duration / 2;
    final long twoThirdsDuration = duration * 2 / 3;
    if (!fromFab) {
        // Force measure / layout the dialog back to it's original bounds
        view.measure(makeMeasureSpec(startBounds.width(), View.MeasureSpec.EXACTLY), makeMeasureSpec(startBounds.height(), View.MeasureSpec.EXACTLY));
        view.layout(startBounds.left, startBounds.top, startBounds.right, startBounds.bottom);
    }
    final int translationX = startBounds.centerX() - endBounds.centerX();
    final int translationY = startBounds.centerY() - endBounds.centerY();
    if (fromFab) {
        view.setTranslationX(translationX);
        view.setTranslationY(translationY);
    }
    // Add a color overlay to fake appearance of the FAB
    final ColorDrawable fabColor = new ColorDrawable(color);
    fabColor.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
    if (!fromFab)
        fabColor.setAlpha(0);
    view.getOverlay().add(fabColor);
    // Add an icon overlay again to fake the appearance of the FAB
    final Drawable fabIcon = ContextCompat.getDrawable(sceneRoot.getContext(), icon).mutate();
    final int iconLeft = (dialogBounds.width() - fabIcon.getIntrinsicWidth()) / 2;
    final int iconTop = (dialogBounds.height() - fabIcon.getIntrinsicHeight()) / 2;
    fabIcon.setBounds(iconLeft, iconTop, iconLeft + fabIcon.getIntrinsicWidth(), iconTop + fabIcon.getIntrinsicHeight());
    if (!fromFab)
        fabIcon.setAlpha(0);
    view.getOverlay().add(fabIcon);
    // Since the view that's being transition to always seems to be on the top (z-order), we have
    // to make a copy of the "from" view and put it in the "to" view's overlay, then fade it out.
    // There has to be another way to do this, right?
    Drawable dialogView = null;
    if (!fromFab) {
        startValues.view.setDrawingCacheEnabled(true);
        startValues.view.buildDrawingCache();
        Bitmap viewBitmap = startValues.view.getDrawingCache();
        dialogView = new BitmapDrawable(view.getResources(), viewBitmap);
        dialogView.setBounds(0, 0, dialogBounds.width(), dialogBounds.height());
        view.getOverlay().add(dialogView);
    }
    // Circular clip from/to the FAB size
    final Animator circularReveal;
    if (fromFab) {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2, view.getHeight() / 2, startBounds.width() / 2, (float) Math.hypot(endBounds.width() / 2, endBounds.height() / 2));
        circularReveal.setInterpolator(AnimUtils.getFastOutLinearInInterpolator());
    } else {
        circularReveal = ViewAnimationUtils.createCircularReveal(view, view.getWidth() / 2, view.getHeight() / 2, (float) Math.hypot(startBounds.width() / 2, startBounds.height() / 2), endBounds.width() / 2);
        circularReveal.setInterpolator(AnimUtils.getLinearOutSlowInInterpolator());
        // Persist the end clip i.e. stay at FAB size after the reveal has run
        circularReveal.addListener(new AnimatorListenerAdapter() {

            @Override
            public void onAnimationEnd(Animator animation) {
                final ViewOutlineProvider fabOutlineProvider = view.getOutlineProvider();
                view.setOutlineProvider(new ViewOutlineProvider() {

                    boolean hasRun = false;

                    @Override
                    public void getOutline(final View view, Outline outline) {
                        final int left = (view.getWidth() - endBounds.width()) / 2;
                        final int top = (view.getHeight() - endBounds.height()) / 2;
                        outline.setOval(left, top, left + endBounds.width(), top + endBounds.height());
                        if (!hasRun) {
                            hasRun = true;
                            view.setClipToOutline(true);
                            // We have to remove this as soon as it's laid out so we can get the shadow back
                            view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

                                @Override
                                public boolean onPreDraw() {
                                    if (view.getWidth() == endBounds.width() && view.getHeight() == endBounds.height()) {
                                        view.setOutlineProvider(fabOutlineProvider);
                                        view.setClipToOutline(false);
                                        view.getViewTreeObserver().removeOnPreDrawListener(this);
                                        return true;
                                    }
                                    return true;
                                }
                            });
                        }
                    }
                });
            }
        });
    }
    circularReveal.setDuration(duration);
    // Translate to end position along an arc
    final Animator translate = ObjectAnimator.ofFloat(view, View.TRANSLATION_X, View.TRANSLATION_Y, fromFab ? getPathMotion().getPath(translationX, translationY, 0, 0) : getPathMotion().getPath(0, 0, -translationX, -translationY));
    translate.setDuration(duration);
    translate.setInterpolator(fastOutSlowInInterpolator);
    // Fade contents of non-FAB view in/out
    List<Animator> fadeContents = null;
    if (view instanceof ViewGroup) {
        final ViewGroup vg = ((ViewGroup) view);
        fadeContents = new ArrayList<>(vg.getChildCount());
        for (int i = vg.getChildCount() - 1; i >= 0; i--) {
            final View child = vg.getChildAt(i);
            final Animator fade = ObjectAnimator.ofFloat(child, View.ALPHA, fromFab ? 1f : 0f);
            if (fromFab) {
                child.setAlpha(0f);
            }
            fade.setDuration(twoThirdsDuration);
            fade.setInterpolator(fastOutSlowInInterpolator);
            fadeContents.add(fade);
        }
    }
    // Fade in/out the fab color & icon overlays
    final Animator colorFade = ObjectAnimator.ofInt(fabColor, "alpha", fromFab ? 0 : 255);
    final Animator iconFade = ObjectAnimator.ofInt(fabIcon, "alpha", fromFab ? 0 : 255);
    if (!fromFab) {
        colorFade.setStartDelay(halfDuration);
        iconFade.setStartDelay(halfDuration);
    }
    colorFade.setDuration(halfDuration);
    iconFade.setDuration(halfDuration);
    colorFade.setInterpolator(fastOutSlowInInterpolator);
    iconFade.setInterpolator(fastOutSlowInInterpolator);
    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (dialogView != null) {
        final Animator dialogViewFade = ObjectAnimator.ofInt(dialogView, "alpha", 0).setDuration(twoThirdsDuration);
        dialogViewFade.setInterpolator(fastOutSlowInInterpolator);
        transition.playTogether(dialogViewFade);
    }
    transition.addListener(new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            // Clean up
            view.getOverlay().clear();
            if (!fromFab) {
                view.setTranslationX(0);
                view.setTranslationY(0);
                view.setTranslationZ(0);
                view.measure(makeMeasureSpec(endBounds.width(), View.MeasureSpec.EXACTLY), makeMeasureSpec(endBounds.height(), View.MeasureSpec.EXACTLY));
                view.layout(endBounds.left, endBounds.top, endBounds.right, endBounds.bottom);
            }
        }
    });
    return new AnimUtils.NoPauseAnimator(transition);
}
Also used : Rect(android.graphics.Rect) ViewGroup(android.view.ViewGroup) ColorDrawable(android.graphics.drawable.ColorDrawable) Drawable(android.graphics.drawable.Drawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Outline(android.graphics.Outline) AnimatorSet(android.animation.AnimatorSet) BitmapDrawable(android.graphics.drawable.BitmapDrawable) View(android.view.View) ViewOutlineProvider(android.view.ViewOutlineProvider) Bitmap(android.graphics.Bitmap) Animator(android.animation.Animator) ObjectAnimator(android.animation.ObjectAnimator) ColorDrawable(android.graphics.drawable.ColorDrawable) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) Interpolator(android.view.animation.Interpolator) OnPreDrawListener(android.view.ViewTreeObserver.OnPreDrawListener)

Example 5 with OnPreDrawListener

use of android.view.ViewTreeObserver.OnPreDrawListener in project FreeFlow by Comcast.

the class FreeFlowContainerTest method testSourceCannotModifyFreeFlowContainerReferences.

/**
	 * Tests that changing an attached layout cannot change 
	 * the internal map of the item proxies insode a container
	 * 
	 * @throws InterruptedException
	 */
public void testSourceCannotModifyFreeFlowContainerReferences() throws InterruptedException {
    final CountDownLatch lock = new CountDownLatch(1);
    main.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            final FreeFlowContainer container = new FreeFlowContainer(main);
            final VLayout vLayout = new VLayout();
            vLayout.setLayoutParams(new VLayout.LayoutParams(300, 200, 10));
            container.setLayout(vLayout);
            final DefaultSectionAdapter adapter = new DefaultSectionAdapter(main, 1, 2);
            container.setAdapter(adapter);
            main.setContentView(container);
            container.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {

                @Override
                public boolean onPreDraw() {
                    int frameCount = container.getFrames().size();
                    adapter.setData(5, 10);
                    // setItems will force new frames to be generated, but not set 
                    vLayout.setAdapter(adapter);
                    vLayout.prepareLayout();
                    //assertEquals("Layout frames did not generate as expected", 5*(10+1), vLayout.getAllProxies().size());
                    assertEquals("FreeFlowContainer frames changed unexpectedly with data", frameCount, container.getFrames().size());
                    lock.countDown();
                    return false;
                }
            });
        }
    });
    lock.await(5000, TimeUnit.MILLISECONDS);
}
Also used : VLayout(com.comcast.freeflow.layouts.VLayout) DefaultSectionAdapter(com.comcast.freeflow.helpers.DefaultSectionAdapter) CountDownLatch(java.util.concurrent.CountDownLatch) OnPreDrawListener(android.view.ViewTreeObserver.OnPreDrawListener)

Aggregations

OnPreDrawListener (android.view.ViewTreeObserver.OnPreDrawListener)11 View (android.view.View)5 AnimatorSet (android.animation.AnimatorSet)2 ViewGroup (android.view.ViewGroup)2 LayoutParams (android.view.ViewGroup.LayoutParams)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 Animator (android.animation.Animator)1 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)1 ObjectAnimator (android.animation.ObjectAnimator)1 Resources (android.content.res.Resources)1 Bitmap (android.graphics.Bitmap)1 Outline (android.graphics.Outline)1 Paint (android.graphics.Paint)1 Rect (android.graphics.Rect)1 RectF (android.graphics.RectF)1 BitmapDrawable (android.graphics.drawable.BitmapDrawable)1 ColorDrawable (android.graphics.drawable.ColorDrawable)1 Drawable (android.graphics.drawable.Drawable)1 ViewOutlineProvider (android.view.ViewOutlineProvider)1