Search in sources :

Example 41 with ViewGroup

use of android.view.ViewGroup 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 42 with ViewGroup

use of android.view.ViewGroup 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 43 with ViewGroup

use of android.view.ViewGroup in project Conductor by bluelinelabs.

the class ParentController method addChild.

private void addChild(final int index) {
    @IdRes final int frameId = getResources().getIdentifier("child_content_" + (index + 1), "id", getActivity().getPackageName());
    final ViewGroup container = (ViewGroup) getView().findViewById(frameId);
    final Router childRouter = getChildRouter(container).setPopsLastView(true);
    if (!childRouter.hasRootController()) {
        ChildController childController = new ChildController("Child Controller #" + index, ColorUtil.getMaterialColor(getResources(), index), false);
        childController.addLifecycleListener(new LifecycleListener() {

            @Override
            public void onChangeEnd(@NonNull Controller controller, @NonNull ControllerChangeHandler changeHandler, @NonNull ControllerChangeType changeType) {
                if (!isBeingDestroyed()) {
                    if (changeType == ControllerChangeType.PUSH_ENTER && !hasShownAll) {
                        if (index < NUMBER_OF_CHILDREN - 1) {
                            addChild(index + 1);
                        } else {
                            hasShownAll = true;
                        }
                    } else if (changeType == ControllerChangeType.POP_EXIT) {
                        if (index > 0) {
                            removeChild(index - 1);
                        } else {
                            getRouter().popController(ParentController.this);
                        }
                    }
                }
            }
        });
        childRouter.setRoot(RouterTransaction.with(childController).pushChangeHandler(new FadeChangeHandler()).popChangeHandler(new FadeChangeHandler()));
    }
}
Also used : ControllerChangeHandler(com.bluelinelabs.conductor.ControllerChangeHandler) FadeChangeHandler(com.bluelinelabs.conductor.changehandler.FadeChangeHandler) IdRes(android.support.annotation.IdRes) ViewGroup(android.view.ViewGroup) ControllerChangeType(com.bluelinelabs.conductor.ControllerChangeType) Router(com.bluelinelabs.conductor.Router) Controller(com.bluelinelabs.conductor.Controller) BaseController(com.bluelinelabs.conductor.demo.controllers.base.BaseController)

Example 44 with ViewGroup

use of android.view.ViewGroup in project Conductor by bluelinelabs.

the class ViewAttachHandler method listenForDeepestChildAttach.

void listenForDeepestChildAttach(final View view, final ChildAttachListener attachListener) {
    if (!(view instanceof ViewGroup)) {
        attachListener.onAttached();
        return;
    }
    ViewGroup viewGroup = (ViewGroup) view;
    if (viewGroup.getChildCount() == 0) {
        attachListener.onAttached();
        return;
    }
    childOnAttachStateChangeListener = new OnAttachStateChangeListener() {

        boolean attached = false;

        @Override
        public void onViewAttachedToWindow(View v) {
            if (!attached) {
                attached = true;
                attachListener.onAttached();
                v.removeOnAttachStateChangeListener(this);
                childOnAttachStateChangeListener = null;
            }
        }

        @Override
        public void onViewDetachedFromWindow(View v) {
        }
    };
    findDeepestChild(viewGroup).addOnAttachStateChangeListener(childOnAttachStateChangeListener);
}
Also used : ViewGroup(android.view.ViewGroup) OnAttachStateChangeListener(android.view.View.OnAttachStateChangeListener) View(android.view.View)

Example 45 with ViewGroup

use of android.view.ViewGroup in project Conductor by bluelinelabs.

the class ViewAttachHandlerTests method testNestedViewGroupAttachDetach.

@Test
public void testNestedViewGroupAttachDetach() {
    ViewGroup view = new LinearLayout(activity);
    View child = new LinearLayout(activity);
    view.addView(child);
    viewAttachHandler.listenForAttach(view);
    assertEquals(0, viewAttachListener.attaches);
    assertEquals(0, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(view, true, false);
    assertEquals(0, viewAttachListener.attaches);
    assertEquals(0, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(child, true, false);
    assertEquals(1, viewAttachListener.attaches);
    assertEquals(0, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(view, true, false);
    ViewUtils.reportAttached(child, true, false);
    assertEquals(1, viewAttachListener.attaches);
    assertEquals(0, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(view, false, false);
    assertEquals(1, viewAttachListener.attaches);
    assertEquals(1, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(view, false, false);
    assertEquals(1, viewAttachListener.attaches);
    assertEquals(1, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(view, true, false);
    assertEquals(1, viewAttachListener.attaches);
    assertEquals(1, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(child, true, false);
    assertEquals(2, viewAttachListener.attaches);
    assertEquals(1, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    viewAttachHandler.onActivityStopped();
    assertEquals(2, viewAttachListener.attaches);
    assertEquals(2, viewAttachListener.detaches);
    assertEquals(0, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(view, false, false);
    assertEquals(2, viewAttachListener.attaches);
    assertEquals(2, viewAttachListener.detaches);
    assertEquals(1, viewAttachListener.detachAfterStops);
    ViewUtils.reportAttached(view, true, false);
    ViewUtils.reportAttached(child, true, false);
    assertEquals(2, viewAttachListener.attaches);
    assertEquals(2, viewAttachListener.detaches);
    assertEquals(1, viewAttachListener.detachAfterStops);
    viewAttachHandler.onActivityStarted();
    assertEquals(3, viewAttachListener.attaches);
    assertEquals(2, viewAttachListener.detaches);
    assertEquals(1, viewAttachListener.detachAfterStops);
}
Also used : ViewGroup(android.view.ViewGroup) View(android.view.View) LinearLayout(android.widget.LinearLayout) Test(org.junit.Test)

Aggregations

ViewGroup (android.view.ViewGroup)2327 View (android.view.View)1300 TextView (android.widget.TextView)452 ImageView (android.widget.ImageView)282 ArrayList (java.util.ArrayList)204 ViewParent (android.view.ViewParent)185 ListView (android.widget.ListView)159 LayoutInflater (android.view.LayoutInflater)127 FrameLayout (android.widget.FrameLayout)127 Paint (android.graphics.Paint)126 AdapterView (android.widget.AdapterView)118 LinearLayout (android.widget.LinearLayout)113 AbsListView (android.widget.AbsListView)106 RecyclerView (android.support.v7.widget.RecyclerView)100 Drawable (android.graphics.drawable.Drawable)95 Animator (android.animation.Animator)94 AnimatedView (carbon.animation.AnimatedView)88 ComponentView (carbon.component.ComponentView)88 RippleView (carbon.drawable.ripple.RippleView)88 ShadowView (carbon.shadow.ShadowView)88