Java Code Examples for android.graphics.drawable.PaintDrawable#setShaderFactory()

The following examples show how to use android.graphics.drawable.PaintDrawable#setShaderFactory() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Ariana.java    From Ariana with Apache License 2.0 6 votes vote down vote up
public static Drawable drawable(final int[] colorBoxes, final float[] position, final GradientAngle gradientAngle) {
    ShapeDrawable.ShaderFactory shaderFactory = new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            AngleCoordinate ac = AngleCoordinate.getAngleCoordinate(gradientAngle, width, height);
            LinearGradient linearGradient = new LinearGradient(ac.x1, ac.y1, ac.x2, ac.y2,
                    colorBoxes,
                    position,
                    Shader.TileMode.REPEAT);
            return linearGradient;
        }
    };
    PaintDrawable paint = new PaintDrawable();
    paint.setShape(new RectShape());
    paint.setShaderFactory(shaderFactory);
    return paint;
}
 
Example 2
Source File: ScrimUtil.java    From music_player with Open Software License 3.0 4 votes vote down vote up
/**
 * 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.
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

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

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

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    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) {
            return new LinearGradient(
                    width * x0,
                    height * y0,
                    width * x1,
                    height * y1,
                    stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}
 
Example 3
Source File: ScrimUtil.java    From StatusBarColorManager with MIT License 4 votes vote down vote up
/**
 * 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.
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

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

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

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    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) {
            return new LinearGradient(
                    width * x0,
                    height * y0,
                    width * x1,
                    height * y1,
                    stopColors, null,
                    Shader.TileMode.CLAMP);
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}
 
Example 4
Source File: ScrimUtil.java    From DialogUtils with Apache License 2.0 4 votes vote down vote up
/**
 * 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.
 */
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

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

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

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
       /* float opacity = MathUtil.constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);*/
    }

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

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}
 
Example 5
Source File: ScrimUtil.java    From android-proguards with Apache License 2.0 4 votes vote down vote up
/**
 * 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.
 */
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.constrain(0, 1, (float) Math.pow(x, 3));
        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;
}