Search in sources :

Example 31 with PaintDrawable

use of android.graphics.drawable.PaintDrawable in project quran_android by quran.

the class QuranDisplayHelper method getPaintDrawable.

public static PaintDrawable getPaintDrawable(int startX, int endX) {
    PaintDrawable drawable = new PaintDrawable();
    drawable.setShape(new RectShape());
    drawable.setShaderFactory(getShaderFactory(startX, endX));
    return drawable;
}
Also used : RectShape(android.graphics.drawable.shapes.RectShape) PaintDrawable(android.graphics.drawable.PaintDrawable)

Example 32 with PaintDrawable

use of android.graphics.drawable.PaintDrawable in project Launcher3 by chislon.

the class Utilities method createIconBitmap.

/**
 * Returns a bitmap suitable for the all apps view.
 */
static Bitmap createIconBitmap(Drawable icon, Context context) {
    synchronized (sCanvas) {
        // we share the statics :-(
        if (sIconWidth == -1) {
            initStatics(context);
        }
        int width = sIconWidth;
        int height = sIconHeight;
        if (icon instanceof PaintDrawable) {
            PaintDrawable painter = (PaintDrawable) icon;
            painter.setIntrinsicWidth(width);
            painter.setIntrinsicHeight(height);
        } else if (icon instanceof BitmapDrawable) {
            // Ensure the bitmap has a density.
            BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
            Bitmap bitmap = bitmapDrawable.getBitmap();
            if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
            }
        }
        int sourceWidth = icon.getIntrinsicWidth();
        int sourceHeight = icon.getIntrinsicHeight();
        if (sourceWidth > 0 && sourceHeight > 0) {
            // Scale the icon proportionally to the icon dimensions
            final float ratio = (float) sourceWidth / sourceHeight;
            if (sourceWidth > sourceHeight) {
                height = (int) (width / ratio);
            } else if (sourceHeight > sourceWidth) {
                width = (int) (height * ratio);
            }
        }
        // no intrinsic size --> use default size
        int textureWidth = sIconTextureWidth;
        int textureHeight = sIconTextureHeight;
        final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight, Bitmap.Config.ARGB_8888);
        final Canvas canvas = sCanvas;
        canvas.setBitmap(bitmap);
        final int left = (textureWidth - width) / 2;
        final int top = (textureHeight - height) / 2;
        // suppress dead code warning
        @SuppressWarnings("all") final boolean debug = false;
        if (debug) {
            // draw a big box for the icon for debugging
            canvas.drawColor(sColors[sColorIndex]);
            if (++sColorIndex >= sColors.length)
                sColorIndex = 0;
            Paint debugPaint = new Paint();
            debugPaint.setColor(0xffcccc00);
            canvas.drawRect(left, top, left + width, top + height, debugPaint);
        }
        sOldBounds.set(icon.getBounds());
        icon.setBounds(left, top, left + width, top + height);
        icon.draw(canvas);
        icon.setBounds(sOldBounds);
        canvas.setBitmap(null);
        return bitmap;
    }
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) PaintDrawable(android.graphics.drawable.PaintDrawable) BitmapDrawable(android.graphics.drawable.BitmapDrawable) Paint(android.graphics.Paint) Paint(android.graphics.Paint)

Example 33 with PaintDrawable

use of android.graphics.drawable.PaintDrawable in project Douya by DreaminginCodeZH.

the class DrawableUtils method makeScrimDrawable.

// From Muzei, Copyright 2014 Google Inc.
public static Drawable makeScrimDrawable(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 = MathUtils.constrain((float) Math.pow(x, 3), 0, 1);
        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);
        }
    });
    paintDrawable.setAlpha(Math.round(0.4f * 255));
    return paintDrawable;
}
Also used : LinearGradient(android.graphics.LinearGradient) RectShape(android.graphics.drawable.shapes.RectShape) ShapeDrawable(android.graphics.drawable.ShapeDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) Shader(android.graphics.Shader)

Example 34 with PaintDrawable

use of android.graphics.drawable.PaintDrawable in project TicktockMusic by Lauzy.

the class ScrimUtil method makeCubicGradientScrimDrawable.

/**
 * 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;
}
Also used : LinearGradient(android.graphics.LinearGradient) RectShape(android.graphics.drawable.shapes.RectShape) PaintDrawable(android.graphics.drawable.PaintDrawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) Drawable(android.graphics.drawable.Drawable) ShapeDrawable(android.graphics.drawable.ShapeDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) Shader(android.graphics.Shader)

Example 35 with PaintDrawable

use of android.graphics.drawable.PaintDrawable in project iosched by google.

the class UIUtils method makeCubicGradientScrimDrawable.

// 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.
 * <p/>
 * Borrowed from github.com/romannurik/muzei
 * <p/>
 * 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) {
        // noinspection RtlHardcoded
        case Gravity.LEFT:
            x0 = 1;
            x1 = 0;
            break;
        // noinspection RtlHardcoded
        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;
}
Also used : LinearGradient(android.graphics.LinearGradient) RectShape(android.graphics.drawable.shapes.RectShape) ShapeDrawable(android.graphics.drawable.ShapeDrawable) PaintDrawable(android.graphics.drawable.PaintDrawable) Shader(android.graphics.Shader) Paint(android.graphics.Paint)

Aggregations

PaintDrawable (android.graphics.drawable.PaintDrawable)35 RectShape (android.graphics.drawable.shapes.RectShape)13 Paint (android.graphics.Paint)12 Bitmap (android.graphics.Bitmap)11 Canvas (android.graphics.Canvas)11 LinearGradient (android.graphics.LinearGradient)11 BitmapDrawable (android.graphics.drawable.BitmapDrawable)11 ShapeDrawable (android.graphics.drawable.ShapeDrawable)11 Shader (android.graphics.Shader)8 Drawable (android.graphics.drawable.Drawable)7 LayerDrawable (android.graphics.drawable.LayerDrawable)5 TypedArray (android.content.res.TypedArray)4 GradientDrawable (android.graphics.drawable.GradientDrawable)3 StateListDrawable (android.graphics.drawable.StateListDrawable)3 Attributes (com.cengalabs.flatui.Attributes)3 Typeface (android.graphics.Typeface)2 InsetDrawable (android.graphics.drawable.InsetDrawable)2 TextPaint (android.text.TextPaint)2 Resources (android.content.res.Resources)1 ClipDrawable (android.graphics.drawable.ClipDrawable)1