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:com.kogitune.activitytransition.core.TransitionAnimation.java

public static MoveData startAnimation(Context context, final View toView, Bundle transitionBundle,
        Bundle savedInstanceState, final int duration, final TimeInterpolator interpolator) {
    final TransitionData transitionData = new TransitionData(context, transitionBundle);
    if (transitionData.imageFilePath != null) {
        setImageToView(toView, transitionData.imageFilePath);
    }//from  w w  w .  j a  v  a 2s . c  o  m
    final MoveData moveData = new MoveData();
    moveData.toView = toView;
    moveData.duration = duration;
    if (savedInstanceState == null) {
        ViewTreeObserver observer = toView.getViewTreeObserver();
        observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

            @Override
            public boolean onPreDraw() {
                toView.getViewTreeObserver().removeOnPreDrawListener(this);

                int[] screenLocation = new int[2];
                toView.getLocationOnScreen(screenLocation);
                moveData.leftDelta = transitionData.thumbnailLeft - screenLocation[0];
                moveData.topDelta = transitionData.thumbnailTop - screenLocation[1];

                moveData.widthScale = (float) transitionData.thumbnailWidth / toView.getWidth();
                moveData.heightScale = (float) transitionData.thumbnailHeight / toView.getHeight();

                runEnterAnimation(moveData, interpolator);

                return true;
            }
        });
    }
    return moveData;
}

From source file:net.naonedbus.card.impl.MapCard.java

@Override
protected void bindView(final Context context, final View base, final View view) {

    mImageView = (ImageView) view;
    showContent();/*w w w  .  j av a 2 s  .  c om*/
    final ViewTreeObserver obs = mImageView.getViewTreeObserver();
    obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (mImageView.getWidth() != 0) {
                mImageView.getViewTreeObserver().removeOnPreDrawListener(this);
                fillView(mImageView);
            }
            return true;
        }
    });

}

From source file:de.schildbach.wallet.util.OnFirstPreDraw.java

private OnFirstPreDraw(final ViewTreeObserver viewTreeObserver, final Callback callback) {
    this.viewTreeObserver = viewTreeObserver;
    this.callback = checkNotNull(callback);
    viewTreeObserver.addOnPreDrawListener(this);
}

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

private void fixLayout() {
    ViewTreeObserver obs = fragmentView.getViewTreeObserver();
    obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override// w ww  .java  2  s .  c  om
        public boolean onPreDraw() {
            if (fragmentView == null) {
                return true;
            }
            fragmentView.getViewTreeObserver().removeOnPreDrawListener(this);
            LinearLayout layout = (LinearLayout) fragmentView;
            WindowManager manager = (WindowManager) ApplicationLoader.applicationContext
                    .getSystemService(Context.WINDOW_SERVICE);
            int rotation = manager.getDefaultDisplay().getRotation();

            if (rotation == Surface.ROTATION_270 || rotation == Surface.ROTATION_90) {
                layout.setOrientation(LinearLayout.HORIZONTAL);
            } else {
                layout.setOrientation(LinearLayout.VERTICAL);
            }

            fragmentView.setPadding(fragmentView.getPaddingLeft(), 0, fragmentView.getPaddingRight(),
                    fragmentView.getPaddingBottom());
            return true;
        }
    });
}

From source file:com.audiokernel.euphonyrmt.fragments.NowPlayingSmallFragment.java

