Example usage for android.view View setClipToOutline

List of usage examples for android.view View setClipToOutline

Introduction

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

Prototype

public void setClipToOutline(boolean clipToOutline) 

Source Link

Document

Sets whether the View's Outline should be used to clip the contents of the View.

Usage

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Applies a circular mask to a view./*from  w w w.ja  v a 2  s .c om*/
 * <p/>
 * Note that this method only works on Lollipop and above; it will silently fail on older versions.
 *
 * @param view The view to apply the mask to.
 */
public static void applyCircularMask(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.setOutlineProvider(new ViewOutlineProvider() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setOval(0, 0, view.getWidth(), view.getHeight());
            }
        });
        view.setClipToOutline(true);
    }
}

From source file:net.kourlas.voipms_sms.Utils.java

/**
 * Applies a rectangular rounded corners mask to a view.
 * <p/>//w  ww  . j a  v  a  2s . com
 * Note that this method only works on Lollipop and above; it will silently fail on older versions.
 *
 * @param view The view to apply the mask to.
 */
public static void applyRoundedCornersMask(View view) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        view.setOutlineProvider(new ViewOutlineProvider() {
            @TargetApi(Build.VERSION_CODES.LOLLIPOP)
            @Override
            public void getOutline(View view, Outline outline) {
                outline.setRoundRect(0, 0, view.getWidth(), view.getHeight(), 15);
            }
        });
        view.setClipToOutline(true);
    }
}

From source file:mad.com.applicationproject.activity.DragFragment.java

private void addDragView(View view) {
    mDragViews.add(view);/*  www .  j  ava 2s . c  o m*/
    view.setOutlineProvider(mOutlineProviderCircle);
    view.setClipToOutline(true);
    mRootView.addDragView(view);
}

From source file:com.malmstein.materialanimations.elevation.ElevationDragFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_ztranslation, container, false);

    /* Find the {@link View} to apply z-translation to. */
    final View floatingShape = rootView.findViewById(R.id.circle);

    /* Define the shape of the {@link View}'s shadow by setting one of the {@link Outline}s. */
    floatingShape.setOutlineProvider(mOutlineProviderCircle);

    /* Clip the {@link View} with its outline. */
    floatingShape.setClipToOutline(true);

    DragFrameLayout dragLayout = ((DragFrameLayout) rootView.findViewById(R.id.main_layout));

    dragLayout.setDragFrameController(new DragFrameLayout.DragFrameLayoutController() {

        @Override/*  w  w w .ja  va 2 s .  com*/
        public void onDragDrop(boolean captured) {
            /* Animate the translation of the {@link View}. Note that the translation
             is being modified, not the elevation. */
            floatingShape.animate().translationZ(captured ? 50 : 0).scaleX(1.2f).scaleY(1.3f).setDuration(100);
            Log.d(TAG, captured ? "Drag" : "Drop");
        }
    });

    dragLayout.addDragView(floatingShape);

    /* Raise the circle in z when the "z+" button is clicked. */
    rootView.findViewById(R.id.raise_bt).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mElevation += mElevationStep;
            Log.d(TAG, String.format("Elevation: %.1f", mElevation));
            floatingShape.setElevation(mElevation);
        }
    });

    /* Lower the circle in z when the "z-" button is clicked. */
    rootView.findViewById(R.id.lower_bt).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mElevation -= mElevationStep;
            // Don't allow for negative values of Z.
            if (mElevation < 0) {
                mElevation = 0;
            }
            Log.d(TAG, String.format("Elevation: %.1f", mElevation));
        }
    });

    return rootView;
}

