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

public static Bitmap getColorPreviewBitmap(final Context context, final int color, final boolean border) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectangleSize = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectangleSize);
    final int numRectanglesVertical = (int) Math.ceil(height / rectangleSize);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectangleSize;/*from   w w  w.j av a  2 s  . c o  m*/
            r.left = j * rectangleSize;
            r.bottom = r.top + rectangleSize;
            r.right = r.left + rectangleSize;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    if (border) {
        final Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(1f * density);
        final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0,
                height, width, height };
        canvas.drawLines(points, paint);
    }
    return bm;
}

From source file:Main.java

/**
 * Save jpeg image with background color
 * @param strFileName Save file path/*from w  w w .  ja  va  2 s .c  o  m*/
 * @param bitmap Input bitmap
 * @param nQuality Jpeg quality for saving
 * @param nBackgroundColor background color
 * @return whether success or not
 */
public static boolean saveBitmapJPEGWithBackgroundColor(String strFileName, Bitmap bitmap, int nQuality,
        int nBackgroundColor) {
    boolean bSuccess1 = false;
    boolean bSuccess2 = false;
    boolean bSuccess3;
    File saveFile = new File(strFileName);

    if (saveFile.exists()) {
        if (!saveFile.delete())
            return false;
    }

    int nA = (nBackgroundColor >> 24) & 0xff;

    // If Background color alpha is 0, Background color substitutes as white
    if (nA == 0)
        nBackgroundColor = 0xFFFFFFFF;

    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(newBitmap);
    canvas.drawColor(nBackgroundColor);
    canvas.drawBitmap(newBitmap, rect, rect, new Paint());
    canvas.drawBitmap(bitmap, rect, rect, new Paint());

    // Quality limitation min/max
    if (nQuality < 10)
        nQuality = 10;
    else if (nQuality > 100)
        nQuality = 100;

    OutputStream out = null;

    try {
        bSuccess1 = saveFile.createNewFile();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        out = new FileOutputStream(saveFile);
        bSuccess2 = newBitmap.compress(CompressFormat.JPEG, nQuality, out);
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        if (out != null) {
            out.flush();
            out.close();
            bSuccess3 = true;
        } else
            bSuccess3 = false;

    } catch (IOException e) {
        e.printStackTrace();
        bSuccess3 = false;
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    return (bSuccess1 && bSuccess2 && bSuccess3);
}

From source file:Main.java

public static Bitmap getColorPreviewBitmap(final Context context, final int color, final boolean border) {
    if (context == null)
        return null;
    final float density = context.getResources().getDisplayMetrics().density;
    final int width = (int) (32 * density), height = (int) (32 * density);

    final Bitmap bm = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas canvas = new Canvas(bm);

    final int rectrangleSize = (int) (density * 5);
    final int numRectanglesHorizontal = (int) Math.ceil(width / rectrangleSize);
    final int numRectanglesVertical = (int) Math.ceil(height / rectrangleSize);
    final Rect r = new Rect();
    boolean verticalStartWhite = true;
    for (int i = 0; i <= numRectanglesVertical; i++) {

        boolean isWhite = verticalStartWhite;
        for (int j = 0; j <= numRectanglesHorizontal; j++) {

            r.top = i * rectrangleSize;/*from   w w w  .j av a  2  s .c  o  m*/
            r.left = j * rectrangleSize;
            r.bottom = r.top + rectrangleSize;
            r.right = r.left + rectrangleSize;
            final Paint paint = new Paint();
            paint.setColor(isWhite ? Color.WHITE : Color.GRAY);

            canvas.drawRect(r, paint);

            isWhite = !isWhite;
        }

        verticalStartWhite = !verticalStartWhite;

    }
    canvas.drawColor(color);
    if (border) {
        final Paint paint = new Paint();
        paint.setColor(Color.WHITE);
        paint.setStrokeWidth(1f * density);
        final float[] points = new float[] { 0, 0, width, 0, 0, 0, 0, height, width, 0, width, height, 0,
                height, width, height };
        canvas.drawLines(points, paint);
    }
    return bm;
}

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 . j a v a  2 s.  c om
    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

public static Drawable rotateBitmap(Bitmap inputBitmap) {
    Matrix matrix = new Matrix();
    matrix.setRotate(90, (float) inputBitmap.getWidth() / 2, (float) inputBitmap.getHeight() / 2);

    float outputX = inputBitmap.getHeight();
    float outputY = 0;

    final float[] values = new float[9];
    matrix.getValues(values);/* w  ww.ja v a 2 s . c om*/
    float x1 = values[Matrix.MTRANS_X];
    float y1 = values[Matrix.MTRANS_Y];
    matrix.postTranslate(outputX - x1, outputY - y1);
    Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap.getHeight(), inputBitmap.getWidth(),
            Bitmap.Config.ARGB_8888);
    Paint paint = new Paint();
    Canvas canvas = new Canvas(outputBitmap);
    canvas.drawBitmap(inputBitmap, matrix, paint);
    return new BitmapDrawable(null, outputBitmap);
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    Bitmap bitmap = null;/* ww  w  .ja  v a2s  .  c  om*/

    if (drawable instanceof BitmapDrawable) {
        BitmapDrawable bitmapDrawable = (BitmapDrawable) drawable;
        if (bitmapDrawable.getBitmap() != null) {
            return bitmapDrawable.getBitmap();
        }
    }

    if (drawable.getIntrinsicWidth() <= 0 || drawable.getIntrinsicHeight() <= 0) {
        bitmap = Bitmap.createBitmap(1, 1, Bitmap.Config.ARGB_8888); // Single color bitmap will be created of 1x1 pixel
    } else {
        bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
    }

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
    Bitmap scaledSrcBmp;//w  ww.  jav a  2  s.c om
    int diameter = radius * 2;
    int bmpWidth = bmp.getWidth();
    int bmpHeight = bmp.getHeight();
    int squareWidth = 0, squareHeight = 0;
    int x = 0, y = 0;
    Bitmap squareBitmap;
    if (bmpHeight > bmpWidth) {
        squareWidth = squareHeight = bmpWidth;
        x = 0;
        y = (bmpHeight - bmpWidth) / 2;
        squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
    } else if (bmpHeight < bmpWidth) {
        squareWidth = squareHeight = bmpHeight;
        x = (bmpWidth - bmpHeight) / 2;
        y = 0;
        squareBitmap = Bitmap.createBitmap(bmp, x, y, squareWidth, squareHeight);
    } else {
        squareBitmap = bmp;
    }

    if (squareBitmap.getWidth() != diameter || squareBitmap.getHeight() != diameter) {
        scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true);

    } else {
        scaledSrcBmp = squareBitmap;
    }
    Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());

    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(scaledSrcBmp.getWidth() / 2, scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2,
            paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
        int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
        Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);/*from w w  w  .  j  a  va  2s.c o m*/
        Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
        canvas.drawBitmap(bitmap, 0, 0, paint);
        if (recycle)
            bitmap.recycle();
        return target;
    } else {
        int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
        Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
        canvas.scale(scale, scale);
        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 scaleCenterCrop(Bitmap source, int newHeight, int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();

    // Compute the scaling factors to fit the new height and width, respectively.
    // To cover the final image, the final scaling will be the bigger
    // of these two.
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale, yScale);

    // Now get the size of the source bitmap when scaled
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;

    // Let's find out the upper left coordinates if the scaled bitmap
    // should be centered in the new size give by the parameters
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;

    // The target rectangle for the new, scaled version of the source bitmap will now
    // be//from ww  w. j  av a2 s. com
    RectF targetRect = new RectF(left, top, left + scaledWidth, top + scaledHeight);

    // Finally, we create a new bitmap of the specified size and draw our new,
    // scaled bitmap onto it.
    Bitmap dest = Bitmap.createBitmap(newWidth, newHeight, CONFIG);
    Canvas canvas = new Canvas(dest);
    Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setFilterBitmap(true);
    paint.setDither(true);
    canvas.drawBitmap(source, null, targetRect, paint);

    return dest;
}

From source file:Main.java

public static Bitmap getBitmap(int background, Bitmap contour, float alpha) {
    Bitmap bitmap = Bitmap.createBitmap(contour.getWidth(), contour.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawColor(background);/*from   w ww. j  a va  2s  . com*/
    return getBitmap(bitmap, contour, alpha);
}