@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
        final Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.now_playing_small_fragment, container, false);
    mSongTitle = (TextView) view.findViewById(R.id.song_title);
    mSongTitle.setSelected(true);/*  w  ww  .j  a v  a2  s.  c o m*/
    mSongArtist = (TextView) view.findViewById(R.id.song_artist);
    mSongArtist.setSelected(true);
    final ImageButton buttonPrev = (ImageButton) view.findViewById(R.id.prev);
    mButtonPlayPause = (ImageButton) view.findViewById(R.id.playpause);
    final ImageButton buttonNext = (ImageButton) view.findViewById(R.id.next);
    buttonPrev.setOnClickListener(mButtonClickListener);
    mButtonPlayPause.setOnClickListener(mButtonClickListener);
    buttonNext.setOnClickListener(mButtonClickListener);

    mCoverArt = (ImageView) view.findViewById(R.id.albumCover);
    final ProgressBar coverArtProgress = (ProgressBar) view.findViewById(R.id.albumCoverProgress);
    final CoverDownloadListener coverArtListener = new AlbumCoverDownloadListener(mCoverArt, coverArtProgress,
            false);
    mCoverHelper = new CoverAsyncHelper();
    mCoverHelper.setCoverMaxSizeFromScreen(getActivity());
    final ViewTreeObserver vto = mCoverArt.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (mCoverHelper != null) {
                mCoverHelper.setCachedCoverMaxSize(mCoverArt.getMeasuredHeight());
            }
            return true;
        }
    });
    mCoverHelper.addCoverDownloadListener(coverArtListener);

    return view;
}

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

public void animate() {
    if (animateCalled) {
        throw new RuntimeException("animate must only be called once");
    }/*w  w  w.j a v  a  2 s. c  o  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:org.mythtv.android.player.app.recordings.RecordingDetailsFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    Log.v(TAG, "onActivityCreated : enter");
    super.onActivityCreated(savedInstanceState);

    ViewTreeObserver vto = coverart.getViewTreeObserver();
    vto.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {

        public boolean onPreDraw() {

            finalWidth = coverart.getMeasuredWidth();
            finalHeight = coverart.getMeasuredHeight();

            return true;
        }/*from w  w w .j a v a2  s . c  om*/

    });

    Log.v(TAG, "onActivityCreated : exit");
}

From source file:net.naonedbus.card.impl.HoraireCard.java

@Override
protected void bindView(final Context context, final View base, final View view) {

    mTerminusView = (ViewGroup) view.findViewById(R.id.terminus);

    mHoraireViews.clear();/*from  w  w w.java2  s.  c o m*/
    mDelaiViews.clear();

    ViewHelper.findViewsByTag(view, context.getString(R.string.cardHoraireTag), new OnTagFoundHandler() {
        @Override
        public void onTagFound(final View v) {
            mHoraireViews.add((TextView) v);
            setTypefaceRobotoLight((TextView) v);
        }
    });

    ViewHelper.findViewsByTag(view, context.getString(R.string.cardDelaiTag), new OnTagFoundHandler() {
        @Override
        public void onTagFound(final View v) {
            mDelaiViews.add((TextView) v);
        }
    });

    final ViewTreeObserver obs = base.getViewTreeObserver();
    obs.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            if (base.getMeasuredWidth() != 0) {
                base.getViewTreeObserver().removeOnPreDrawListener(this);
                fillView((ViewGroup) base, (ViewGroup) view);
            }
            return true;
        }
    });

}

From source file:com.shalzz.attendance.fragment.AttendanceListFragment.java

