Example usage for android.graphics Paint setShader

List of usage examples for android.graphics Paint setShader

Introduction

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

Prototype

public Shader setShader(Shader shader) 

Source Link

Document

Set or clear the shader object.

Usage

From source file:org.stockchart.core.Appearance.java

public void applyFill(Paint p, RectF rect) {
    p.reset();/*  w w  w. j a  v a 2 s.  c om*/
    p.setStyle(Style.FILL);
    p.setColor(fPrimaryFillColor);

    switch (fGradient) {
    case LINEAR_HORIZONTAL: {
        p.setShader(new LinearGradient(rect.left, rect.top, rect.right, rect.top, fPrimaryFillColor,
                fSecondaryFillColor, TileMode.MIRROR));
    }
        break;

    case LINEAR_VERTICAL: {
        p.setShader(new LinearGradient(rect.left, rect.top, rect.left, rect.bottom, fPrimaryFillColor,
                fSecondaryFillColor, TileMode.MIRROR));
    }
        break;
    default:
        p.setShader(null);
    }
}

From source file:com.richtodd.android.quiltdesign.block.PaperPiecedBlockPiece.java

private Paint createShadowPaint(int left, int top, int width, int height, int shadowStrokeWidth) {
    List<PointF> points = getPoints(width, height);
    PointF fromPoint = new PointF(left + points.get(0).x, top + points.get(0).y);
    PointF toPoint = new PointF(left + points.get(1).x, top + points.get(1).y);

    PointF midpoint = new PointF((fromPoint.x + toPoint.x) * 0.5f, (fromPoint.y + toPoint.y) * 0.5f);

    PointF vector = new PointF(fromPoint.x - midpoint.x, fromPoint.y - midpoint.y);

    PointF rotatedVector = new PointF(vector.y, -vector.x);

    float vectorLength = (float) Math.sqrt((vector.x * vector.x) + (vector.y * vector.y));

    PointF shadowVector = new PointF(rotatedVector.x * (shadowStrokeWidth * 0.5f) / vectorLength,
            rotatedVector.y * (shadowStrokeWidth * 0.5f) / vectorLength);

    LinearGradient gradient = new LinearGradient(midpoint.x, midpoint.y, midpoint.x + shadowVector.x,
            midpoint.y + shadowVector.y, Color.argb(100, 0, 0, 0), Color.argb(0, 0, 0, 0), TileMode.MIRROR);

    Paint paint = new Paint();
    paint.setStyle(Style.STROKE);
    paint.setShader(gradient);
    paint.setStrokeWidth(shadowStrokeWidth);
    paint.setStrokeCap(Cap.SQUARE);/*  www . j  a  va2 s . c  om*/

    return paint;
}

From source file:github.madmarty.madsonic.util.ImageLoader.java

