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, 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 2  s  .c om
    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

private static Bitmap toGrayscale(Bitmap bmpOriginal) {
    int width, height;
    height = bmpOriginal.getHeight();/*from   w ww .  j  a  v a2 s. co 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 fillet(Bitmap bitmap, int pixels) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), 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);
    final float roundPx = pixels;

    paint.setAntiAlias(true);//from   w  ww.ja  v  a 2  s.  c  om
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(Color.BLACK);
    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 getClip(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

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

    Canvas canvas = new Canvas(output);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, width, height);

    paint.setAntiAlias(true);// w  ww.java 2  s .c  o m
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawCircle(width / 2, height / 2, width / 2, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, null, rect, paint);
    return output;
}

From source file:Main.java

public static Drawable toGrey(Drawable drawable) {
    int w = drawable.getMinimumWidth();
    int h = drawable.getMinimumHeight();
    if (w <= 0 || h <= 0) {
        return drawable;
    }/*from w  ww. j  av a  2  s .c o  m*/
    Rect bounds = drawable.getBounds();
    Bitmap grey = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    drawable.setColorFilter(new ColorMatrixColorFilter(cm));
    drawable.setBounds(0, 0, w, h);
    drawable.draw(c);
    drawable.clearColorFilter();
    drawable.setBounds(bounds);
    BitmapDrawable bd = new BitmapDrawable(grey);
    bd.setBounds(0, 0, w, h);
    return bd;
}

From source file:Main.java

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

From source file:Main.java

public static Bitmap getBitmapFromDrawable(Drawable drawable) {
    int width = drawable.getIntrinsicWidth();
    int height = drawable.getIntrinsicHeight();
    Bitmap bitmap = Bitmap.createBitmap(width, height,
            drawable.getOpacity() != PixelFormat.OPAQUE ? Config.ARGB_8888 : Config.RGB_565);
    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);/* ww  w.j a va 2  s  .  co  m*/
    return bitmap;

}

From source file:Main.java

public static Bitmap getHistogram(Bitmap bmpOriginal) {

    //Scale bmpOriginal to improve performance 
    final int dstWidth = 200;
    int dstHeight = dstWidth * bmpOriginal.getHeight() / bmpOriginal.getWidth();
    Bitmap bmpScaled = Bitmap.createScaledBitmap(bmpOriginal, dstWidth, dstHeight, false);
    int[] histogramValues = new int[256];

    int[] pixels = new int[dstWidth];
    int pxBrightness;
    for (int row = 0; row < dstHeight; row++) {
        bmpScaled.getPixels(pixels, 0, dstWidth, 0, row, dstWidth, 1);
        for (int col = 0; col < dstWidth; col++) {
            pxBrightness = rgbToGray(pixels[col]);
            histogramValues[pxBrightness]++;
        }/*from   w  ww .j  av a  2 s.  c o  m*/
    }
    bmpScaled.recycle();

    int histogramMax = max(histogramValues);
    Bitmap bmpHistogram = Bitmap.createBitmap(256, histogramMax, Config.ARGB_8888);
    Canvas canvas = new Canvas(bmpHistogram);
    Paint paint = new Paint();
    paint.setColor(Color.CYAN);

    for (int i = 0; i < 256; i++)
        canvas.drawLine(i, histogramMax - histogramValues[i], i, histogramMax, paint);

    return bmpHistogram;
}

From source file:Main.java

public static Bitmap createDrawableFromView(Context context, View view) {

    Bitmap bit = null;//  w w w.ja  va 2  s  .c om
    try {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        ((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
        view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT));
        view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
        view.buildDrawingCache();
        bit = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(bit);
        view.draw(canvas);
    } catch (NullPointerException e) {
        Log.e("NullPointerException", e.toString());
        e.printStackTrace();
    }
    return bit;
}

From source file:Main.java

private static Drawable recolor(Resources res, Drawable drawable, int color) {
    if (drawable == null) {
        return null;
    }/*from www .  j a va  2s .com*/

    Bitmap outBitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(outBitmap);
    drawable.setBounds(0, 0, outBitmap.getWidth(), outBitmap.getHeight());
    drawable.setColorFilter(color, PorterDuff.Mode.SRC_IN);
    drawable.draw(canvas);
    drawable.setColorFilter(null);
    drawable.setCallback(null); // free up any references
    return new BitmapDrawable(res, outBitmap);
}