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 view2Bitmap(View view) {
    if (view == null)
        return null;
    Bitmap ret = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(ret);
    Drawable bgDrawable = view.getBackground();
    if (bgDrawable != null) {
        bgDrawable.draw(canvas);//from w w  w .j av a 2 s. c o  m
    } else {
        canvas.drawColor(Color.WHITE);
    }
    view.draw(canvas);
    return ret;
}

From source file:Main.java

/**
 * Helper to create a bitmap to set as imageview or bg
 * @param bgcolor/*from  w  ww  .j a  va2  s  . c o m*/
 * @return bitmap
 */
public static Bitmap getBackground(int bgcolor) {
    try {
        Bitmap.Config config = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = Bitmap.createBitmap(2, 2, config);
        Canvas canvas = new Canvas(bitmap);
        canvas.drawColor(bgcolor);
        return bitmap;
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

public static Bitmap pictureDrawable2Bitmap(PictureDrawable pictureDrawable) {
    Bitmap bitmap = Bitmap.createBitmap(pictureDrawable.getIntrinsicWidth(),
            pictureDrawable.getIntrinsicHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    canvas.drawPicture(pictureDrawable.getPicture());
    return bitmap;
}

From source file:Main.java

public static Bitmap captureWebView(WebView webView) {
    Picture snapShot = webView.capturePicture();
    Bitmap bmp = Bitmap.createBitmap(snapShot.getWidth(), snapShot.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(bmp);
    snapShot.draw(canvas);//from   w w  w. j  a  v a2  s .  c o m
    return bmp;
}

From source file:Main.java

public static Bitmap toGreyBitmap(Bitmap bitmap) {
    Bitmap grey = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(grey);
    Paint p = new Paint();
    ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);//from  ww w .j a v  a2  s . co  m
    p.setColorFilter(new ColorMatrixColorFilter(cm));
    c.drawBitmap(bitmap, 0, 0, p);
    return grey;
}

From source file:Main.java

public static Bitmap drawable2Bitmap(Drawable d) {
    if (d == null) {
        return null;
    }//www  .ja  va 2 s .co  m
    Bitmap bm = Bitmap.createBitmap(d.getIntrinsicWidth(), d.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bm);
    d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
    d.draw(canvas);
    return bm;
}

From source file:Main.java

public static BitmapDrawable createDrawableFromView(Context context, View v) {
    Bitmap b = Bitmap.createBitmap(v.getLayoutParams().width, v.getLayoutParams().height,
            Bitmap.Config.ARGB_8888);//from   w ww  .  j a va2  s  .c  o m
    Canvas c = new Canvas(b);
    v.layout(v.getLeft(), v.getTop(), v.getRight(), v.getBottom());
    v.draw(c);
    return new BitmapDrawable(context.getResources(), b);
}

From source file:Main.java

public static Bitmap drawableToBitmap(Drawable drawable, int width, int height) {
    Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmap);
    drawable.setBounds(0, 0, width, height);
    drawable.draw(canvas);/*  w  w  w  .ja v a  2s . co  m*/
    return bitmap;
}

From source file:Main.java

public static Bitmap revertImage(Bitmap bm) {
    Bitmap bitmap = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);
    Paint paint = new Paint();

    float[] matrixs = new float[] { -1, 0, 0, 1, 1, 0, -1, 0, 1, 1, 0, 0, -1, 1, 1, 0, 0, 0, 1, 0 };

    ColorMatrix colorMatrix = new ColorMatrix();
    colorMatrix.set(matrixs);//  ww  w. j  a  v  a 2  s.co  m
    paint.setColorFilter(new ColorMatrixColorFilter(colorMatrix));

    canvas.drawBitmap(bm, 0, 0, paint);

    return bitmap;
}

From source file:Main.java

private static Bitmap drawToBitmap(Resources resources, int drawableResourceID, Bitmap bitmap) {
    Drawable drawable = resources.getDrawable(drawableResourceID);
    drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());
    drawable.draw(new Canvas(bitmap));
    return bitmap;
}