Example usage for android.view View getTranslationY

List of usage examples for android.view View getTranslationY

Introduction

In this page you can find the example usage for android.view View getTranslationY.

Prototype

@ViewDebug.ExportedProperty(category = "drawing")
public float getTranslationY() 

Source Link

Document

The vertical location of this view relative to its #getTop() top position.

Usage

From source file:com.mad.splitlist.util.DividerItemDecoration.java

@Override
public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
    final int offset = (int) (mPaint.getStrokeWidth() / DIVIDE_OFFSET);

    for (int i = 0; i < parent.getChildCount(); i++) {
        final View view = parent.getChildAt(i);
        final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) view.getLayoutParams();

        final int position = params.getViewAdapterPosition();

        // Draw separator using paint.
        if (position < state.getItemCount()) {
            // apply alpha to support animations
            mPaint.setAlpha((int) (view.getAlpha() * mAlpha));

            float positionY = view.getBottom() + offset + view.getTranslationY();
            // do the drawing
            c.drawLine(view.getLeft() + view.getTranslationX(), positionY,
                    view.getRight() + view.getTranslationX(), positionY, mPaint);
        }/*from   w w w . j a  v a 2  s  . c o m*/
    }
}

From source file:com.tmall.wireless.tangram.ext.SwipeItemTouchListener.java

private View findScrollableChildViewUnder(MotionEvent event) {
    final int x = (int) event.getX();
    final int y = (int) event.getY();
    final int first = getLayoutManager().findFirstVisibleItemPosition();
    final int last = getLayoutManager().findLastVisibleItemPosition();
    for (int i = 0; i <= last - first; i++) {
        View child = getLayoutManager().getChildAt(i);
        if (child instanceof ViewGroup) {
            float translationX = child.getTranslationX();
            float translationY = child.getTranslationY();
            if (x >= (float) child.getLeft() + translationX && x <= (float) child.getRight() + translationX
                    && y >= (float) child.getTop() + translationY
                    && y <= (float) child.getBottom() + translationY) {
                if (findCanScrollView(child) != null) {
                    return child;
                }/*from  w  ww.ja va  2 s  .co  m*/
            }
        }
    }
    return null;
}

From source file:comm.lib.photoview.PhotoViewActivity.java

private void toggleDownLoadToolbar(final View view) {
    // API 11/* www  . j ava2s.  c  o m*/
    if (Build.VERSION.SDK_INT >= 11) {
        if (view.getVisibility() == View.VISIBLE) {
            ObjectAnimator hideAnimator = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(),
                    view.getHeight());
            hideAnimator.setDuration(400);
            hideAnimator.start();
            hideAnimator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    view.setVisibility(View.GONE);
                }
            });
        } else {
            view.setVisibility(View.VISIBLE);
            ObjectAnimator showAnimator = ObjectAnimator.ofFloat(view, "translationY", view.getTranslationY(),
                    0);
            showAnimator.setDuration(400);
            showAnimator.start();
        }
    } else {
        if (view.getVisibility() == View.VISIBLE) {
            view.setVisibility(View.GONE);
        } else {
            view.setVisibility(View.VISIBLE);
        }
    }

}

From source file:com.hippo.drawerlayout.DrawerLayout.java

public static boolean isViewUnder(@Nullable View view, int x, int y) {
    if (view == null) {
        return false;
    } else {/*w ww .  j a v  a2  s . c om*/
        float translationX = 0.0f;
        float translationY = 0.0f;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            translationX = view.getTranslationX();
            translationY = view.getTranslationY();
        }
        return x >= view.getLeft() + translationX && x < view.getRight() + translationX
                && y >= view.getTop() + translationY && y < view.getBottom() + translationY;
    }
}

From source file:me.lizheng.deckview.helpers.DeckChildViewTransform.java

/**
 * Applies this transform to a view.//from   w w  w . ja  v  a  2  s  .  co m
 */
