Example usage for android.view View setTranslationZ

List of usage examples for android.view View setTranslationZ

Introduction

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

Prototype

public void setTranslationZ(float translationZ) 

Source Link

Document

Sets the depth location of this view relative to its #getElevation() elevation .

Usage

From source file:com.silentcircle.common.util.ViewUtil.java

/**
 * Configures the floating action button, clipping it to a circle and setting its translation z.
 * @param view The float action button's view.
 * @param res The resources file.//  w w w  .  j  av  a 2  s  . c  o  m
 */
public static void setupFloatingActionButton(View view, Resources res) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.setOutlineProvider(OVAL_OUTLINE_PROVIDER);
        view.setTranslationZ(res.getDimensionPixelSize(R.dimen.floating_action_button_translation_z));
    }
}

From source file:com.noprom.Material_Shadows.ElevationBasicFragment.java

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

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

    View shape2 = rootView.findViewById(R.id.floating_shape_2);

    /**//from   ww w .  j  av a2s  .  c  om
     * Sets a {@Link View.OnTouchListener} that responds to a touch event on shape2.
     */
    shape2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getActionMasked();
            /* Raise view on ACTION_DOWN and lower it on ACTION_UP. */
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                view.setTranslationZ(120);
                break;
            case MotionEvent.ACTION_UP:
                view.setTranslationZ(0);
                break;
            default:
                return false;
            }
            return true;
        }
    });
    return rootView;
}

From source file:com.example.android.elevationbasic.ElevationBasicFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    /**//from   www.  j  a v a2s.c  o  m
     * Inflates an XML containing two shapes: the first has a fixed elevation
     * and the second ones raises when tapped.
     */
    View rootView = inflater.inflate(R.layout.elevation_basic, container, false);

    View shape2 = rootView.findViewById(R.id.floating_shape_2);

    /**
     * Sets a {@Link View.OnTouchListener} that responds to a touch event on shape2.
     */
    shape2.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            int action = motionEvent.getActionMasked();
            /* Raise view on ACTION_DOWN and lower it on ACTION_UP. */
            switch (action) {
            case MotionEvent.ACTION_DOWN:
                Log.d(TAG, "ACTION_DOWN on view.");
                view.setTranslationZ(120);
                break;
            case MotionEvent.ACTION_UP:
                Log.d(TAG, "ACTION_UP on view.");
                view.setTranslationZ(0);
                break;
            default:
                return false;
            }
            return true;
        }
    });
    return rootView;
}

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();/*  w w w.j a v a 2  s. c  om*/
    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:com.bachhuberdesign.deckbuildergwent.util.FabTransform.java

@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);
    }//from  w  w  w  . jav a2s  .c o m

    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);
}