Example usage for android.graphics LinearGradient LinearGradient

List of usage examples for android.graphics LinearGradient LinearGradient

Introduction

In this page you can find the example usage for android.graphics LinearGradient LinearGradient.

Prototype

public LinearGradient(float x0, float y0, float x1, float y1, @ColorInt int color0, @ColorInt int color1,
        @NonNull TileMode tile) 

Source Link

Document

Create a shader that draws a linear gradient along a line.

Usage

From source file:Main.java

public static Bitmap createReflectedBitmap(Bitmap srcBitmap, float reflectHeight) {
    if (null == srcBitmap) {
        return null;
    }/* w w w .ja va  2s .com*/

    int srcWidth = srcBitmap.getWidth();
    int srcHeight = srcBitmap.getHeight();
    int reflectionWidth = srcBitmap.getWidth();
    int reflectionHeight = reflectHeight == 0 ? srcHeight / 3 : (int) (reflectHeight * srcHeight);

    if (0 == srcWidth || srcHeight == 0) {
        return null;
    }

    // The matrix
    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);

    try {
        // The reflection bitmap, width is same with original's
        Bitmap reflectionBitmap = Bitmap.createBitmap(srcBitmap, 0, srcHeight - reflectionHeight,
                reflectionWidth, reflectionHeight, matrix, false);

        if (null == reflectionBitmap) {
            return null;
        }

        Canvas canvas = new Canvas(reflectionBitmap);

        Paint paint = new Paint();
        paint.setAntiAlias(true);

        LinearGradient shader = new LinearGradient(0, 0, 0, reflectionBitmap.getHeight(), 0x70FFFFFF,
                0x00FFFFFF, TileMode.MIRROR);
        paint.setShader(shader);

        paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.DST_IN));

        // Draw the linear shader.
        canvas.drawRect(0, 0, reflectionBitmap.getWidth(), reflectionBitmap.getHeight(), paint);

        return reflectionBitmap;
    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

From source file:Main.java

/**
 * Create a Bitmap that will be used as a mask to create the preview window in the background.
 * The Bitmap will be the given width and height. The Bitmap will be transparent except for a
 * gradient on the left and bottom sides.
 *
 * @param width Width of the mask./*w  ww .  ja v a  2  s.  c om*/
 * @param height Height of the mask.
 * @param gradientSize Size of the gradient.
 * @return Bitmap mask that will be used to create a preview window.
 */
private static Bitmap createPreviewWindowMask(int width, int height, int gradientSize) {

    // Do nothing if the size is invalid
    if (width <= 0 || height <= 0) {
        return null;
    }

    // Initialize the mask
    Bitmap mask = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(mask);
    canvas.drawColor(Color.TRANSPARENT);

    // If the gradient size is zero, don't draw the gradients
    if (gradientSize <= 0) {
        return mask;
    }

    // Calculate gradient rects
    Rect leftGradientRect = new Rect(0, 0, gradientSize, height - gradientSize);
    Rect bottomGradientRect = new Rect(leftGradientRect.right, height - gradientSize, width, height);
    Rect cornerGradientRect = new Rect(leftGradientRect.left, leftGradientRect.bottom, bottomGradientRect.left,
            bottomGradientRect.bottom);

    // Create left gradient
    Paint leftGradientPaint = new Paint();
    leftGradientPaint.setDither(true);
    leftGradientPaint.setShader(new LinearGradient(leftGradientRect.left, 0, leftGradientRect.right, 0,
            Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP));

    // Create right gradient
    Paint bottomGradientPaint = new Paint();
    bottomGradientPaint.setDither(true);
    bottomGradientPaint.setShader(
            new LinearGradient(leftGradientRect.right, bottomGradientRect.bottom, leftGradientRect.right,
                    bottomGradientRect.top, Color.BLACK, Color.TRANSPARENT, Shader.TileMode.CLAMP));

    // Create corner gradient
    Paint cornerGradientPaint = new Paint();
    cornerGradientPaint.setDither(true);
    cornerGradientPaint.setShader(new RadialGradient(cornerGradientRect.right, cornerGradientRect.top,
            gradientSize, Color.TRANSPARENT, Color.BLACK, Shader.TileMode.CLAMP));

    // Draw the gradients
    canvas.drawRect(leftGradientRect, leftGradientPaint);
    canvas.drawRect(bottomGradientRect, bottomGradientPaint);
    canvas.drawRect(cornerGradientRect, cornerGradientPaint);

    return mask;
}

From source file:Main.java

public static Bitmap toReflectionBitmap(Bitmap bitmap) {
    if (bitmap == null) {
        return null;
    }/* w ww .  jav  a  2s.  c  om*/

    try {
        int reflectionGap = 1;
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();

        // This will not scale but will flip on the Y axis
        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);

        // Create a Bitmap with the flip matrix applied to it.
        // We only want the bottom half of the image
        Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

        // Create a new bitmap with same width but taller to fit
        // reflection
        Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

        // Create a new Canvas with the bitmap that's big enough for
        // the image plus gap plus reflection
        Canvas canvas = new Canvas(bitmapWithReflection);
        // Draw in the original image
        canvas.drawBitmap(bitmap, 0, 0, null);
        // Draw in the gap
        Paint deafaultPaint = new Paint();
        canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
        // Draw in the reflection
        canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);
        // Create a shader that is a linear gradient that covers the
        // reflection
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
        // Set the paint to use this shader (linear gradient)
        paint.setShader(shader);
        // Set the Transfer mode to be porter duff and destination in
        paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
        // Draw a rectangle using the paint with our linear gradient
        canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

        bitmap = bitmapWithReflection;
    } catch (Exception e) {
        e.printStackTrace();
    }
    return bitmap;
}

From source file:Main.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./*  w  w  w .jav  a2  s .  co m*/
 */
public static Drawable makeCubicGradientScrimDrawable(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 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 = 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.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        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);
        }
    });

    return paintDrawable;
}

From source file:Main.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  w  w  w  .  ja v  a 2s. co  m
 */
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 = Math.max(0, Math.min(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.START:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.END:
        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;
}

From source file:Main.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   w ww .ja v  a 2 s  . c  om
 */
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;
}

From source file:Main.java

/**
 * This helper method creates a 'nice' scrim or background protection for layering text over
 * an image. This non-linear scrim is less noticable than a linear or constant one.
 *
 * Borrowed from github.com/romannurik/muzei
 *
 * 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.//  w  w  w .  j ava  2  s. c  om
 */
public static Drawable makeCubicGradientScrimDrawable(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++) {
        double x = i * 1f / (numStops - 1);
        double opacity = Math.max(0, Math.min(1, Math.pow(x, 3)));
        stopColors[i] = (baseColor & 0x00ffffff) | ((int) (alpha * opacity) << 24);
    }

    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.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./* www .  ja v a 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:Main.java

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient.
 *//*from www  .  j av a  2  s .c o m*/
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 = 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;
}

From source file:Main.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.//  w w w .  jav a 2  s  .c o m
 */
public static Drawable makeCubicGradientScrimDrawable(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 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 = (float) Math.max(0, Math.min(1, 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;
        }
    });

    return paintDrawable;
}