From source file:com.example.android.elevationdrag.ElevationDragFragment.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.ztranslation, container, false);

    /* Find the {@link View} to apply z-translation to. */
    final View floatingShape = rootView.findViewById(R.id.circle);

    /* Define the shape of the {@link View}'s shadow by setting one of the {@link Outline}s. */
    floatingShape.setOutlineProvider(mOutlineProviderCircle);

    /* Clip the {@link View} with its outline. */
    floatingShape.setClipToOutline(true);

    DragFrameLayout dragLayout = ((DragFrameLayout) rootView.findViewById(R.id.main_layout));

    dragLayout.setDragFrameController(new DragFrameLayout.DragFrameLayoutController() {

        @Override//from  www .  j  a  v  a  2s .  c  o  m
        public void onDragDrop(boolean captured) {
            /* Animate the translation of the {@link View}. Note that the translation
             is being modified, not the elevation. */
            floatingShape.animate().translationZ(captured ? 50 : 0).setDuration(100);
            Log.d(TAG, captured ? "Drag" : "Drop");
        }
    });

    dragLayout.addDragView(floatingShape);

    /* Raise the circle in z when the "z+" button is clicked. */
    rootView.findViewById(R.id.raise_bt).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mElevation += mElevationStep;
            Log.d(TAG, String.format(Locale.US, "Elevation: %.1f", mElevation));
            floatingShape.setElevation(mElevation);
        }
    });

    /* Lower the circle in z when the "z-" button is clicked. */
    rootView.findViewById(R.id.lower_bt).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            mElevation -= mElevationStep;
            // Don't allow for negative values of Z.
            if (mElevation < 0) {
                mElevation = 0;
            }
            Log.d(TAG, String.format(Locale.US, "Elevation: %.1f", mElevation));
            floatingShape.setElevation(mElevation);
        }
    });

    return rootView;
}

From source file:com.myhexaville.iconanimations.gooey_fab.GooeyFab.java

private void init() {
    final int fabRestElevation = getContext().getResources().getDimensionPixelSize(R.dimen.fab_elevation_rest);
    setOnClickListener(new OnClickListener() {
        @Override//from  w  ww  .ja  va  2  s .  com
        public void onClick(View v) {

        }
    });
    onElevationsChanged(fabRestElevation, 20f);

    inflate(getContext(), R.layout.fab_layout, this);
    View v = findViewById(R.id.ripple);
    v.setClipToOutline(true);
    v.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(final View v) {
            if (!mIsAnimating) {
                mIsAnimating = true;

                onLargeFabClicked();
                v.animate().scaleY(1.1f).setDuration(187).start();

                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        v.animate().scaleY(1f).setDuration(187).start();
                    }
                }, 187);
            }
        }
    });

    setBackground(AnimatedVectorDrawableCompat.create(getContext(), R.drawable.avd_gooey));

    //        setOnTouchListener(new View.OnTouchListener() {
    //            @Override
    //            public boolean onTouch(View v, MotionEvent event) {
    //                if (event.getAction() == MotionEvent.ACTION_UP) {
    //                    if (!isClickedOnLargeFab(event)) {
    //                        onSmallFabClicked();
    //                    }
    //                }
    //                return true;
    //            }
    //        });
}

From source file:com.example.android.clippingbasic.ClippingBasicFragment.java

@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    /* Set the initial text for the TextView. */
    mTextView = (TextView) view.findViewById(R.id.text_view);
    changeText();/*from   w  w w  .j a  v  a  2 s  . c  o m*/

    final View clippedView = view.findViewById(R.id.frame);

    /* Sets the OutlineProvider for the View. */
    clippedView.setOutlineProvider(mOutlineProvider);

    /* When the button is clicked, the text is clipped or un-clipped. */
    view.findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View bt) {
            // Toggle whether the View is clipped to the outline
            if (clippedView.getClipToOutline()) {
                /* The Outline is set for the View, but disable clipping. */
                clippedView.setClipToOutline(false);

                Log.d(TAG, String.format("Clipping to outline is disabled"));
                ((Button) bt).setText(R.string.clip_button);
            } else {
                /* Enables clipping on the View. */
                clippedView.setClipToOutline(true);

                Log.d(TAG, String.format("Clipping to outline is enabled"));
                ((Button) bt).setText(R.string.unclip_button);
            }
        }
    });

    /* When the text is clicked, a new string is shown. */
    view.findViewById(R.id.text_view).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mClickCount++;

            // Update the text in the TextView
            changeText();

            // Invalidate the outline just in case the TextView changed size
            clippedView.invalidateOutline();
        }
    });
}

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

