Example usage for android.view ViewTreeObserver addOnPreDrawListener

List of usage examples for android.view ViewTreeObserver addOnPreDrawListener

Introduction

In this page you can find the example usage for android.view ViewTreeObserver addOnPreDrawListener.

Prototype

public void addOnPreDrawListener(OnPreDrawListener listener) 

Source Link

Document

Register a callback to be invoked when the view tree is about to be drawn

Usage

From source file:org.telegram.ui.GalleryImageViewer.java

private void fixLayout() {
    if (mViewPager != null) {
        ViewTreeObserver obs = mViewPager.getViewTreeObserver();
        obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override/*from   ww  w. j  a  va2s  .c om*/
            public boolean onPreDraw() {
                mViewPager.beginFakeDrag();
                if (mViewPager.isFakeDragging()) {
                    mViewPager.fakeDragBy(1);
                    mViewPager.endFakeDrag();
                }
                mViewPager.getViewTreeObserver().removeOnPreDrawListener(this);
                return false;
            }
        });
    }
}

From source file:org.mozilla.gecko.animation.PropertyAnimator.java

public void start() {
    if (mDuration == 0) {
        return;//  w  ww  . j  a v a  2s .co  m
    }

    mStartTime = AnimationUtils.currentAnimationTimeMillis();

    // Fix the from value based on current position and property
    for (ElementHolder element : mElementsList) {
        if (element.property == Property.ALPHA)
            element.from = element.proxy.getAlpha();
        else if (element.property == Property.TRANSLATION_Y)
            element.from = element.proxy.getTranslationY();
        else if (element.property == Property.TRANSLATION_X)
            element.from = element.proxy.getTranslationX();
        else if (element.property == Property.SCROLL_Y)
            element.from = element.proxy.getScrollY();
        else if (element.property == Property.SCROLL_X)
            element.from = element.proxy.getScrollX();
        else if (element.property == Property.WIDTH)
            element.from = element.proxy.getWidth();
        else if (element.property == Property.HEIGHT)
            element.from = element.proxy.getHeight();

        ViewCompat.setHasTransientState(element.view, true);

        if (shouldEnableHardwareLayer(element))
            element.view.setLayerType(View.LAYER_TYPE_HARDWARE, null);
        else
            element.view.setDrawingCacheEnabled(true);
    }

    // Get ViewTreeObserver from any of the participant views
    // in the animation.
    final ViewTreeObserver treeObserver;
    if (mElementsList.size() > 0) {
        treeObserver = mElementsList.get(0).view.getViewTreeObserver();
    } else {
        treeObserver = null;
    }

    final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (treeObserver.isAlive()) {
                treeObserver.removeOnPreDrawListener(this);
            }

            mFramePoster.postFirstAnimationFrame();
            return true;
        }
    };

    // Try to start animation after any on-going layout round
    // in the current view tree. OnPreDrawListener seems broken
    // on pre-Honeycomb devices, start animation immediatelly
    // in this case.
    if (Versions.feature11Plus && treeObserver != null && treeObserver.isAlive()) {
        treeObserver.addOnPreDrawListener(preDrawListener);
    } else {
        mFramePoster.postFirstAnimationFrame();
    }

    if (mListeners != null) {
        for (PropertyAnimationListener listener : mListeners) {
            listener.onPropertyAnimationStart();
        }
    }
}

From source file:com.givewaygames.transition.TransitionManager.java

private static void sceneChangeRunTransition(final ViewGroup sceneRoot, final Transition transition) {
    if (transition != null) {
        final ViewTreeObserver observer = sceneRoot.getViewTreeObserver();
        final ViewTreeObserver.OnPreDrawListener listener = new ViewTreeObserver.OnPreDrawListener() {
            public boolean onPreDraw() {
                sceneRoot.getViewTreeObserver().removeOnPreDrawListener(this);
                sPendingTransitions.remove(sceneRoot);
                // Add to running list, handle end to remove it
                final ArrayMap<ViewGroup, ArrayList<Transition>> runningTransitions = getRunningTransitions();
                ArrayList<Transition> currentTransitions = runningTransitions.get(sceneRoot);
                ArrayList<Transition> previousRunningTransitions = null;
                if (currentTransitions == null) {
                    currentTransitions = new ArrayList<Transition>();
                    runningTransitions.put(sceneRoot, currentTransitions);
                } else if (currentTransitions.size() > 0) {
                    previousRunningTransitions = new ArrayList<Transition>(currentTransitions);
                }//from   w  ww.j a  v  a 2 s  .  co  m
                currentTransitions.add(transition);
                transition.addListener(new Transition.TransitionListenerAdapter() {
                    @Override
                    public void onTransitionEnd(Transition transition) {
                        ArrayList<Transition> currentTransitions = runningTransitions.get(sceneRoot);
                        currentTransitions.remove(transition);
                    }
                });
                transition.captureValues(sceneRoot, false);
                if (previousRunningTransitions != null) {
                    for (Transition runningTransition : previousRunningTransitions) {
                        runningTransition.resume();
                    }
                }
                transition.playTransition(sceneRoot);

                return true;
            }
        };
        observer.addOnPreDrawListener(listener);
    }
}