public void applyToTaskView(View v, int duration, Interpolator interp, /*boolean allowLayers,*/
        boolean allowShadows/*, ValueAnimator.AnimatorUpdateListener updateCallback*/) {
    // Check to see if any properties have changed, and update the task view
    if (duration > 0) {
        ViewPropertyAnimator anim = v.animate();
        //            boolean requiresLayers = false;

        // Animate to the final state
        if (hasTranslationYChangedFrom(v.getTranslationY())) {
            anim.translationY(translationY);
        }
        //            if (allowShadows && hasTranslationZChangedFrom(v.getTranslationZ())) {
        //                anim.translationZ(translationZ);
        //            }
        if (hasScaleChangedFrom(v.getScaleX())) {
            anim.scaleX(scale).scaleY(scale);
            //                requiresLayers = true;
        }
        if (hasAlphaChangedFrom(v.getAlpha())) {
            // Use layers if we animate alpha
            anim.alpha(alpha);
            //                requiresLayers = true;
        }
        //            if (requiresLayers && allowLayers) {
        //                anim.withLayer();
        //            }
        //            if (updateCallback != null) {
        //                anim.setUpdateListener(updateCallback);
        //            } else {
        //                anim.setUpdateListener(null);
        //            }
        anim.setStartDelay(startDelay).setDuration(duration).setInterpolator(interp).start();
    } else {
        // Set the changed properties
        if (hasTranslationYChangedFrom(v.getTranslationY())) {
            v.setTranslationY(translationY);
        }
        if (allowShadows && hasTranslationZChangedFrom(ViewCompat.getTranslationZ(v))) {
            ViewCompat.setTranslationZ(v, translationZ);
        }
        if (hasScaleChangedFrom(v.getScaleX())) {
            v.setScaleX(scale);
            v.setScaleY(scale);
        }
        if (hasAlphaChangedFrom(v.getAlpha())) {
            v.setAlpha(alpha);
        }
    }
}

From source file:com.hmatalonga.greenhub.ui.TaskListActivity.java

/**
 * We're gonna setup another ItemDecorator that will draw the red background in the empty space while the items are animating to thier new positions
 * after an item is removed./* w  w w.  j a  v a2  s.c  o  m*/
 */
private void setUpAnimationDecoratorHelper() {
    mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {

        // we want to cache this and not allocate anything repeatedly in the onDraw method
        Drawable background;
        boolean initiated;

        private void init() {
            background = new ColorDrawable(Color.DKGRAY);
            initiated = true;
        }

        @Override
        public void onDraw(Canvas canvas, RecyclerView parent, RecyclerView.State state) {

            if (!initiated) {
                init();
            }

            // only if animation is in progress
            if (parent.getItemAnimator().isRunning()) {

                // some items might be animating down and some items might be animating up to close the gap left by the removed item
                // this is not exclusive, both movement can be happening at the same time
                // to reproduce this leave just enough items so the first one and the last one would be just a little off screen
                // then remove one from the middle

                // find first child with translationY > 0
                // and last one with translationY < 0
                // we're after a rect that is not covered in recycler-view views at this point in time
                View lastViewComingDown = null;
                View firstViewComingUp = null;

                // this is fixed
                int left = 0;
                int right = parent.getWidth();

                // this we need to find out
                int top = 0;
                int bottom = 0;

                // find relevant translating views
                int childCount = parent.getLayoutManager().getChildCount();
                for (int i = 0; i < childCount; i++) {
                    View child = parent.getLayoutManager().getChildAt(i);
                    if (child.getTranslationY() < 0) {
                        // view is coming down
                        lastViewComingDown = child;
                    } else if (child.getTranslationY() > 0) {
                        // view is coming up
                        if (firstViewComingUp == null) {
                            firstViewComingUp = child;
                        }
                    }
                }

                if (lastViewComingDown != null && firstViewComingUp != null) {
                    // views are coming down AND going up to fill the void
                    top = lastViewComingDown.getBottom() + (int) lastViewComingDown.getTranslationY();
                    bottom = firstViewComingUp.getTop() + (int) firstViewComingUp.getTranslationY();
                } else if (lastViewComingDown != null) {
                    // views are going down to fill the void
                    top = lastViewComingDown.getBottom() + (int) lastViewComingDown.getTranslationY();
                    bottom = lastViewComingDown.getBottom();
                } else if (firstViewComingUp != null) {
                    // views are coming up to fill the void
                    top = firstViewComingUp.getTop();
                    bottom = firstViewComingUp.getTop() + (int) firstViewComingUp.getTranslationY();
                }

                background.setBounds(left, top, right, bottom);
                background.draw(canvas);

            }
            super.onDraw(canvas, parent, state);
        }

    });
}