@Override
public void onItemExpanded(final View view) {
    final int spec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED);
    final ExpandableListAdapter.GenericViewHolder viewHolder = (ExpandableListAdapter.GenericViewHolder) view
            .getTag();/*from w  w  w.  j  av  a  2 s.  com*/
    final RelativeLayout childView = viewHolder.childView;
    childView.measure(spec, spec);
    final int startingHeight = view.getHeight();
    final ViewTreeObserver observer = mRecyclerView.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            // We don'mTracker want to continue getting called for every draw.
            if (observer.isAlive()) {
                observer.removeOnPreDrawListener(this);
            }
            // Calculate some values to help with the animation.
            final int endingHeight = view.getHeight();
            final int distance = Math.abs(endingHeight - startingHeight);
            final int baseHeight = Math.min(endingHeight, startingHeight);
            final boolean isExpanded = endingHeight > startingHeight;

            // Set the views back to the start state of the animation
            view.getLayoutParams().height = startingHeight;
            if (!isExpanded) {
                viewHolder.childView.setVisibility(View.VISIBLE);
            }

            // Set up the fade effect for the action buttons.
            if (isExpanded) {
                // Start the fade in after the expansion has partly completed, otherwise it
                // will be mostly over before the expansion completes.
                viewHolder.childView.setAlpha(0f);
                viewHolder.childView.animate().alpha(1f).setStartDelay(mFadeInStartDelay)
                        .setDuration(mFadeInDuration).start();
            } else {
                viewHolder.childView.setAlpha(1f);
                viewHolder.childView.animate().alpha(0f).setDuration(mFadeOutDuration).start();
            }
            view.requestLayout();

            // Set up the animator to animate the expansion and shadow depth.
            ValueAnimator animator = isExpanded ? ValueAnimator.ofFloat(0f, 1f) : ValueAnimator.ofFloat(1f, 0f);

            // scroll to make the view fully visible.
            mRecyclerView.smoothScrollToPosition(viewHolder.position);

            animator.addUpdateListener(animator1 -> {
                Float value = (Float) animator1.getAnimatedValue();

                // For each value from 0 to 1, animate the various parts of the layout.
                view.getLayoutParams().height = (int) (value * distance + baseHeight);
                float z = mExpandedItemTranslationZ * value;
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    view.setTranslationZ(z);
                }
                view.requestLayout();
            });

            // Set everything to their final values when the animation's done.
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    view.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;

                    if (!isExpanded) {
                        viewHolder.childView.setVisibility(View.GONE);
                    } else {
                        // This seems like it should be unnecessary, but without this, after
                        // navigating out of the activity and then back, the action view alpha
                        // is defaulting to the value (0) at the start of the expand animation.
                        viewHolder.childView.setAlpha(1);
                    }
                }
            });

            animator.setDuration(mExpandCollapseDuration);
            animator.start();

            // Return false so this draw does not occur to prevent the final frame from
            // being drawn for the single frame before the animations start.
            return false;
        }
    });
}

From source file:ch.gianulli.flashcards.ui.Flashcard.java

private void expandButtonBar() {
    mButtonBarShowing = true;//from   w  w  w .j a  v  a  2 s  . co m

    mButtonBar.setVisibility(View.VISIBLE);
    mButtonBar.setAlpha(0.0f);

    final int startingHeight = mCardView.getHeight();

    final ViewTreeObserver observer = mCardView.getViewTreeObserver();
    observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            // We don't want to continue getting called for every listview drawing.
            if (observer.isAlive()) {
                observer.removeOnPreDrawListener(this);
            }

            final int endingHeight = mCardView.getHeight();
            final int distance = endingHeight - startingHeight;

            mCardView.getLayoutParams().height = startingHeight;

            mCardView.requestLayout();

            ValueAnimator heightAnimator = ValueAnimator.ofFloat(0f, 1f).setDuration(300);

            heightAnimator.setInterpolator(new DecelerateInterpolator());
            heightAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animator) {
                    Float value = (Float) animator.getAnimatedValue();
                    mCardView.getLayoutParams().height = (int) (value * distance + startingHeight);
                    mCardView.requestLayout();
                }
            });
            heightAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mCardView.getLayoutParams().height = ViewGroup.LayoutParams.WRAP_CONTENT;
                }
            });

            mButtonBar.setLayerType(View.LAYER_TYPE_HARDWARE, null);
            ObjectAnimator alphaAnimator = ObjectAnimator.ofFloat(mButtonBar, "alpha", 0.0f, 1.0f);
            alphaAnimator.setInterpolator(new DecelerateInterpolator());
            alphaAnimator.setDuration(300);
            alphaAnimator.setStartDelay(100);

            AnimatorSet set = new AnimatorSet();
            set.playTogether(heightAnimator, alphaAnimator);
            set.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    mButtonBar.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
                }
            });

            set.start();

            return false;
        }
    });
}