From source file:com.android.incallui.CallCardFragment.java

/**
 * Sets the visibility of the secondary caller info box.  Note, if the {@code visible} parameter
 * is passed in {@code true}, and there is no secondary caller info populated (as determined by
 * {@code mHasSecondaryCallInfo}, the secondary caller info box will not be shown.
 *
 * @param visible {@code true} if the secondary caller info should be shown, {@code false}
 *      otherwise./*from  w w  w  . java 2  s .c  om*/
 */
@Override
public void setSecondaryInfoVisible(final boolean visible) {
    boolean wasVisible = mSecondaryCallInfo.isShown();
    final boolean isVisible = visible && mHasSecondaryCallInfo;
    Log.v(this, "setSecondaryInfoVisible: wasVisible = " + wasVisible + " isVisible = " + isVisible);

    // If visibility didn't change, nothing to do.
    if (wasVisible == isVisible) {
        return;
    }

    // If we are showing the secondary info, we need to show it before animating so that its
    // height will be determined on layout.
    if (isVisible) {
        mSecondaryCallInfo.setVisibility(View.VISIBLE);
    } else {
        mSecondaryCallInfo.setVisibility(View.GONE);
    }

    updateFabPositionForSecondaryCallInfo();
    // We need to translate the secondary caller info, but we need to know its position after
    // the layout has occurred so use a {@code ViewTreeObserver}.
    final ViewTreeObserver observer = getView().getViewTreeObserver();

    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            // We don't want to continue getting called.
            getView().getViewTreeObserver().removeOnPreDrawListener(this);

            // Get the height of the secondary call info now, and then re-hide the view prior
            // to doing the actual animation.
            int secondaryHeight = mSecondaryCallInfo.getHeight();
            if (isVisible) {
                mSecondaryCallInfo.setVisibility(View.GONE);
            } else {
                mSecondaryCallInfo.setVisibility(View.VISIBLE);
            }
            Log.v(this, "setSecondaryInfoVisible: secondaryHeight = " + secondaryHeight);

            // Set the position of the secondary call info card to its starting location.
            mSecondaryCallInfo.setTranslationY(visible ? secondaryHeight : 0);

            // Animate the secondary card info slide up/down as it appears and disappears.
            ViewPropertyAnimator secondaryInfoAnimator = mSecondaryCallInfo.animate()
                    .setInterpolator(AnimUtils.EASE_OUT_EASE_IN).setDuration(mVideoAnimationDuration)
                    .translationY(isVisible ? 0 : secondaryHeight).setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            if (!isVisible) {
                                mSecondaryCallInfo.setVisibility(View.GONE);
                            }
                        }

                        @Override
                        public void onAnimationStart(Animator animation) {
                            if (isVisible) {
                                mSecondaryCallInfo.setVisibility(View.VISIBLE);
                            }
                        }
                    });
            secondaryInfoAnimator.start();

            // Notify listeners of a change in the visibility of the secondary info. This is
            // important when in a video call so that the video call presenter can shift the
            // video preview up or down to accommodate the secondary caller info.
            InCallPresenter.getInstance().notifySecondaryCallerInfoVisibilityChanged(visible, secondaryHeight);

            return true;
        }
    });
}

From source file:com.android.incallui.CallCardFragment.java

/**
 * Sets the visibility of the primary call card.
 * Ensures that when the primary call card is hidden, the video surface slides over to fill the
 * entire screen.//from  ww  w  .j a va 2 s .  c  om
 *
 * @param visible {@code True} if the primary call card should be visible.
 */