From source file:com.chrismorais.android.sunshine.app.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_forecast);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

    View emptyView = rootView.findViewById(R.id.recyclerview_forecast_empty);

    mRecyclerView.setHasFixedSize(true);

    mForecastAdapter = new ForecastAdapter(getActivity(), new ForecastAdapter.ForecastAdapterOnClickHandler() {
        @Override/*from  w ww . j  a v  a 2s  .  c om*/
        public void onClick(Long date, ForecastAdapter.ForecastAdapterViewHolder vh) {
            String locationSetting = Utility.getPreferredLocation(getActivity());
            ((Callback) getActivity()).onItemSelected(
                    WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationSetting, date));
            mPosition = vh.getAdapterPosition();
        }
    }, emptyView, mChoiceMode);

    mRecyclerView.setAdapter(mForecastAdapter);

    final View parallaxView = rootView.findViewById(R.id.parallax_bar);

    if (null != parallaxView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int max = parallaxView.getHeight();
                    if (dy > 0) {
                        parallaxView.setTranslationY(Math.max(-max, parallaxView.getTranslationY() - dy / 2));
                    } else {
                        parallaxView.setTranslationY(Math.min(0, parallaxView.getTranslationY() - dy / 2));
                    }
                }
            });
        }
    }

    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey(SELECTED_KEY)) {
            mPosition = savedInstanceState.getInt(SELECTED_KEY);
        }
        mForecastAdapter.onRestoreInstanceState(savedInstanceState);
    }

    mForecastAdapter.setUseTodayLayout(mUseTodayLayout);

    return rootView;
}

From source file:io.asv.mtgocr.ocrreader.OcrCaptureActivity.java

/**
 * We're gonna setup another ItemDecorator that will draw the red background in the empty space
 * while the items are animating to thier new positions
 * after an item is removed./*from  ww  w .  j  av  a  2s . co  m*/
 */
private void setUpAnimationDecoratorHelper() {
    mRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {

        // we want to cache this and not allocate anything repeatedly in the onDraw method
        Drawable background;
        boolean initiated;

        private void init() {
            background = new ColorDrawable(Color.RED);
            initiated = true;
        }

        @Override
        public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {

            if (!initiated) {
                init();
            }

            // only if animation is in progress
            if (parent.getItemAnimator().isRunning()) {

                // some items might be animating down and some items might be animating up to close the gap left by the removed item
                // this is not exclusive, both movement can be happening at the same time
                // to reproduce this leave just enough items so the first one and the last one would be just a little off screen
                // then remove one from the middle

                // find first child with translationY > 0
                // and last one with translationY < 0
                // we're after a rect that is not covered in recycler-view views at this point in time
                View lastViewComingDown = null;
                View firstViewComingUp = null;

                // this is fixed
                int left = 0;
                int right = parent.getWidth();

                // this we need to find out
                int top = 0;
                int bottom = 0;

                // find relevant translating views
                int childCount = parent.getLayoutManager().getChildCount();
                for (int i = 0; i < childCount; i++) {
                    View child = parent.getLayoutManager().getChildAt(i);
                    if (child.getTranslationY() < 0) {
                        // view is coming down
                        lastViewComingDown = child;
                    } else if (child.getTranslationY() > 0) {
                        // view is coming up
                        if (firstViewComingUp == null) {
                            firstViewComingUp = child;
                        }
                    }
                }

                if (lastViewComingDown != null && firstViewComingUp != null) {
                    // views are coming down AND going up to fill the void
                    top = lastViewComingDown.getBottom() + (int) lastViewComingDown.getTranslationY();
                    bottom = firstViewComingUp.getTop() + (int) firstViewComingUp.getTranslationY();
                } else if (lastViewComingDown != null) {
                    // views are going down to fill the void
                    top = lastViewComingDown.getBottom() + (int) lastViewComingDown.getTranslationY();
                    bottom = lastViewComingDown.getBottom();
                } else if (firstViewComingUp != null) {
                    // views are coming up to fill the void
                    top = firstViewComingUp.getTop();
                    bottom = firstViewComingUp.getTop() + (int) firstViewComingUp.getTranslationY();
                }

                background.setBounds(left, top, right, bottom);
                background.draw(c);
            }
            super.onDraw(c, parent, state);
        }
    });
}

