Example usage for android.graphics Canvas drawBitmap

List of usage examples for android.graphics Canvas drawBitmap

Introduction

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

Prototype

public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) 

Source Link

Document

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.

Usage

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) {
    if (bitmap == null)
        return bitmap;
    maxHeight = (int) (getApplicationContext().getResources().getDisplayMetrics().density * maxHeight);
    float scale = bitmap.getHeight() * 1.0f / maxHeight;
    int newWidth = (int) (bitmap.getWidth() / scale);

    Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final Rect dstRect = new Rect(0, 0, newWidth, maxHeight);
    final RectF rectF = new RectF(dstRect);

    paint.setAntiAlias(true);/*from  w  w  w  .  j a  va2  s  .co  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, dstRect, paint);

    return output;
}

From source file:Main.java

/**
 * Creates a bitmap with rounded corners
 *
 * @param bitmap    source bitmap/*from w  w  w  .  ja v  a  2s.  c  o m*/
 * @param maxHeight maximal height for result bitmap
 * @param pixels    corner radius
 * @return a new bitmap if succeed, nil instead
 */
public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int maxHeight, int pixels) {
    if (bitmap == null || sApplicationContext == null) {
        return bitmap;
    }
    maxHeight = (int) (sApplicationContext.getResources().getDisplayMetrics().density * maxHeight);
    float scale = bitmap.getHeight() * 1.0f / maxHeight;
    int newWidth = (int) (bitmap.getWidth() / scale);

    Bitmap output = Bitmap.createBitmap(newWidth, maxHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int color = 0xffffffff;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final Rect dstRect = new Rect(0, 0, newWidth, maxHeight);
    final RectF rectF = new RectF(dstRect);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, (float) pixels, (float) pixels, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, dstRect, paint);

    return output;
}

From source file:Main.java

public static Bitmap getCroppedBitmap(Bitmap bmp, int radius) {

    Bitmap sbmp;//  w  w  w.j  av  a2 s .com
    if (bmp.getWidth() != radius || bmp.getHeight() != radius)
        sbmp = Bitmap.createScaledBitmap(bmp, radius, radius, false);
    else
        sbmp = bmp;
    Bitmap output = Bitmap.createBitmap(sbmp.getWidth(), sbmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    //final int color = 0xffa19774;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, sbmp.getWidth(), sbmp.getHeight());
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.parseColor("#BAB399"));
    canvas.drawCircle(sbmp.getWidth() / 2 + 0.7f, sbmp.getHeight() / 2 + 0.7f, sbmp.getWidth() / 2 + 0.1f,
            paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(sbmp, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    if (bitmap == null)
        return null;
    Bitmap output;/*from   w  ww  .  j  a va2  s  .co m*/
    try {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);

        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);
    } catch (Exception ex) {

        //adding as a precaution because finding a few crashes in bitmap modification
        return bitmap;
    }
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float cornerRadius) {
    if (bitmap == null) {
        return null;
    }/*from w ww . java2 s . c  om*/
    Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(result);

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(result, rect, rect, paint);

    return result;
}

From source file:Main.java

public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp,
        boolean fitInScreen) {
    if (fitInScreen) {
        source = scaleTo(scaler, source, targetWidth, targetHeight, true);
    }/*from  w w  w.ja  v a 2  s  . com*/
    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if ((!scaleUp || fitInScreen) && (deltaX < 0 || deltaY < 0)) {
        Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b2);

        int deltaXHalf = Math.max(0, deltaX / 2);
        int deltaYHalf = Math.max(0, deltaY / 2);
        Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()),
                deltaYHalf + Math.min(targetHeight, source.getHeight()));
        int dstX = (targetWidth - src.width()) / 2;
        int dstY = (targetHeight - src.height()) / 2;
        Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
        c.drawBitmap(source, src, dst, null);
        source.recycle();
        return b2;
    }

    Bitmap b1 = scaleTo(scaler, source, targetWidth, targetHeight, false);

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);
    b1.recycle();

    return b2;
}

From source file:Main.java

