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 toRoundedCornerBitmap(Bitmap bitmap, int radius) {
    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());
    final RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);//from w w w.j a  v  a2s .  co  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, radius, radius, 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 getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null)
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);/* w  w w .  j  av a 2  s .c o  m*/
    else
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}

From source file:Main.java

public static Bitmap createBitmapFromView(View view) {
    // Clear any focus from the view first to remove any cursor
    view.clearFocus();//from  w ww.  ja  va 2  s . com
    Bitmap drawingBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    drawingBitmap.eraseColor(Color.TRANSPARENT);
    Canvas canvas = new Canvas(drawingBitmap);
    view.draw(canvas);
    return drawingBitmap;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

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

    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  2s .c o m*/
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, paint);

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

From source file:Main.java

private static Bitmap decorate(Bitmap bitmap, char label, int color, boolean glossy) {

    Canvas canvas = new Canvas(bitmap);

    canvas.drawColor(color);//  ww w .  j  av a2s.  co m

    String labelString = String.valueOf(label);
    Paint textPaint = getTextPaint(bitmap.getHeight() * 0.75f);
    float cx = bitmap.getWidth() / 2;
    float cy = bitmap.getHeight() / 2;
    float oy = 0;

    synchronized (junkRectangle) {
        textPaint.getTextBounds(labelString, 0, 1, junkRectangle);
        oy = (junkRectangle.top + junkRectangle.bottom) / 2;
    }

    canvas.drawText(labelString, cx, cy - oy, textPaint);

    if (glossy) {
        canvas.drawPath(gloss(bitmap.getWidth()), getGlossPaint());
    }

    return bitmap;

}

From source file:Main.java

public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();//from   ww  w  .ja  v a 2 s.  c o  m
    width = bmpOriginal.getWidth();
    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap toSepia(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();//from  w  w  w.  jav a2  s.co  m
    width = bmpOriginal.getWidth();

    Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas c = new Canvas(bmpGrayscale);
    Paint paint = new Paint();

    ColorMatrix grMatrix = new ColorMatrix();
    grMatrix.setSaturation(0);

    ColorMatrix scMatrix = new ColorMatrix();
    scMatrix.setScale(1f, .85f, .72f, 1.0f);
    grMatrix.setConcat(scMatrix, grMatrix);

    ColorMatrixColorFilter f = new ColorMatrixColorFilter(grMatrix);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

public static Bitmap toOvalBitmap(Bitmap bitmap) {
    Bitmap output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    paint.setAntiAlias(true);//from w w w  .  j a v  a  2  s.  c om
    Rect rect = new Rect(0, 0, bitmap.getHeight(), bitmap.getHeight());
    RectF rectF = new RectF(rect);

    canvas.drawOval(rectF, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rectF, paint);
    return output;
}

From source file:Main.java

public static Bitmap getMosaic(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int radius = 10;

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

    int horCount = (int) Math.ceil(width / (float) radius);
    int verCount = (int) Math.ceil(height / (float) radius);

    Paint paint = new Paint();
    paint.setAntiAlias(true);/*from w  w  w  . ja v  a2  s.  c  om*/

    for (int horIndex = 0; horIndex < horCount; ++horIndex) {
        for (int verIndex = 0; verIndex < verCount; ++verIndex) {
            int l = radius * horIndex;
            int t = radius * verIndex;
            int r = l + radius;
            if (r > width) {
                r = width;
            }
            int b = t + radius;
            if (b > height) {
                b = height;
            }
            int color = bitmap.getPixel(l, t);
            Rect rect = new Rect(l, t, r, b);
            paint.setColor(color);
            canvas.drawRect(rect, paint);
        }
    }
    canvas.save();

    return mosaicBitmap;
}

From source file:Main.java

public static Bitmap greyScale(Bitmap source) {
    int width = source.getWidth();
    int height = source.getHeight();

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

    Canvas canvas = new Canvas(bitmap);
    ColorMatrix saturation = new ColorMatrix();
    saturation.setSaturation(0f);/* w w  w. jav a 2 s . c  o m*/
    Paint paint = new Paint();
    paint.setColorFilter(new ColorMatrixColorFilter(saturation));
    canvas.drawBitmap(source, 0, 0, paint);
    source.recycle();

    if (source != bitmap) {
        source.recycle();
    }

    return bitmap;
}