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 takeScreenShot(View view) {
    if (null == view) {
        return null;
    }/* ww  w  .ja  v a2  s .c om*/

    assert view.getWidth() > 0 && view.getHeight() > 0;
    Bitmap.Config config = Bitmap.Config.ARGB_8888;

    Bitmap bitmap = null;
    try {
        bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), config);
        Canvas canvas = new Canvas(bitmap);
        view.draw(canvas);
    } catch (OutOfMemoryError e) {
        e.printStackTrace();
    }

    return bitmap;
}

From source file:Main.java

public static Bitmap convertGrayscale(final Bitmap source) {
    final int width = source.getWidth();
    final int height = source.getHeight();
    final Bitmap output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(output);
    final Paint paint = new Paint();
    final ColorMatrix matrix = new ColorMatrix();
    matrix.setSaturation(0f);/*from w  w w  .ja  v a 2s .  c o m*/
    paint.setColorFilter(new ColorMatrixColorFilter(matrix));
    canvas.drawBitmap(source, 0, 0, paint);
    return output;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(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 av  a  2 s .  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 getRoundedShape(Bitmap scaleBitmapImage, int width) {
    // TODO Auto-generated method stub
    int targetWidth = width;
    int targetHeight = width;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);
    canvas.clipPath(path);/*from   www.  j  av a  2  s.co m*/
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

public static Bitmap toBitmap(View v) {
    int w = v.getWidth();
    int h = v.getHeight();
    if (w <= 0 || h <= 0) {
        return null;
    }//w w  w .ja  va2  s. c  o  m
    Bitmap bitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    v.draw(canvas);
    return bitmap;
}

From source file:Main.java

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);/*from w  w  w .ja v  a2  s  .  com*/
    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

public static Bitmap createScreenshot3(View view, int thumbnailWidth, int thumbnailHeight, int top) {
    if (view != null) {
        Bitmap mCapture;//w w  w. ja v a2  s.c o  m
        try {
            mCapture = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.RGB_565);
        } catch (OutOfMemoryError e) {
            return null;
        }
        Canvas c = new Canvas(mCapture);
        //            final int left = view.getScrollX();
        //            final int top = view.getScrollY();
        c.translate(0, -top);
        //c.scale(0.65f, 0.65f, left, top);
        try {
            // draw webview may nullpoint
            view.draw(c);
        } catch (Exception e) {
        }
        return mCapture;
    }
    return null;
}

From source file:Main.java

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

    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);

    paint.setAntiAlias(true);//  w ww .j a v a 2s.c om

    canvas.drawARGB(0, 0, 0, 0);

    paint.setColor(color);

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

    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;

}

From source file:Main.java

public static final Bitmap grey(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap greyBitmap = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    Canvas canvas = new Canvas(greyBitmap);
    Paint paint = new Paint();
    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.setSaturation(0);/*from www. j  a  v a  2s. c  om*/
    ColorMatrixColorFilter colorMatrixFilter = new ColorMatrixColorFilter(colorMatrix);
    paint.setColorFilter(colorMatrixFilter);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return greyBitmap;
}

From source file:Main.java

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