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 getScaledCircleCroppedBitmap(Bitmap bitmap, int destSize) {
    if (bitmap == null)
        return null;
    Bitmap output = Bitmap.createBitmap(destSize, destSize, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int srcSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
    final int srcX = (bitmap.getWidth() - srcSize) / 2;
    final int srcY = (bitmap.getHeight() - srcSize) / 2;
    final Rect srcRect = new Rect(srcX, srcY, srcX + srcSize, srcY + srcSize);
    final Rect destRect = new Rect(0, 0, destSize, destSize);
    final int color = 0xff424242;
    final Paint paint = new Paint();

    paint.setAntiAlias(true);//ww w  . j  a va2 s.c om
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(destSize / 2, destSize / 2, destSize / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, srcRect, destRect, paint);
    return output;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888 : Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);//from  www . j a v a  2s . c o  m
    return bitmap;
}

From source file:Main.java

public static Bitmap drawTextToBitmap(Bitmap bitmap, String gText) {
    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  o m*/
    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) (21)); //* scale));
    // text shadow
    paint.setShadowLayer(2f, 1f, 1f, Color.WHITE);
    int x = bitmap.getWidth() - 150;//bounds.width()) - 150;
    int y = bitmap.getHeight() - 27;//bounds.height()) - 30;
    canvas.drawRect(x, y, x + 150, y + 27, paint);
    canvas.drawText(gText, x, y + 20, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    int w = drawable.getIntrinsicWidth();
    int h = drawable.getIntrinsicHeight();

    Bitmap.Config config = drawable.getOpacity() != PixelFormat.OPAQUE ? Bitmap.Config.ARGB_8888
            : Bitmap.Config.RGB_565;//from w  w  w.ja  v a 2s .  c o  m
    Bitmap bitmap = Bitmap.createBitmap(w, h, config);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, w, h);
    drawable.draw(canvas);
    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap srcBitmap, float radius) {
    Bitmap resultBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);//ww w. j  av  a2 s  .  c  om
    Canvas canvas = new Canvas(resultBitmap);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(0xBDBDBE);
    canvas.drawRoundRect(rectF, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(srcBitmap, rect, rect, paint);
    return resultBitmap;
}

From source file:Main.java

public static Bitmap getCircleBitmap(Bitmap bitmap) {
    final Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);

    final int color = Color.WHITE;
    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);/*from   w  w  w .j  a v  a  2  s  . co  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawOval(rectF, paint);

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

    bitmap.recycle();

    return output;
}

From source file:Main.java

public static Bitmap int2Icon(Context context, int i) {
    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 0x7f020047);
    Bitmap bitmap1 = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            android.graphics.Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap1);
    Paint paint = new Paint();
    paint.setDither(true);// w w  w.ja va2 s. c  om
    paint.setFilterBitmap(true);
    canvas.drawBitmap(bitmap, 0.0F, 0.0F, paint);
    bitmap.recycle();
    Paint paint1 = new Paint(257);
    paint1.setColor(-1);
    paint1.setTypeface(Typeface.DEFAULT_BOLD);
    paint1.setTextAlign(android.graphics.Paint.Align.CENTER);
    canvas.drawText(String.valueOf(i), bitmap.getWidth() / 2, 3 + bitmap.getHeight() / 2, paint1);
    return bitmap1;
}

From source file:Main.java

public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 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);/*  w ww .j  a v a  2s  .  c o m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

public static Bitmap roundCorner(Bitmap src, float round) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(result);
    canvas.drawARGB(0, 0, 0, 0);/*from  w  w  w  .  j av  a2 s .c  o m*/

    final Paint paint = new Paint();
    paint.setAntiAlias(true);
    paint.setColor(Color.BLACK);

    final Rect rect = new Rect(0, 0, width, height);
    final RectF rectF = new RectF(rect);

    canvas.drawRoundRect(rectF, round, round, paint);

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

    return result;
}

From source file:Main.java

public static Bitmap circleCrop(Bitmap bitmap) {
    int size = Math.min(bitmap.getWidth(), bitmap.getHeight());

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

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, size, size);

    paint.setAntiAlias(true);//w  ww  .  j a v  a 2 s.com
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
    canvas.drawCircle(size / 2, size / 2, size / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    // Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false);
    // return _bmp;
    return output;
}