Example usage for android.graphics Paint setShadowLayer

List of usage examples for android.graphics Paint setShadowLayer

Introduction

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

Prototype

public void setShadowLayer(float radius, float dx, float dy, int shadowColor) 

Source Link

Document

This draws a shadow layer below the main layer, with the specified offset and color, and blur radius.

Usage

From source file:com.tafayor.selfcamerashot.taflib.helpers.GraphicsHelper.java

public static Bitmap addShadow(Bitmap src, float radius, float dx, float dy, int shadowColor) {

    int margin = 0;
    if (radius <= 0)
        radius = 1;/*from  ww w.  jav a 2 s .  c  o m*/
    if (dx < 0)
        dx = 0;
    if (dy < 0)
        dy = 0;

    margin = (int) radius;

    Bitmap alpha = src.extractAlpha();

    Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin * 2, src.getHeight() + margin * 2,
            Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bmp);
    //LogHelper.log ("Canvas.isHardwareAccelerated() " + canvas.isHardwareAccelerated() );
    Paint paint = new Paint();
    paint.setColor(shadowColor);

    paint.setShadowLayer(radius, dx, dy, shadowColor);

    canvas.drawBitmap(alpha, margin, margin, paint);
    canvas.drawBitmap(src, margin, margin, null);

    return bmp;
}

From source file:de.vanita5.twittnuker.view.ShapedImageView.java

private void updateShadowBitmap() {
    if (USE_OUTLINE)
        return;/*  w  ww. j a va 2 s .co m*/
    final int width = getWidth(), height = getHeight();
    if (width <= 0 || height <= 0)
        return;
    final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
            contentRight = width - getPaddingRight(), contentBottom = height - getPaddingBottom();
    final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
    final float radius = mShadowRadius, dy = radius * 1.5f / 2;
    final int size = Math.round(Math.min(contentWidth, contentHeight) + radius * 2);
    mShadowBitmap = Bitmap.createBitmap(size, Math.round(size + dy), Config.ARGB_8888);
    Canvas canvas = new Canvas(mShadowBitmap);
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.WHITE);
    paint.setShadowLayer(radius, 0, radius * 1.5f / 2, SHADOW_START_COLOR);
    final RectF rect = new RectF(radius, radius, size - radius, size - radius);
    canvas.drawOval(rect, paint);
    paint.setShadowLayer(0, 0, 0, 0);
    paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
    canvas.drawOval(rect, paint);
    invalidate();
}

From source file:com.android.gallery3d.data.UriImage.java

public Bitmap drawTextToBitmap(Context gContext, String gText, Bitmap bitmap) {
    Resources resources = gContext.getResources();
    float scale = resources.getDisplayMetrics().density;

    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();
    // set default bitmap config if none
    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }/*from   ww w.j  a v a  2 s . c  om*/
    // resource bitmaps are imutable, 
    // so we need to convert it to mutable one
    bitmap = bitmap.copy(bitmapConfig, true);

    Canvas canvas = new Canvas(bitmap);
    // new antialised Paint
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    // text color - #3D3D3D
    paint.setColor(Color.rgb(61, 61, 61));
    // text size in pixels
    paint.setTextSize((int) (25 * scale));
    // text shadow
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    // draw text to the Canvas center
    Rect bounds = new Rect();
    paint.setTextAlign(Align.CENTER);

    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;

    canvas.drawText(gText, x * scale, y * scale, paint);

    return bitmap;
}

From source file:org.getlantern.firetweet.view.ShapedImageView.java

private void updateShadowBitmap() {
    if (USE_OUTLINE)
        return;/*ww  w .  jav  a 2s  .  com*/
    final int width = getWidth(), height = getHeight();
    if (width <= 0 || height <= 0)
        return;
    final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
            contentRight = width - getPaddingRight(), contentBottom = height - getPaddingBottom();
    final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
    final float radius = mShadowRadius, dy = radius * 1.5f / 2;
    final int size = Math.round(Math.min(contentWidth, contentHeight) + radius * 2);
    mShadowBitmap = Bitmap.createBitmap(size, Math.round(size + dy), Config.ARGB_8888);
    Canvas canvas = new Canvas(mShadowBitmap);
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0xFF000000 | mBackgroundPaint.getColor());
    paint.setShadowLayer(radius, 0, radius * 1.5f / 2, SHADOW_START_COLOR);
    final RectF rect = new RectF(radius, radius, size - radius, size - radius);
    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawOval(rect, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawOval(rect, paint);
    } else {
        final float cr = getCalculatedCornerRadius();
        canvas.drawRoundRect(rect, cr, cr, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawRoundRect(rect, cr, cr, paint);
    }
    invalidate();
}

From source file:org.mariotaku.twidere.view.ShapedImageView.java