private Bitmap createReflection(Bitmap originalImage) {

    //   int reflectionH = 80;

    int width = originalImage.getWidth();
    int height = originalImage.getHeight();

    // Height of reflection
    int reflectionHeight = height / 2;

    // The gap we want between the reflection and the original image
    final int reflectionGap = 4;

    // Create a new bitmap with same width but taller to fit reflection
    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + reflectionHeight),
            Bitmap.Config.ARGB_8888);//from  w  w w  .  jav  a2s  . c  om

    //// ----

    Bitmap reflection = Bitmap.createBitmap(width, reflectionHeight, Bitmap.Config.ARGB_8888);
    Bitmap blurryBitmap = Bitmap.createBitmap(originalImage, 0, height - reflectionHeight, height,
            reflectionHeight);

    // cheap and easy scaling algorithm; down-scale it, then
    // upscale it. The filtering during the scale operations
    // will blur the resulting image
    blurryBitmap = Bitmap
            .createScaledBitmap(
                    Bitmap.createScaledBitmap(blurryBitmap, blurryBitmap.getWidth() / 2,
                            blurryBitmap.getHeight() / 2, true),
                    blurryBitmap.getWidth(), blurryBitmap.getHeight(), true);

    // This shadier will hold a cropped, inverted,
    // blurry version of the original image
    BitmapShader bitmapShader = new BitmapShader(blurryBitmap, TileMode.CLAMP, TileMode.CLAMP);
    Matrix invertMatrix = new Matrix();
    invertMatrix.setScale(1f, -1f);
    invertMatrix.preTranslate(0, -reflectionHeight);
    bitmapShader.setLocalMatrix(invertMatrix);

    // This shader holds an alpha gradient
    Shader alphaGradient = new LinearGradient(0, 0, 0, reflectionHeight, 0x80ffffff, 0x00000000,
            TileMode.CLAMP);

    // This shader combines the previous two, resulting in a
    // blurred, fading reflection
    ComposeShader compositor = new ComposeShader(bitmapShader, alphaGradient, PorterDuff.Mode.DST_IN);

    Paint reflectionPaint = new Paint();
    reflectionPaint.setShader(compositor);

    // Draw the reflection into the bitmap that we will return
    Canvas canvas = new Canvas(reflection);
    canvas.drawRect(0, 0, reflection.getWidth(), reflection.getHeight(), reflectionPaint);

    /// -----

    // Create a new Canvas with the bitmap that's big enough for
    // the image plus gap plus reflection
    Canvas finalcanvas = new Canvas(bitmapWithReflection);

    // Draw in the original image
    finalcanvas.drawBitmap(originalImage, 0, 0, null);

    // Draw in the gap
    Paint defaultPaint = new Paint();

    // transparent gap
    defaultPaint.setColor(0);

    finalcanvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);

    // Draw in the reflection
    finalcanvas.drawBitmap(reflection, 0, height + reflectionGap, null);

    return bitmapWithReflection;
}

From source file:github.daneren2005.dsub.util.ImageLoader.java

private Bitmap createUnknownImage(int size, int primaryColor, String topText, String bottomText) {
    Bitmap bitmap = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Paint color = new Paint();
    color.setColor(primaryColor);//w  w w  . ja va  2s  .c o m
    canvas.drawRect(0, 0, size, size * 2.0f / 3.0f, color);

    color.setShader(new LinearGradient(0, 0, 0, size / 3.0f, Color.rgb(82, 82, 82), Color.BLACK,
            Shader.TileMode.MIRROR));
    canvas.drawRect(0, size * 2.0f / 3.0f, size, size, color);

    if (topText != null || bottomText != null) {
        Paint font = new Paint();
        font.setFlags(Paint.ANTI_ALIAS_FLAG);
        font.setColor(Color.WHITE);
        font.setTextSize(3.0f + size * 0.07f);

        if (topText != null) {
            canvas.drawText(topText, size * 0.05f, size * 0.6f, font);
        }

        if (bottomText != null) {
            canvas.drawText(bottomText, size * 0.05f, size * 0.8f, font);
        }
    }

    return bitmap;
}

From source file:com.android.leanlauncher.WidgetPreviewLoader.java

