Example usage for android.graphics Canvas Canvas

List of usage examples for android.graphics Canvas Canvas

Introduction

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

Prototype

@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 115609023)
public Canvas(long nativeCanvas) 

Source Link

Usage

From source file:Main.java

/**
 * This method takes a square bitmap and clips it into a circle
 *
 * @param bitmap : the image to clip// w  ww .  j a  v a2  s  .  c  o  m
 * @return the clipped bitmap
 */
public static Bitmap getCroppedBitmap(Bitmap bitmap) {
    Bitmap 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());

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    //return _bmp;
    return output;
}

From source file:Main.java

public static Bitmap getBitmap(int resId, Context ctx) {
    int mLargeIconWidth = (int) ctx.getResources().getDimension(android.R.dimen.notification_large_icon_width);
    int mLargeIconHeight = (int) ctx.getResources()
            .getDimension(android.R.dimen.notification_large_icon_height);
    Drawable d = ctx.getResources().getDrawable(resId);
    Bitmap b = Bitmap.createBitmap(mLargeIconWidth, mLargeIconHeight, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    d.setBounds(0, 0, mLargeIconWidth, mLargeIconHeight);
    d.draw(c);/*from w ww  .  j  a v a 2 s .c  o  m*/
    return b;
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle) {
    int width = Math.round(bitmap.getWidth() * scale);
    int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);/*from  ww  w . j ava2s  .  com*/
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static Bitmap createIdenticon(byte[] hash, int background) {
    Bitmap identicon = Bitmap.createBitmap(5, 5, Bitmap.Config.ARGB_8888);

    float[] hsv = { Integer.valueOf(hash[3] + 128).floatValue() * 360.0f / 255.0f, 0.8f, 0.8f };
    int foreground = Color.HSVToColor(hsv);

    for (int x = 0; x < 5; x++) {
        //Enforce horizontal symmetry
        int i = x < 3 ? x : 4 - x;
        for (int y = 0; y < 5; y++) {
            int pixelColor;
            //toggle pixels based on bit being on/off
            if ((hash[i] >> y & 1) == 1)
                pixelColor = foreground;
            else/*from w  ww  . j ava  2s.  c om*/
                pixelColor = background;
            identicon.setPixel(x, y, pixelColor);
        }
    }

    Bitmap bmpWithBorder = Bitmap.createBitmap(48, 48, identicon.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(background);
    identicon = Bitmap.createScaledBitmap(identicon, 46, 46, false);
    canvas.drawBitmap(identicon, 1, 1, null);

    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap adjustPhotoRotation(Bitmap bm, final int orientationDegree) {
    Matrix m = new Matrix();
    m.setRotate(orientationDegree, (float) bm.getWidth() / 2, (float) bm.getHeight() / 2);
    float targetX, targetY;
    if (orientationDegree == 90) {
        targetX = bm.getHeight();/*  w  ww  .  j ava2  s. c  om*/
        targetY = 0;
    } else {
        targetX = bm.getHeight();
        targetY = bm.getWidth();
    }
    final float[] values = new float[9];
    m.getValues(values);
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    m.postTranslate(targetX - x1, targetY - y1);
    Bitmap bm1 = Bitmap.createBitmap(bm.getHeight(), bm.getWidth(), Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(bm1);
    canvas.drawBitmap(bm, m, paint);
    bm.recycle();
    bm = null;
    return bm1;
}

From source file:Main.java

public static Bitmap combineImagesToSameSize(Bitmap bgd, Bitmap fg) {
    Bitmap bmp;/*from  ww w  . j a v  a  2  s.c o  m*/

    int width = bgd.getWidth() < fg.getWidth() ? bgd.getWidth() : fg.getWidth();
    int height = bgd.getHeight() < fg.getHeight() ? bgd.getHeight() : fg.getHeight();

    if (fg.getWidth() != width && fg.getHeight() != height) {
        fg = zoom(fg, width, height);
    }
    if (bgd.getWidth() != width && bgd.getHeight() != height) {
        bgd = zoom(bgd, width, height);
    }

    bmp = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_ATOP));

    Canvas canvas = new Canvas(bmp);
    canvas.drawBitmap(bgd, 0, 0, null);
    canvas.drawBitmap(fg, 0, 0, paint);

    return bmp;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels) {
    if (bitmap == null)
        return null;
    Bitmap output;/*from  w  ww .  java  2  s.  c  o  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 drawableToBitmap(Drawable drawable) {
    if (drawable != null) {
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bitmap);
        drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight());
        drawable.draw(canvas);/*  w ww  .  j a  va2s  .c  o m*/
        return bitmap;
    } else {
        return null;
    }
}

From source file:Main.java

public static Bitmap transform(Matrix scaler, Bitmap source, int targetWidth, int targetHeight, boolean scaleUp,
        boolean recycle) {
    int deltaX = source.getWidth() - targetWidth;
    int deltaY = source.getHeight() - targetHeight;
    if (!scaleUp && (deltaX < 0 || deltaY < 0)) {
        /*//from   w  ww  .  jav  a 2s.c o  m
         * In this case the bitmap is smaller, at least in one dimension,
         * than the target.  Transform it by placing as much of the image
         * as possible into the target and leaving the top/bottom or
         * left/right (or both) black.
         */
        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();
        }
        return b2;
    }
    float bitmapWidthF = source.getWidth();
    float bitmapHeightF = source.getHeight();

    float bitmapAspect = bitmapWidthF / bitmapHeightF;
    float viewAspect = (float) targetWidth / targetHeight;

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

    Bitmap b1;
    if (scaler != null) {
        // this is used for minithumb and crop, so we want to filter here.
        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) {
        if (recycle || b1 != source) {
            b1.recycle();
        }
    }

    return b2;
}

From source file:Main.java

/**
 * Method to combine images side by side.
 *
 * @param leftBmp  The left Bitmap.//from   w w  w  .ja  v  a2s.co  m
 * @param rightBmp The right Bitmap.
 * @return A Bitmap with left and right bitmap are glued side by side.
 */
public static Bitmap combineImagesSideBySide(Bitmap leftBmp, Bitmap rightBmp) {
    int width;
    int height = leftBmp.getHeight();

    if (leftBmp.getWidth() > rightBmp.getWidth()) {
        width = leftBmp.getWidth() + rightBmp.getWidth();
    } else {
        width = rightBmp.getWidth() + rightBmp.getWidth();
    }

    Bitmap cs = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas comboImage = new Canvas(cs);
    comboImage.drawBitmap(leftBmp, 0f, 0f, null);
    comboImage.drawBitmap(rightBmp, leftBmp.getWidth(), 0f, null);

    return cs;
}