@Override
public void setCallCardVisible(final boolean visible) {
    Log.v(this, "setCallCardVisible : isVisible = " + visible);
    // When animating the hide/show of the views in a landscape layout, we need to take into
    // account whether we are in a left-to-right locale or a right-to-left locale and adjust
    // the animations accordingly.
    final boolean isLayoutRtl = InCallPresenter.isRtl();

    // Retrieve here since at fragment creation time the incoming video view is not inflated.
    final View videoView = getView().findViewById(R.id.incomingVideo);
    if (videoView == null) {
        return;
    }

    // Determine how much space there is below or to the side of the call card.
    final float spaceBesideCallCard = getSpaceBesideCallCard();

    // We need to translate the video surface, but we need to know its position after the layout
    // has occurred so use a {@code ViewTreeObserver}.
    final ViewTreeObserver observer = getView().getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            // We don't want to continue getting called.
            getView().getViewTreeObserver().removeOnPreDrawListener(this);

            float videoViewTranslation = 0f;

            // Translate the call card to its pre-animation state.
            if (!mIsLandscape) {
                mPrimaryCallCardContainer.setTranslationY(visible ? -mPrimaryCallCardContainer.getHeight() : 0);

                ViewGroup.LayoutParams p = videoView.getLayoutParams();
                videoViewTranslation = p.height / 2 - spaceBesideCallCard / 2;
            }

            // Perform animation of video view.
            ViewPropertyAnimator videoViewAnimator = videoView.animate()
                    .setInterpolator(AnimUtils.EASE_OUT_EASE_IN).setDuration(mVideoAnimationDuration);
            if (mIsLandscape) {
                videoViewAnimator.translationX(visible ? videoViewTranslation : 0);
            } else {
                videoViewAnimator.translationY(visible ? videoViewTranslation : 0);
            }
            videoViewAnimator.start();

            // Animate the call card sliding.
            ViewPropertyAnimator callCardAnimator = mPrimaryCallCardContainer.animate()
                    .setInterpolator(AnimUtils.EASE_OUT_EASE_IN).setDuration(mVideoAnimationDuration)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            if (!visible) {
                                mPrimaryCallCardContainer.setVisibility(View.GONE);
                            }
                        }

                        @Override
                        public void onAnimationStart(Animator animation) {
                            super.onAnimationStart(animation);
                            if (visible) {
                                mPrimaryCallCardContainer.setVisibility(View.VISIBLE);
                            }
                        }
                    });

            if (mIsLandscape) {
                float translationX = mPrimaryCallCardContainer.getWidth();
                translationX *= isLayoutRtl ? 1 : -1;
                callCardAnimator.translationX(visible ? 0 : translationX).start();
            } else {
                callCardAnimator.translationY(visible ? 0 : -mPrimaryCallCardContainer.getHeight()).start();
            }

            return true;
        }
    });
}

From source file:com.raspi.chatapp.ui.chatting.ChatFragment.java

/**
 * will load the background image/* w  w  w .j  a  v a 2  s .  c  o m*/
 */
private void loadWallPaper() {
    // retrieve the wallpaper file
    final File file = new File(getActivity().getFilesDir(), Constants.WALLPAPER_NAME);
    if (file.exists()) {
        // if it exists retrieve the wallpaperImageView and load the image
        final WallpaperImageView imageView = (WallpaperImageView) getView().findViewById(R.id.chat_wallpaper);
        ViewTreeObserver vto = imageView.getViewTreeObserver();
        // load the image onPreDraw because I need to measuredWidth and height
        // that would be zero if retrieving them onResume or anything else...
        vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
            @Override
            public boolean onPreDraw() {
                // only call this function once after resuming
                imageView.getViewTreeObserver().removeOnPreDrawListener(this);
                int width = imageView.getMeasuredWidth();
                int height = imageView.getMeasuredHeight();
                // start the backgroundImageLoaderTask
                new WallpaperWorkerTask(imageView, width, height).execute(file);
                return true;
            }
        });
    }
}

From source file:com.bq.robotic.robopad_plusplus.fragments.ScheduleRobotMovementsFragment.java

/**
 * Set the listeners to the views that need them. It must be done here in the fragment in order
 * to get the callback here and not in the FragmentActivity, that would be a mess with all the 
 * callbacks of all the possible fragments
 */// w ww. j a  v  a2  s . c o  m