private static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, int options) {
    boolean scaleUp = (options & OPTIONS_SCALE_UP) != 0;
    boolean recycle = (options & OPTIONS_RECYCLE_INPUT) != 0;

    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
        Bitmap b2 = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);
        Canvas c = new Canvas(b2);

        int deltaXHalf = Math.max(0, deltaX / 2);
        int deltaYHalf = Math.max(0, deltaY / 2);
        Rect src = new Rect(deltaXHalf, deltaYHalf, deltaXHalf + Math.min(targetWidth, source.getWidth()),
                deltaYHalf + Math.min(targetHeight, source.getHeight()));
        int dstX = (targetWidth - src.width()) / 2;
        int dstY = (targetHeight - src.height()) / 2;
        Rect dst = new Rect(dstX, dstY, targetWidth - dstX, targetHeight - dstY);
        c.drawBitmap(source, src, dst, null);
        if (recycle)
            source.recycle();//from w  ww .j a v a  2s .  c  om
        return b2;
    }

    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();
    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

    float scale = bitmapAspect > viewAspect ? targetHeight / bitmapHeightF : targetWidth / bitmapWidthF;
    if (scale < .9F || scale > 1F)
        scaler.setScale(scale, scale);
    else
        scaler = null;

    Bitmap b1;
    if (scaler != null)
        b1 = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), scaler, true);
    else
        b1 = source;

    if (recycle && b1 != source)
        source.recycle();

    int dx1 = Math.max(0, b1.getWidth() - targetWidth);
    int dy1 = Math.max(0, b1.getHeight() - targetHeight);

    Bitmap b2 = Bitmap.createBitmap(b1, dx1 / 2, dy1 / 2, targetWidth, targetHeight);

    if (b2 != b1 && (recycle || b1 != source))
        b1.recycle();

    return b2;
}

From source file:Main.java

public static Bitmap getCircularBitmap(Bitmap srcBitmap) {
    // Calculate the circular bitmap width with border
    int squareBitmapWidth = Math.min(srcBitmap.getWidth(), srcBitmap.getHeight());

    //int squareBitmapWidth = 300;

    // Initialize a new instance of Bitmap
    Bitmap dstBitmap = Bitmap.createBitmap(squareBitmapWidth, // Width
            squareBitmapWidth, // Height
            Bitmap.Config.ARGB_8888 // Config
    );/*  w w w  . java2 s  .  c o m*/

    Canvas canvas = new Canvas(dstBitmap);

    // Initialize a new Paint instance
    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect rect = new Rect(0, 0, squareBitmapWidth, squareBitmapWidth);

    RectF rectF = new RectF(rect);

    canvas.drawOval(rectF, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));

    // Calculate the left and top of copied bitmap
    float left = (squareBitmapWidth - srcBitmap.getWidth()) / 2;
    float top = (squareBitmapWidth - srcBitmap.getHeight()) / 2;

    canvas.drawBitmap(srcBitmap, left, top, paint);

    srcBitmap.recycle();

    return dstBitmap;
}

From source file:com.nextgis.maplibui.util.ControlHelper.java

public static Drawable getIconByVectorType(Context context, int geometryType, int color, int defaultIcon,
        boolean syncable) {
    int drawableId;

    switch (geometryType) {
    case GTPoint:
        drawableId = R.drawable.ic_type_point;
        break;/*  w ww . ja v a 2 s.  c  o  m*/
    case GTMultiPoint:
        drawableId = R.drawable.ic_type_multipoint;
        break;
    case GTLineString:
        drawableId = R.drawable.ic_type_line;
        break;
    case GTMultiLineString:
        drawableId = R.drawable.ic_type_multiline;
        break;
    case GTPolygon:
        drawableId = R.drawable.ic_type_polygon;
        break;
    case GTMultiPolygon:
        drawableId = R.drawable.ic_type_multipolygon;
        break;
    default:
        return ContextCompat.getDrawable(context, defaultIcon);
    }

    BitmapDrawable icon = (BitmapDrawable) ContextCompat.getDrawable(context, drawableId);
    if (icon != null) {
        Bitmap src = icon.getBitmap();
        Bitmap bitmap = Bitmap.createBitmap(src.getWidth(), src.getHeight(), src.getConfig());
        Canvas canvas = new Canvas(bitmap);

        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        ColorFilter filter = new PorterDuffColorFilter(color, PorterDuff.Mode.SRC_ATOP);
        paint.setColorFilter(filter);
        canvas.drawBitmap(src, 0, 0, paint);

        if (syncable) {
            int syncIconId = isDarkTheme(context) ? R.drawable.ic_action_refresh_dark
                    : R.drawable.ic_action_refresh_light;
            ;
            src = BitmapFactory.decodeResource(context.getResources(), syncIconId);
            src = Bitmap.createScaledBitmap(src, bitmap.getWidth() / 2, bitmap.getWidth() / 2, true);
            canvas.drawBitmap(src, bitmap.getWidth() - bitmap.getWidth() / 2,
                    bitmap.getWidth() - bitmap.getWidth() / 2, new Paint());
        }

        icon = new BitmapDrawable(context.getResources(), bitmap);
    }

    return icon;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, final float roundPx) {
    if (bitmap == null)
        return null;
    try {//from www.  j av a2 s  .  c o  m
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(output);
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()));
        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(Color.BLACK);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        final Rect src = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        canvas.drawBitmap(bitmap, src, rect, paint);
        return output;
    } catch (Exception e) {
        return bitmap;
    }
}