public Bitmap generateWidgetPreview(AppWidgetProviderInfo info, int cellHSpan, int cellVSpan,
        int maxPreviewWidth, int maxPreviewHeight, Bitmap preview, int[] preScaledWidthOut) {
    // Load the preview image if possible
    if (maxPreviewWidth < 0)
        maxPreviewWidth = Integer.MAX_VALUE;

    Drawable drawable = null;//from  w w  w. j av a 2 s. c  om
    if (info.previewImage != 0) {
        drawable = mManager.loadPreview(info);
        if (drawable != null) {
            drawable = mutateOnMainThread(drawable);
        } else {
            Log.w(TAG, "Can't load widget preview drawable 0x" + Integer.toHexString(info.previewImage)
                    + " for provider: " + info.provider);
        }
    }

    int previewWidth;
    int previewHeight;
    Bitmap defaultPreview = null;
    boolean widgetPreviewExists = (drawable != null);
    if (widgetPreviewExists) {
        previewWidth = drawable.getIntrinsicWidth();
        previewHeight = drawable.getIntrinsicHeight();
    } else {
        // Generate a preview image if we couldn't load one
        if (cellHSpan < 1)
            cellHSpan = 1;
        if (cellVSpan < 1)
            cellVSpan = 1;

        // This Drawable is not directly drawn, so there's no need to mutate it.
        BitmapDrawable previewDrawable = (BitmapDrawable) mContext.getResources()
                .getDrawable(R.drawable.widget_tile);
        final int previewDrawableWidth = previewDrawable.getIntrinsicWidth();
        final int previewDrawableHeight = previewDrawable.getIntrinsicHeight();
        previewWidth = previewDrawableWidth * cellHSpan;
        previewHeight = previewDrawableHeight * cellVSpan;

        defaultPreview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        c.setBitmap(defaultPreview);
        Paint p = mDefaultAppWidgetPreviewPaint.get();
        if (p == null) {
            p = new Paint();
            p.setShader(new BitmapShader(previewDrawable.getBitmap(), Shader.TileMode.REPEAT,
                    Shader.TileMode.REPEAT));
            mDefaultAppWidgetPreviewPaint.set(p);
        }
        final Rect dest = mCachedAppWidgetPreviewDestRect.get();
        dest.set(0, 0, previewWidth, previewHeight);
        c.drawRect(dest, p);
        c.setBitmap(null);

        // Draw the icon in the top left corner
        int minOffset = (int) (mAppIconSize * WIDGET_PREVIEW_ICON_PADDING_PERCENTAGE);
        int smallestSide = Math.min(previewWidth, previewHeight);
        float iconScale = Math.min((float) smallestSide / (mAppIconSize + 2 * minOffset), 1f);

        try {
            Bitmap icon = mIconCache.getIconForComponent(info.configure, UserHandleCompat.myUserHandle());
            if (icon != null) {
                int hoffset = (int) ((previewDrawableWidth - mAppIconSize * iconScale) / 2);
                int yoffset = (int) ((previewDrawableHeight - mAppIconSize * iconScale) / 2);
                renderBitmapIconOnPreview(icon, defaultPreview, hoffset, yoffset,
                        (int) (mAppIconSize * iconScale), (int) (mAppIconSize * iconScale));
            }
        } catch (Resources.NotFoundException ignored) {
        }
    }

    // Scale to fit width only - let the widget preview be clipped in the
    // vertical dimension
    float scale = 1f;
    if (preScaledWidthOut != null) {
        preScaledWidthOut[0] = previewWidth;
    }
    if (previewWidth > maxPreviewWidth) {
        scale = maxPreviewWidth / (float) previewWidth;
    }
    if (scale != 1f) {
        previewWidth = (int) (scale * previewWidth);
        previewHeight = (int) (scale * previewHeight);
    }

    // If a bitmap is passed in, we use it; otherwise, we create a bitmap of the right size
    if (preview == null) {
        preview = Bitmap.createBitmap(previewWidth, previewHeight, Config.ARGB_8888);
    }

    // Draw the scaled preview into the final bitmap
    int x = (preview.getWidth() - previewWidth) / 2;
    if (widgetPreviewExists) {
        renderDrawableToBitmap(drawable, preview, x, 0, previewWidth, previewHeight);
    } else {
        final Canvas c = mCachedAppWidgetPreviewCanvas.get();
        final Rect src = mCachedAppWidgetPreviewSrcRect.get();
        final Rect dest = mCachedAppWidgetPreviewDestRect.get();
        c.setBitmap(preview);
        src.set(0, 0, defaultPreview.getWidth(), defaultPreview.getHeight());
        dest.set(x, 0, x + previewWidth, previewHeight);

        Paint p = mCachedAppWidgetPreviewPaint.get();
        if (p == null) {
            p = new Paint();
            p.setFilterBitmap(true);
            mCachedAppWidgetPreviewPaint.set(p);
        }
        c.drawBitmap(defaultPreview, src, dest, p);
        c.setBitmap(null);
    }
    return preview;
}

From source file:net.networksaremadeofstring.rhybudd.RhybuddDock.java