From source file:com.sagar.sunshine.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_forecast);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    View emptyView = rootView.findViewById(R.id.recyclerview_forecast_empty);

    mRecyclerView.setHasFixedSize(true);

    mForecastAdapter = new ForecastAdapter(getActivity(), new ForecastAdapter.ForecastAdapterOnClickHandler() {
        @Override/*from w w  w  . j  a  va 2  s .c o  m*/
        public void onClick(Long date, ForecastAdapter.ForecastAdapterViewHolder vh) {
            String locationSetting = Utility.getPreferredLocation(getActivity());
            ((Callback) getActivity()).onItemSelected(
                    WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationSetting, date), vh);
        }
    }, emptyView, mChoiceMode);

    mRecyclerView.setAdapter(mForecastAdapter);

    final View parallaxView = rootView.findViewById(R.id.parallax_bar);
    if (null != parallaxView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int max = parallaxView.getHeight();
                    if (dy > 0) {
                        parallaxView.setTranslationY(Math.max(-max, parallaxView.getTranslationY() - dy / 2));
                    } else {
                        parallaxView.setTranslationY(Math.min(0, parallaxView.getTranslationY() - dy / 2));
                    }
                }
            });
        }
    }

    final AppBarLayout appbarView = (AppBarLayout) rootView.findViewById(R.id.appbar);
    if (null != appbarView) {
        ViewCompat.setElevation(appbarView, 0);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @TargetApi(Build.VERSION_CODES.LOLLIPOP)
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    if (0 == mRecyclerView.computeVerticalScrollOffset()) {
                        appbarView.setElevation(0);
                    } else {
                        appbarView.setElevation(appbarView.getTargetElevation());
                    }
                }
            });
        }
    }

    if (savedInstanceState != null) {
        mForecastAdapter.onRestoreInstanceState(savedInstanceState);
    }

    mForecastAdapter.setUseTodayLayout(mUseTodayLayout);

    return rootView;
}

From source file:com.example.barni.sunshine.ForecastFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);

    // Get a reference to the RecyclerView, and attach this adapter to it.
    mRecyclerView = (RecyclerView) rootView.findViewById(R.id.recyclerview_forecast);

    // Set the layout manager
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    View emptyView = rootView.findViewById(R.id.recyclerview_forecast_empty);

    // use this setting to improve performance if you know that changes
    // in content do not change the layout size of the RecyclerView
    mRecyclerView.setHasFixedSize(true);

    // The ForecastAdapter will take data from a source and
    // use it to populate the RecyclerView it's attached to.
    mForecastAdapter = new ForecastAdapter(getActivity(), new ForecastAdapter.ForecastAdapterOnClickHandler() {
        @Override/*from   w w  w.j  av  a  2s  . c  o  m*/
        public void onClick(Long date, ForecastAdapter.ForecastAdapterViewHolder vh) {
            String locationSetting = Utility.getPreferredLocation(getActivity());
            ((Callback) getActivity()).onItemSelected(
                    WeatherContract.WeatherEntry.buildWeatherLocationWithDate(locationSetting, date));
            mPosition = vh.getAdapterPosition();
        }
    }, emptyView, mChoiceMode);

    // specify an adapter (see also next example)
    mRecyclerView.setAdapter(mForecastAdapter);

    final View parallaxView = rootView.findViewById(R.id.parallax_bar);
    if (null != parallaxView) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            mRecyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
                @TargetApi(Build.VERSION_CODES.HONEYCOMB)
                @Override
                public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                    super.onScrolled(recyclerView, dx, dy);
                    int max = parallaxView.getHeight();
                    if (dy > 0) {
                        float y = Math.max(-max, parallaxView.getTranslationY() - dy / 2);
                        parallaxView.setTranslationY(y);
                    } else {
                        float y = Math.min(0, parallaxView.getTranslationY() - dy / 2);
                        parallaxView.setTranslationY(y);
                    }
                }
            });
        }
    }

    // If there's instance state, mine it for useful information.
    // The end-goal here is that the user never knows that turning their device sideways
    // does crazy lifecycle related things.  It should feel like some stuff stretched out,
    // or magically appeared to take advantage of room, but data or place in the app was never
    // actually *lost*.
    if (savedInstanceState != null) {
        if (savedInstanceState.containsKey(SELECTED_KEY)) {
            // The Recycler View probably hasn't even been populated yet.  Actually perform the
            // swapout in onLoadFinished.
            mPosition = savedInstanceState.getInt(SELECTED_KEY);
        }
        mForecastAdapter.onRestoreInstanceState(savedInstanceState);
    }

    mForecastAdapter.setUseTodayLayout(mUseTodayLayout);

    return rootView;
}