android.graphics.drawable.PaintDrawable

Here are the examples of the java api class android.graphics.drawable.PaintDrawable taken from open source projects.

1. CommentListItemBackgroundFactory#makeDefaultBackground()

Project: socialize-sdk-android
File: CommentListItemBackgroundFactory.java
protected Drawable makeDefaultBackground() {
    PaintDrawable shadow = new PaintDrawable(bottomColor);
    PaintDrawable highlight = new PaintDrawable(topColor);
    PaintDrawable surface = new PaintDrawable(bgColor);
    LayerDrawable layers = new LayerDrawable(new Drawable[] { shadow, highlight, surface });
    layers.setLayerInset(0, 0, 0, 0, 0);
    layers.setLayerInset(1, 0, 0, 0, 1);
    layers.setLayerInset(2, 0, 1, 0, 1);
    return layers;
}

2. Ui#getActionBarDrawable()

Project: Genius-Android
File: Ui.java
/**
     * Returns a suitable drawable for ActionBar with theme colors.
     *
     * @param theme        selected theme
     * @param dark         boolean for choosing dark colors or primary colors
     * @param borderBottom bottom border width
     * @return drawable to be used in ActionBar
     */
public static Drawable getActionBarDrawable(Activity activity, int theme, boolean dark, float borderBottom) {
    int[] colors = activity.getResources().getIntArray(theme);
    int color1 = colors[2];
    int color2 = colors[1];
    if (dark) {
        color1 = colors[1];
        color2 = colors[0];
    }
    borderBottom = dipToPx(activity, borderBottom);
    PaintDrawable front = new PaintDrawable(color1);
    PaintDrawable bottom = new PaintDrawable(color2);
    Drawable[] d = { bottom, front };
    LayerDrawable drawable = new LayerDrawable(d);
    drawable.setLayerInset(1, 0, 0, 0, (int) borderBottom);
    return drawable;
}

3. ScrimUtil#makeCubicGradientScrimDrawable()

Project: sbt-android
File: 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.
     */
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;
}

4. ScrimUtil#makeCubicGradientScrimDrawable()

Project: plaid
File: 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.
     */
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;
}

5. ScrimUtil#makeCubicGradientScrimDrawable()

Project: muzei
File: 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.
     */
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;
}

6. UIUtils#makeCubicGradientScrimDrawable()

Project: iosched
File: UIUtils.java
//    private static final float[] mAlphaMatrixValues = {
//            0, 0, 0, 0, 0,
//            0, 0, 0, 0, 0,
//            0, 0, 0, 0, 0,
//            0, 0, 0, 1, 0
//    };
//    private static final ColorMatrix mMultiplyBlendMatrix = new ColorMatrix();
//    private static final float[] mMultiplyBlendMatrixValues = {
//            0, 0, 0, 0, 0,
//            0, 0, 0, 0, 0,
//            0, 0, 0, 0, 0,
//            0, 0, 0, 1, 0
//    };
//    private static final ColorMatrix mWhitenessColorMatrix = new ColorMatrix();
//
//    /**
//     * Simulates alpha blending an image with {@param color}.
//     */
//    private static ColorMatrix alphaMatrix(float alpha, int color) {
//        mAlphaMatrixValues[0] = 255 * alpha / 255;
//        mAlphaMatrixValues[6] = Color.green(color) * alpha / 255;
//        mAlphaMatrixValues[12] = Color.blue(color) * alpha / 255;
//        mAlphaMatrixValues[4] = 255 * (1 - alpha);
//        mAlphaMatrixValues[9] = 255 * (1 - alpha);
//        mAlphaMatrixValues[14] = 255 * (1 - alpha);
//        mWhitenessColorMatrix.set(mAlphaMatrixValues);
//        return mWhitenessColorMatrix;
//    }
//    /**
//     * Simulates multiply blending an image with a single {@param color}.
//     *
//     * Multiply blending is [Sa * Da, Sc * Dc]. See {@link android.graphics.PorterDuff}.
//     */
//    private static ColorMatrix multiplyBlendMatrix(int color, float alpha) {
//        mMultiplyBlendMatrixValues[0] = multiplyBlend(Color.red(color), alpha);
//        mMultiplyBlendMatrixValues[6] = multiplyBlend(Color.green(color), alpha);
//        mMultiplyBlendMatrixValues[12] = multiplyBlend(Color.blue(color), alpha);
//        mMultiplyBlendMatrix.set(mMultiplyBlendMatrixValues);
//        return mMultiplyBlendMatrix;
//    }
//
//    private static float multiplyBlend(int color, float alpha) {
//        return color * alpha / 255.0f + (1 - alpha);
//    }
/**
     * 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.
     */
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;
}

7. GradientUtil#getCubicGradient()

Project: apps-android-wikipedia
File: GradientUtil.java
public static Drawable getCubicGradient(int baseColor, int gravity) {
    PaintDrawable drawable = new PaintDrawable();
    drawable.setShape(new RectShape());
    setCubicGradient(drawable, baseColor, gravity);
    return drawable;
}

8. OcticonView#initialize()

Project: hubroid
File: OcticonView.java
protected void initialize(AttributeSet attrs) {
    if (OCTICON_TYPE == null) {
        OCTICON_TYPE = Typeface.createFromAsset(getContext().getAssets(), OCTICON_FONT);
    }
    mPaintDrawable = new PaintDrawable(Color.TRANSPARENT);
    mPaintDrawable.setCornerRadius(OCTICON_CORNER_RADIUS);
    setBackgroundDrawable(mPaintDrawable);
    mTextView = new TextView(getContext());
    mTextView.setBackgroundDrawable(null);
    mTextView.setTypeface(OCTICON_TYPE);
    mTextView.setTextColor(Color.WHITE);
    LayoutParams textViewParams = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
    textViewParams.addRule(CENTER_IN_PARENT);
    addView(mTextView, textViewParams);
    LayoutParams layoutParams = (LayoutParams) getLayoutParams();
    if (layoutParams == null) {
        layoutParams = new LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
    }
    setLayoutParams(layoutParams);
    if (attrs != null) {
        int[] handledAttrs = { android.R.attr.textSize, android.R.attr.textColor };
        final TypedArray attrArray = getContext().obtainStyledAttributes(attrs, handledAttrs);
        setGlyphSize(attrArray.getDimension(0, 24));
        mTextView.setTextColor(attrArray.getColor(1, Color.WHITE));
    }
    setId(getId());
}