private void DrawEvents() {
    Bitmap charty = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
    Canvas EventsCanvas = new Canvas(charty);
    final Paint paint = new Paint();

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(getResources().getColor(R.color.DarkBlue));
    paint.setAntiAlias(true);/*  w w w  .  j  a  va2s . com*/
    EventsCanvas.drawOval(new RectF(1, 1, 199, 199), paint);

    RadialGradient gradient = new RadialGradient(200, 200, 200, 0xFF6b7681, 0xFF000000,
            android.graphics.Shader.TileMode.CLAMP);
    paint.setDither(true);
    paint.setShader(gradient);
    EventsCanvas.drawOval(new RectF(6, 6, 194, 194), paint);

    //Special Bits
    int Scale = 100;

    if (EventCount > 100)
        ;
    Scale = EventCount + 50;

    drawScale(EventsCanvas, true, EventCount, Scale);
    drawGaugeTitle(EventsCanvas, "Events Count");
    drawGaugeNeedle(EventsCanvas, EventCount, Scale);
    //drawGloss(EventsCanvas);

    ((ImageView) findViewById(R.id.EventsGauge)).setImageBitmap(charty);
}

From source file:net.networksaremadeofstring.rhybudd.RhybuddDock.java

private void DrawDevices() {
    Bitmap charty = Bitmap.createBitmap(200, 200, Bitmap.Config.ARGB_8888);
    Canvas DeviceCanvas = new Canvas(charty);
    final Paint paint = new Paint();

    paint.setStyle(Paint.Style.FILL);
    paint.setColor(getResources().getColor(R.color.DarkBlue));
    paint.setAntiAlias(true);//from w w w. j a v  a  2 s  . co  m
    DeviceCanvas.drawOval(new RectF(1, 1, 199, 199), paint);

    RadialGradient gradient = new RadialGradient(100, 50, 150, 0xFF6b7681, 0xFF000000,
            android.graphics.Shader.TileMode.CLAMP);
    paint.setDither(true);
    paint.setShader(gradient);
    DeviceCanvas.drawOval(new RectF(6, 6, 194, 194), paint);

    int Scale = 10;

    //Special Bits
    if (DeviceCount < 500)
        Scale = 750;

    if (DeviceCount < 250)
        Scale = 350;

    if (DeviceCount < 100)
        Scale = 150;

    if (DeviceCount < 50)
        Scale = 50;
    try {
        drawScale(DeviceCanvas, false, DeviceCount, Scale);
        drawGaugeTitle(DeviceCanvas, "Device Count");
        drawGaugeNeedle(DeviceCanvas, DeviceCount, Scale);
        //drawGloss(EventsCanvas);
        ((ImageView) findViewById(R.id.DeviceGauge)).setImageBitmap(charty);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:android.support.wear.widget.drawer.PageIndicatorView.java

private void updateDotPaint(Paint dotPaint, Paint shadowPaint, float baseRadius, float shadowRadius, int color,
        int shadowColor) {
    float radius = baseRadius + shadowRadius;
    float shadowStart = baseRadius / radius;
    Shader gradient = new RadialGradient(0, 0, radius,
            new int[] { shadowColor, shadowColor, Color.TRANSPARENT }, new float[] { 0f, shadowStart, 1f },
            TileMode.CLAMP);/*  www  .  j  av a2  s  . c om*/

    shadowPaint.setShader(gradient);
    dotPaint.setColor(color);
    dotPaint.setStyle(Style.FILL);
}

From source file:com.dwdesign.tweetings.fragment.UserProfileFragment.java

public static Bitmap createAlphaGradientBitmap(Bitmap orig) {
    final int width = orig.getWidth(), height = orig.getHeight();
    final Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);
    final Paint paint = new Paint();
    final LinearGradient shader = new LinearGradient(width / 2, 0, width / 2, height, 0xffffffff, 0x00ffffff,
            Shader.TileMode.CLAMP);/*from w  ww  . j  a  v  a2  s. c o m*/
    paint.setShader(shader);
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    canvas.drawBitmap(orig, 0, 0, null);
    canvas.drawRect(0, 0, width, height, paint);
    return bitmap;
}

From source file:com.yk.notification.util.BitmapUtil.java

/**
 * //ww  w  . jav a 2 s  . c om
 * 
 * @param bitmap
 *            ?Bitmap
 * @return Bitmap
 */
public static Bitmap createReflectionBitmap(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

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

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    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);

    return bitmapWithReflection;
}