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 getCircleBitmap(Context context, Bitmap src, float radius) {
    radius = dipTopx(context, radius);//from   w  w w .ja  v a2  s.  c  o m
    int w = src.getWidth();
    int h = src.getHeight();
    int canvasW = Math.round(radius * 2);
    Bitmap bitmap = Bitmap.createBitmap(canvasW, canvasW, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Path path = new Path();
    path.addCircle(radius, radius, radius, Path.Direction.CW);
    canvas.clipPath(path);

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect srcRect = new Rect(0, 0, w, h);
    Rect dstRect = new Rect(0, 0, canvasW, canvasW);

    canvas.drawBitmap(src, srcRect, dstRect, paint);

    return bitmap;
}

From source file:Main.java

static Bitmap generatorContactCountIcon(Context context, Bitmap icon) {
    int iconSize = (int) context.getResources().getDimension(android.R.dimen.app_icon_size);
    Bitmap contactIcon = Bitmap.createBitmap(iconSize, iconSize, Config.ARGB_8888);
    Canvas canvas = new Canvas(contactIcon);

    Paint iconPaint = new Paint();
    iconPaint.setDither(true);//w ww.j a  v  a  2 s  .co m
    iconPaint.setFilterBitmap(true);
    Rect src = new Rect(0, 0, icon.getWidth(), icon.getHeight());
    Rect dst = new Rect(0, 0, iconSize, iconSize);
    canvas.drawBitmap(icon, src, dst, iconPaint);
    int contacyCount = 11;
    Paint countPaint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.DEV_KERN_TEXT_FLAG);
    countPaint.setColor(Color.RED);
    countPaint.setTextSize(20f);
    countPaint.setTypeface(Typeface.DEFAULT_BOLD);
    canvas.drawText(String.valueOf(contacyCount), iconSize - 18, 25, countPaint);
    return contactIcon;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Context context, Bitmap input, int pixels, int w, int h,
        boolean squareTL, boolean squareTR, boolean squareBL, boolean squareBR) {

    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    final float densityMultiplier = context.getResources().getDisplayMetrics().density;

    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, w, h);
    final RectF rectF = new RectF(rect);

    //make sure that our rounded corner is scaled appropriately
    final float roundPx = pixels * densityMultiplier;

    paint.setAntiAlias(true);/*from   ww w. j  a  v  a2s.c  o  m*/
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

    //draw rectangles over the corners we want to be square
    if (squareTL) {
        canvas.drawRect(0, 0, w / 2, h / 2, paint);
    }
    if (squareTR) {
        canvas.drawRect(w / 2, 0, w, h / 2, paint);
    }
    if (squareBL) {
        canvas.drawRect(0, h / 2, w / 2, h, paint);
    }
    if (squareBR) {
        canvas.drawRect(w / 2, h / 2, w, h, paint);
    }

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(input, 0, 0, paint);

    return output;
}

From source file:Main.java

public static Bitmap getRoundedImage(Bitmap bitmap) {
    Bitmap circleBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);

    BitmapShader shader = new BitmapShader(bitmap, TileMode.CLAMP, TileMode.CLAMP);
    Paint paint = new Paint();
    paint.setShader(shader);//ww  w .  ja  va  2 s  .  c o  m

    Canvas c = new Canvas(circleBitmap);
    c.drawCircle(bitmap.getWidth() / 2, bitmap.getHeight() / 2, bitmap.getWidth() / 2, paint);

    return circleBitmap;
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable) {
    if (drawable == null || drawable.getIntrinsicWidth() < 0 || drawable.getIntrinsicHeight() < 0)
        return null;
    try {/* ww  w .j a v  a  2  s. c  o m*/
        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);
        return bitmap;
    } catch (OutOfMemoryError e) {
        return null;
    }

}

From source file:Main.java

/**
 * convert Bitmap to round corner//from  w w  w .  j  av  a2 s.  c  o m
 *
 * @param bitmap
 * @return
 */
public static Bitmap toRoundCorner(Bitmap bitmap) {
    int height = bitmap.getHeight();
    int width = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(output);

    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(0xff424242);
    //paint.setColor(Color.TRANSPARENT);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:Main.java

/**
 * Create a bitmap from a background and a foreground bitmap
 *
 * @param background The bitmap placed in the background
 * @param foreground The bitmap placed in the foreground
 * @return the merged bitmap//from www . ja  v a 2s  .c  o  m
 */
public static Bitmap mergeBitmap(@NonNull Bitmap background, @NonNull Bitmap foreground) {
    Bitmap result = Bitmap.createBitmap(background.getWidth(), background.getHeight(), background.getConfig());
    Canvas canvas = new Canvas(result);
    canvas.drawBitmap(background, 0f, 0f, null);
    canvas.drawBitmap(foreground, 10, 10, null);
    return result;
}

From source file:Main.java

public static Bitmap setBrightness(Bitmap srcBitmap, int brightness) {
    Bitmap bitmap = Bitmap.createBitmap(srcBitmap);
    ColorMatrix cm = new ColorMatrix(new float[] { 1, 0, 0, 0, brightness, 0, 1, 0, 0, brightness, 0, 0, 1, 0,
            brightness, 0, 0, 0, 1, 0 });
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(cm));
    Canvas canvas = new Canvas(bitmap);
    canvas.drawBitmap(srcBitmap, 0, 0, paint);
    return bitmap;
}

From source file:Main.java

public static Bitmap getBitmap(Drawable drawable, boolean scaleBitmap, int width, int height) {
    Bitmap bitmap;//from w ww  . ja  v  a 2s .co m
    if (drawable instanceof BitmapDrawable) {
        bitmap = ((BitmapDrawable) drawable).getBitmap();
    } 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);
    }
    if (scaleBitmap) {
        bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
    }
    return bitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    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  . jav  a2s .co  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;
}