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 getRoundedCornerBitmap(Bitmap bitmap) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    float roundPx = w / 2;
    Bitmap output = Bitmap.createBitmap(w, h, 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, w, h);
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);/*from w  w w . j  ava 2s  .c  o  m*/
    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);
    return output;
}

From source file:Main.java

public static Bitmap punchAHoleInABitmap(Context context, Bitmap foreground, float x1, float y1) {
    Bitmap bitmap = Bitmap.createBitmap(foreground.getWidth(), foreground.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();
    canvas.drawBitmap(foreground, 0, 0, paint);
    paint.setAntiAlias(true);//from w  ww .  j  a v  a2s.  c  o  m
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
    float radius = (float) (getScreenSize(context).x * .06);
    canvas.drawCircle(x1, y1 - 450, radius, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap convertDrawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable) {
        return ((BitmapDrawable) drawable).getBitmap();
    }//from  www .j  a v a  2 s  .  c  om

    Bitmap 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 GetBitmapClippedCircle(Bitmap bitmap) {

    final int width = bitmap.getWidth();
    final int height = bitmap.getHeight();
    final Bitmap outputBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    final Path path = new Path();
    path.addCircle((float) (width / 2), (float) (height / 2), (float) Math.min(width, (height / 2)),
            Path.Direction.CCW);/*from w  w w  .j a va 2s.  c  o  m*/

    final Canvas canvas = new Canvas(outputBitmap);
    canvas.clipPath(path);
    canvas.drawBitmap(bitmap, 0, 0, null);
    return outputBitmap;
}

From source file:Main.java

public static Bitmap duplicateBitmap(Bitmap bmpSrc, int width, int height) {
    if (null == bmpSrc) {
        return null;
    }//from   w  w  w. j av a 2 s. c  o  m

    int bmpSrcWidth = bmpSrc.getWidth();
    int bmpSrcHeight = bmpSrc.getHeight();

    Bitmap bmpDest = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    if (null != bmpDest) {
        Canvas canvas = new Canvas(bmpDest);
        Rect viewRect = new Rect();
        final Rect rect = new Rect(0, 0, bmpSrcWidth, bmpSrcHeight);
        if (bmpSrcWidth <= width && bmpSrcHeight <= height) {
            viewRect.set(rect);
        } else if (bmpSrcHeight > height && bmpSrcWidth <= width) {
            viewRect.set(0, 0, bmpSrcWidth, height);
        } else if (bmpSrcHeight <= height && bmpSrcWidth > width) {
            viewRect.set(0, 0, width, bmpSrcWidth);
        } else if (bmpSrcHeight > height && bmpSrcWidth > width) {
            viewRect.set(0, 0, width, height);
        }
        canvas.drawBitmap(bmpSrc, rect, viewRect, null);
    }

    return bmpDest;
}

From source file:Main.java

public static Bitmap rotateDrawable(Context context, @DrawableRes int resId, int angle) {
    Bitmap bmpOriginal = BitmapFactory.decodeResource(context.getResources(), resId);
    Bitmap bmResult = Bitmap.createBitmap(bmpOriginal.getWidth(), bmpOriginal.getHeight(),
            Bitmap.Config.ARGB_8888);/*from w ww  .j a v  a2s. c  o m*/
    Canvas tempCanvas = new Canvas(bmResult);
    tempCanvas.rotate(angle - 90, bmpOriginal.getWidth() / 2, bmpOriginal.getHeight() / 2);
    tempCanvas.drawBitmap(bmpOriginal, 0, 0, null);
    return bmResult;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable instanceof BitmapDrawable)
        return ((BitmapDrawable) drawable).getBitmap();
    else {//ww  w. j  ava  2  s  .  co  m
        Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(bitmap);
        drawable.draw(canvas);
        return bitmap;
    }
}

From source file:Main.java

public static Bitmap getRoundBitmap(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap out = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(out);
    Paint paint = new Paint();
    paint.setColor(Color.WHITE);//ww  w.  jav a  2  s .  c o m
    paint.setAntiAlias(true);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    Rect rect = new Rect(0, 0, width, height);
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return out;
}

From source file:Main.java

/**
 * Adds the white border to the specifed bitmap image using specified border size.
 * /*from  w w w  .  j a v a  2 s . c om*/
 * @param bmp to add border
 * @param borderSize
 * @return bitmap image with border.
 */
public static Bitmap addWhiteBorder(Bitmap bmp, int borderSize, int imgSize) {

    Bitmap bmpWithBorder = Bitmap.createBitmap(bmp.getWidth() + borderSize * 2,
            bmp.getHeight() + borderSize * 2, bmp.getConfig());
    Canvas canvas = new Canvas(bmpWithBorder);
    canvas.drawColor(Color.WHITE);
    canvas.drawBitmap(bmp, borderSize, borderSize, null);

    return bmpWithBorder;
}

From source file:Main.java

public static Bitmap roundCorners(Bitmap bitmap, float radius) {
    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from w  ww.j a  v  a 2s .  c  o m*/

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
    Canvas canvas = new Canvas(output);

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

    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, radius, radius, paint);

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

    return output;
}