private void updateShadowBitmap() {
    if (useOutline())
        return;/*w  w w .  j  a v  a2  s. c  o m*/
    final int width = getWidth(), height = getHeight();
    if (width <= 0 || height <= 0)
        return;
    final int contentLeft = getPaddingLeft(), contentTop = getPaddingTop(),
            contentRight = width - getPaddingRight(), contentBottom = height - getPaddingBottom();
    final int contentWidth = contentRight - contentLeft, contentHeight = contentBottom - contentTop;
    final float radius = mShadowRadius, dy = radius * 1.5f / 2;
    final int size = Math.round(Math.min(contentWidth, contentHeight) + radius * 2);
    mShadowBitmap = Bitmap.createBitmap(size, Math.round(size + dy), Config.ARGB_8888);
    Canvas canvas = new Canvas(mShadowBitmap);
    final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(0xFF000000 | mBackgroundPaint.getColor());
    paint.setShadowLayer(radius, 0, radius * 1.5f / 2, SHADOW_START_COLOR);
    final RectF rect = new RectF(radius, radius, size - radius, size - radius);
    if (getStyle() == SHAPE_CIRCLE) {
        canvas.drawOval(rect, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawOval(rect, paint);
    } else {
        final float cr = getCalculatedCornerRadius();
        canvas.drawRoundRect(rect, cr, cr, paint);
        paint.setShadowLayer(0, 0, 0, 0);
        paint.setXfermode(new PorterDuffXfermode(Mode.CLEAR));
        canvas.drawRoundRect(rect, cr, cr, paint);
    }
    invalidate();
}

From source file:com.codetroopers.shakemytours.ui.activity.TripActivity.java

private Bitmap createMarker(int radius, int strokeWidth, String letter, int strokeColor, int backgroundColor) {
    int width = (radius * 2) + (strokeWidth * 2);
    Bitmap marker = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(marker);

    Paint strokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    strokePaint.setColor(strokeColor);/*  w  ww  . j a va2s.  c  o  m*/
    strokePaint.setShadowLayer(strokeWidth, 1.0f, 1.0f, Color.BLACK);
    canvas.drawCircle(width / 2, width / 2, radius, strokePaint);

    Paint backgroundPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
    backgroundPaint.setColor(backgroundColor);
    canvas.drawCircle(width / 2, width / 2, radius - strokeWidth, backgroundPaint);

    if (letter != null) {
        Rect result = new Rect();
        Paint textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        textPaint.setTextAlign(Paint.Align.CENTER);
        textPaint.setTextSize(getResources().getDimensionPixelSize(R.dimen.map_marker_text_size));
        textPaint.setColor(strokeColor);
        textPaint.getTextBounds(letter, 0, letter.length(), result);
        int yOffset = result.height() / 2;

        canvas.drawText(letter, width / 2, (width / 2) + yOffset, textPaint);
    }
    return marker;
}

From source file:com.androidmapsextensions.DefaultClusterOptionsProvider.java

private Paint createCircleShadowPaint(Resources resources) {
    Paint paint = null;
    float circleShadowBlurRadius = resources
            .getDimension(R.dimen.ame_default_cluster_circle_shadow_blur_radius);
    if (circleShadowBlurRadius > 0.0f) {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        float offsetX = resources.getDimension(R.dimen.ame_default_cluster_circle_shadow_offset_x);
        float offsetY = resources.getDimension(R.dimen.ame_default_cluster_circle_shadow_offset_y);
        int color = resources.getColor(R.color.ame_default_cluster_circle_shadow_color);
        paint.setShadowLayer(circleShadowBlurRadius, offsetX, offsetY, color);
    }//from   www  .  ja v  a  2  s  .  c  om
    return paint;
}

From source file:com.androidmapsextensions.DefaultClusterOptionsProvider.java

private Paint createTextPaint(Resources resources) {
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(resources.getColor(R.color.ame_default_cluster_text_color));
    shadowBlurRadius = resources.getDimension(R.dimen.ame_default_cluster_text_shadow_blur_radius);
    if (shadowBlurRadius > 0.0f) {
        shadowOffsetX = resources.getDimension(R.dimen.ame_default_cluster_text_shadow_offset_x);
        shadowOffsetY = resources.getDimension(R.dimen.ame_default_cluster_text_shadow_offset_y);
        int shadowColor = resources.getColor(R.color.ame_default_cluster_text_shadow_color);
        paint.setShadowLayer(shadowBlurRadius, shadowOffsetX, shadowOffsetY, shadowColor);
    }//  w  ww .  j a  v  a  2 s  .  c o m
    paint.setTextSize(resources.getDimension(R.dimen.ame_default_cluster_text_size));
    paint.setTypeface(Typeface.DEFAULT_BOLD);
    return paint;
}

From source file:net.gsantner.opoc.util.ContextUtils.java

/**
 * Draw text in the center of the given {@link DrawableRes}
 * This may be useful for e.g. badge counts
 *///w  w  w  . jav  a  2 s. co m
public Bitmap drawTextOnDrawable(@DrawableRes int drawableRes, String text, int textSize) {
    Resources resources = _context.getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = drawableToBitmap(drawableRes);

    bitmap = bitmap.copy(bitmap.getConfig(), true);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(61, 61, 61));
    paint.setTextSize((int) (textSize * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.WHITE);

    Rect bounds = new Rect();
    paint.getTextBounds(text, 0, text.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2;
    canvas.drawText(text, x, y, paint);

    return bitmap;
}

From source file:com.example.angel.parkpanda.MainActivity.java

public Bitmap drawTextToBitmap(int gResId, String gText) {
    Resources resources = getResources();
    float scale = resources.getDisplayMetrics().density;
    Bitmap bitmap = BitmapFactory.decodeResource(resources, gResId);
    android.graphics.Bitmap.Config bitmapConfig = bitmap.getConfig();

    if (bitmapConfig == null) {
        bitmapConfig = android.graphics.Bitmap.Config.ARGB_8888;
    }//ww  w .  j  a  v  a 2 s .c  o m
    bitmap = bitmap.copy(bitmapConfig, true);
    Canvas canvas = new Canvas(bitmap);

    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setColor(Color.rgb(0, 0, 0));
    paint.setTextSize((int) (15 * scale));
    paint.setShadowLayer(1f, 0f, 1f, Color.BLACK);

    Rect bounds = new Rect();
    paint.getTextBounds(gText, 0, gText.length(), bounds);
    int x = (bitmap.getWidth() - bounds.width()) / 2;
    int y = (bitmap.getHeight() + bounds.height()) / 2 - 10;
    canvas.drawText(gText, x, y, paint);

    return bitmap;
}