private void setUiListeners() {

    gridView.setOnRearrangeListener(new OnRearrangeListener() {
        public void onRearrange(int oldIndex, int newIndex) {
            if (scheduledControls.isEmpty()) {
                return;
            }

            String scheduledControl = scheduledControls.remove(oldIndex);
            if (oldIndex < newIndex)
                scheduledControls.add(newIndex, scheduledControl);
            else
                scheduledControls.add(newIndex, scheduledControl);
        }

        public void onRearrange(boolean isDraggedDeleted, int draggedDeletedIndex) {
            if (scheduledControls.isEmpty()) {
                return;
            }

            if (isDraggedDeleted) {
                scheduledControls.remove(draggedDeletedIndex);
            }
        }
    });

    GridLayout movementsViews = (GridLayout) getActivity().findViewById(R.id.type_of_movements_container);
    for (int i = 0; i < movementsViews.getChildCount(); i++) {
        movementsViews.getChildAt(i).setOnClickListener(onMovementsButtonClick);
    }

    LinearLayout optionsLayout = (LinearLayout) getActivity().findViewById(R.id.menu_options_container);
    for (int i = 0; i < optionsLayout.getChildCount(); i++) {
        optionsLayout.getChildAt(i).setOnClickListener(onOptionsButtonClick);
    }

    // Resize the GridLayout depending on the number of icons (depending on the current robot
    // selected). In order not to get too much number of columns in little screen, it is limited
    // to two and scroll the  screen if there are more icons. If scroll is not needed the icons
    // are centered in the layout
    final GridLayout typeOfMovementsContainer = (GridLayout) getActivity()
            .findViewById(R.id.type_of_movements_container);
    final ViewTreeObserver vto = typeOfMovementsContainer.getViewTreeObserver();
    final ViewTreeObserver.OnPreDrawListener preDrawListener = new ViewTreeObserver.OnPreDrawListener() {
        public boolean onPreDraw() {
            Rect scrollBounds = new Rect();
            ScrollView scroll = (ScrollView) getActivity().findViewById(R.id.scroll_container);
            scroll.getDrawingRect(scrollBounds);
            //                int finalHeight = scrollBounds.bottom - getActivity().getResources().getDimensionPixelSize(R.dimen.scheduler_grid_margin);
            int finalHeight = scrollBounds.bottom;
            int childCount = typeOfMovementsContainer.getChildCount();

            if (childCount > 1) {
                for (int i = 0; i < childCount; i++) {

                    if (typeOfMovementsContainer.getChildAt(i).getBottom() > finalHeight) {
                        typeOfMovementsContainer.setColumnCount(2);
                        break;
                    }
                }
            }

            scroll.invalidate();

            final ViewTreeObserver vto = typeOfMovementsContainer.getViewTreeObserver();
            vto.removeOnPreDrawListener(this);

            return true;
        }
    };

    vto.addOnPreDrawListener(preDrawListener);

}

From source file:android.support.design.widget.CoordinatorLayout.java

/**
 * Add the pre-draw listener if we're attached to a window and mark that we currently
 * need it when attached.//w  w  w .j  a  va  2 s .  c om
 */
void addPreDrawListener() {
    if (mIsAttachedToWindow) {
        // Add the listener
        if (mOnPreDrawListener == null) {
            mOnPreDrawListener = new OnPreDrawListener();
        }
        final ViewTreeObserver vto = getViewTreeObserver();
        vto.addOnPreDrawListener(mOnPreDrawListener);
    }

    // Record that we need the listener regardless of whether or not we're attached.
    // We'll add the real listener when we become attached.
    mNeedsPreDrawListener = true;
}

From source file:android.support.design.widget.CoordinatorLayout.java

@Override
public void onAttachedToWindow() {
    super.onAttachedToWindow();
    resetTouchBehaviors();/*from   w  ww .j  a  v  a  2 s .c om*/
    if (mNeedsPreDrawListener) {
        if (mOnPreDrawListener == null) {
            mOnPreDrawListener = new OnPreDrawListener();
        }
        final ViewTreeObserver vto = getViewTreeObserver();
        vto.addOnPreDrawListener(mOnPreDrawListener);
    }
    if (mLastInsets == null && ViewCompat.getFitsSystemWindows(this)) {
        // We're set to fitSystemWindows but we haven't had any insets yet...
        // We should request a new dispatch of window insets
        ViewCompat.requestApplyInsets(this);
    }
    mIsAttachedToWindow = true;
}

From source file:org.telepatch.ui.ChatActivity.java

private void fixLayout(final boolean resume) {
    final int lastPos = chatListView.getLastVisiblePosition();
    ViewTreeObserver obs = chatListView.getViewTreeObserver();
    obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override//from   www.  j  a  va 2  s . co m
        public boolean onPreDraw() {
            if (chatListView != null) {
                chatListView.getViewTreeObserver().removeOnPreDrawListener(this);
            }
            if (getParentActivity() == null) {
                return true;
            }
            int height = AndroidUtilities.dp(48);
            if (!AndroidUtilities.isTablet() && getParentActivity().getResources()
                    .getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
                height = AndroidUtilities.dp(40);
                selectedMessagesCountTextView.setTextSize(16);
            } else {
                selectedMessagesCountTextView.setTextSize(18);
            }
            if (avatarImageView != null) {
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) avatarImageView
                        .getLayoutParams();
                params.width = height;
                params.height = height;
                avatarImageView.setLayoutParams(params);
            }
            if (!resume && lastPos >= messages.size() - 1) {
                chatListView.post(new Runnable() {
                    @Override
                    public void run() {
                        chatListView.setSelectionFromTop(messages.size() - 1,
                                -100000 - chatListView.getPaddingTop());
                    }
                });
            }
            return false;
        }
    });
}