Example usage for android.support.v4.math MathUtils clamp

List of usage examples for android.support.v4.math MathUtils clamp

Introduction

In this page you can find the example usage for android.support.v4.math MathUtils clamp.

Prototype

public static int clamp(int value, int min, int max) 

Source Link

Document

This method takes a numerical value and ensures it fits in a given numerical range.

Usage

From source file:io.plaidapp.core.util.ScrimUtil.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient. See
 * <a href="https://plus.google.com/+RomanNurik/posts/2QvHVFWrHZf">this post</a> for more
 * details.//from   ww w .j  ava 2  s .  co m
 */
public static Drawable makeCubicGradientScrimDrawable(@ColorInt int baseColor, int numStops, int gravity) {
    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtils.clamp((float) Math.pow(x, 3), 0, 1);
        stopColors[i] = ColorUtils.modifyAlpha(baseColor, (int) (alpha * opacity));
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.RIGHT:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(width * x0, height * y0, width * x1, height * y1,
                    stopColors, null, Shader.TileMode.CLAMP);
            return linearGradient;
        }
    });

    return paintDrawable;
}

From source file:io.plaidapp.core.util.CollapsingTextHelper.java

/**
 * Set the value indicating the current scroll value. This decides how much of the
 * background will be displayed, as well as the title metrics/positioning.
 * <p/>/*from w ww .  jav a  2 s  . c  o m*/
 * A value of {@code 0.0} indicates that the layout is fully expanded.
 * A value of {@code 1.0} indicates that the layout is fully collapsed.
 */
public void setExpansionFraction(@FloatRange(from = 0f, to = 1f) float fraction) {
    fraction = MathUtils.clamp(fraction, 0f, 1f);
    if (fraction != mExpandedFraction) {
        mExpandedFraction = fraction;
        calculateCurrentOffsets();
    }
}

From source file:io.plaidapp.core.util.ColorUtils.java

/**
 * Calculate a variant of the color to make it more suitable for overlaying information. Light
 * colors will be lightened and dark colors will be darkened
 *
 * @param color the color to adjust// w w w  .  j av  a  2s .c  o  m
 * @param isDark whether {@code color} is light or dark
 * @param lightnessMultiplier the amount to modify the color e.g. 0.1f will alter it by 10%
 * @return the adjusted color
 */
public static @ColorInt int scrimify(@ColorInt int color, boolean isDark,
        @FloatRange(from = 0f, to = 1f) float lightnessMultiplier) {
    float[] hsl = new float[3];
    android.support.v4.graphics.ColorUtils.colorToHSL(color, hsl);

    if (!isDark) {
        lightnessMultiplier += 1f;
    } else {
        lightnessMultiplier = 1f - lightnessMultiplier;
    }

    hsl[2] = MathUtils.clamp(hsl[2] * lightnessMultiplier, 0f, 1f);
    return android.support.v4.graphics.ColorUtils.HSLToColor(hsl);
}

From source file:com.commonsware.cwac.crossport.design.widget.HeaderScrollingViewBehavior.java

final int getOverlapPixelsForOffset(final View header) {
    return mOverlayTop == 0 ? 0
            : MathUtils.clamp((int) (getOverlapRatioForOffset(header) * mOverlayTop), 0, mOverlayTop);
}

From source file:com.commonsware.cwac.crossport.design.widget.HeaderBehavior.java

int setHeaderTopBottomOffset(CoordinatorLayout parent, V header, int newOffset, int minOffset, int maxOffset) {
    final int curOffset = getTopAndBottomOffset();
    int consumed = 0;

    if (minOffset != 0 && curOffset >= minOffset && curOffset <= maxOffset) {
        // If we have some scrolling range, and we're currently within the min and max
        // offsets, calculate a new offset
        newOffset = MathUtils.clamp(newOffset, minOffset, maxOffset);

        if (curOffset != newOffset) {
            setTopAndBottomOffset(newOffset);
            // Update how much dy we have consumed
            consumed = curOffset - newOffset;
        }//from  w ww  .ja  va 2  s.c  o m
    }

    return consumed;
}

From source file:com.commonsware.cwac.crossport.design.widget.CollapsingTextHelper.java

/**
   * Set the value indicating the current scroll value. This decides how much of the
   * background will be displayed, as well as the title metrics/positioning.
 *
   * A value of {@code 0.0} indicates that the layout is fully expanded.
   * A value of {@code 1.0} indicates that the layout is fully collapsed.
 *//*from  w  ww .j av  a2 s.  c o m*/
void setExpansionFraction(float fraction) {
    fraction = MathUtils.clamp(fraction, 0f, 1f);

    if (fraction != mExpandedFraction) {
        mExpandedFraction = fraction;
        calculateCurrentOffsets();
    }
}

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

/**
 * Set the value indicating the current scroll value. This decides how much of the
 * background will be displayed, as well as the title metrics/positioning.
 * <p>//from   w w  w .  j  av a 2  s .  c  o  m
 * A value of {@code 0.0} indicates that the layout is fully expanded.
 * A value of {@code 1.0} indicates that the layout is fully collapsed.
 */
public void setExpansionFraction(float fraction) {
    fraction = MathUtils.clamp(fraction, 0f, 1f);

    if (fraction != mExpandedFraction) {
        mExpandedFraction = fraction;
        calculateCurrentOffsets();
    }
}