private void setupDecorView(final Point touchPoint, MaterialPalette palette) {
    final View view = getWindow().getDecorView();

    // The circle starts from an initial size of 0 so clip it such that it is invisible. When
    // the animation later starts, this clip will be clobbered by the circular reveal clip.
    // See ViewAnimationUtils.createCircularReveal.
    view.setOutlineProvider(new ViewOutlineProvider() {
        @Override/* w ww .j  av a 2s  . c  o m*/
        public void getOutline(View view, Outline outline) {
            // Using (0, 0, 0, 0) will not work since the outline will simply be treated as
            // an empty outline.
            outline.setOval(-1, -1, 0, 0);
        }
    });
    view.setClipToOutline(true);

    if (palette != null) {
        view.findViewById(R.id.outgoing_call_animation_circle).setBackgroundColor(palette.mPrimaryColor);
        getWindow().setStatusBarColor(palette.mSecondaryColor);
    }

    view.getViewTreeObserver().addOnPreDrawListener(new OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            final ViewTreeObserver vto = view.getViewTreeObserver();
            if (vto.isAlive()) {
                vto.removeOnPreDrawListener(this);
            }
            final Animator animator = getRevealAnimator(touchPoint);
            // Since this animator is a RenderNodeAnimator (native animator), add an arbitary
            // start delay to force the onAnimationStart callback to happen later on the UI
            // thread. Otherwise it would happen right away inside animator.start()
            animator.setStartDelay(5);
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationStart(Animator animation) {
                    InCallPresenter.getInstance().onCircularRevealStarted(CircularRevealActivity.this);
                }

                @Override
                public void onAnimationEnd(Animator animation) {
                    view.setClipToOutline(false);
                    super.onAnimationEnd(animation);
                }
            });
            animator.start();
            return false;
        }
    });
}

From source file:org.protocoderrunner.apprunner.api.PUI.java

@ProtocoderScript
@APIParam(params = { "View", "x", "y", "w", "h" })
public void clipCircle(View v, final int x, final int y, final int w, final int h) {
    Outline outline = new Outline();
    outline.setOval(x, y, w, h);/*from   w  w  w .j  a v  a2s .  com*/
    v.setClipToOutline(true);

    ViewOutlineProvider viewOutlineProvider = new ViewOutlineProvider() {
        @Override
        public void getOutline(View view, Outline outline) {
            // Or read size directly from the view's width/height
            outline.setOval(x, y, w, h);
        }
    };

    v.setOutlineProvider(viewOutlineProvider);
}

From source file:com.github.takahirom.plaidanimation.transition.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 Rect fabBounds = fromFab ? startBounds : endBounds;
    final Interpolator fastOutSlowInInterpolator = new FastOutSlowInInterpolator();
    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);
    }/*w w w . ja  va2s. c om*/

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

    // 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(new FastOutLinearInInterpolator());
    } 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(new LinearOutSlowInInterpolator());

        // 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) {
                view.setOutlineProvider(new ViewOutlineProvider() {
                    @Override
                    public void getOutline(View view, Outline outline) {
                        final int left = (view.getWidth() - fabBounds.width()) / 2;
                        final int top = (view.getHeight() - fabBounds.height()) / 2;
                        outline.setOval(left, top, left + fabBounds.width(), top + fabBounds.height());
                        view.setClipToOutline(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);

    // Work around issue with elevation shadows. At the end of the return transition the shared
    // element's shadow is drawn twice (by each activity) which is jarring. This workaround
    // still causes the shadow to snap, but it's better than seeing it double drawn.
    Animator elevation = null;
    if (!fromFab) {
        elevation = ObjectAnimator.ofFloat(view, View.TRANSLATION_Z, -view.getElevation());
        elevation.setDuration(duration);
        elevation.setInterpolator(fastOutSlowInInterpolator);
    }

    // Run all animations together
    final AnimatorSet transition = new AnimatorSet();
    transition.playTogether(circularReveal, translate, colorFade, iconFade);
    transition.playTogether(fadeContents);
    if (elevation != null)
        transition.play(elevation);
    if (fromFab) {
        transition.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // Clean up
                view.getOverlay().clear();
            }
        });
    }
    return new AnimUtils.NoPauseAnimator(transition);
}