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

/**
 * @brief      Converting picture to bitmap array.
 * //w w w  . j av  a 2s  . c  om
 * @author     daiping.zhao
 * @param      bm   [IN]  bitmap object
 * 
 * @return     Return converted bytes array
 */
public static Bitmap pictureToBitmap(Picture picture, int width, int height) {
    Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
    Canvas canvas = new Canvas(b);

    // May need to tweak these values to determine what is the
    // best scale factor
    int picWidth = picture.getWidth();
    int picHeight = picture.getHeight();
    float scaleFactorX = 1.0f;
    float scaleFactorY = 1.0f;
    if (picWidth > 0) {
        scaleFactorX = (float) width / (float) picWidth;
    } else {
        return null;
    }

    if (picWidth > picHeight) {
        // If the device is in landscape and the page is shorter
        // than the height of the view, stretch the thumbnail to fill the
        // space.
        scaleFactorY = (float) height / (float) picHeight;
    } else {
        // In the portrait case, this looks nice.
        scaleFactorY = scaleFactorX;
    }

    canvas.scale(scaleFactorX, scaleFactorY);

    picture.draw(canvas);
    return b;
}

From source file:Main.java

public static Bitmap cropMaxVisibleBitmap(Drawable drawable, int iconSize) {
    Bitmap tmp = Bitmap.createBitmap(drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight(),
            Bitmap.Config.ARGB_8888);//from  w  w w.j  av a2s  . c o m
    Canvas canvas = new Canvas(tmp);
    drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
    drawable.draw(canvas);

    Rect crop = new Rect(tmp.getWidth(), tmp.getHeight(), -1, -1);
    for (int y = 0; y < tmp.getHeight(); y++) {
        for (int x = 0; x < tmp.getWidth(); x++) {
            int alpha = (tmp.getPixel(x, y) >> 24) & 255;
            if (alpha > 0) { // pixel is not 100% transparent
                if (x < crop.left)
                    crop.left = x;
                if (x > crop.right)
                    crop.right = x;
                if (y < crop.top)
                    crop.top = y;
                if (y > crop.bottom)
                    crop.bottom = y;
            }
        }
    }

    if (crop.width() <= 0 || crop.height() <= 0) {
        return Bitmap.createScaledBitmap(tmp, iconSize, iconSize, true);
    }

    // We want to crop a square region.
    float size = Math.max(crop.width(), crop.height());
    float xShift = (size - crop.width()) * 0.5f;
    crop.left -= Math.floor(xShift);
    crop.right += Math.ceil(xShift);

    float yShift = (size - crop.height()) * 0.5f;
    crop.top -= Math.floor(yShift);
    crop.bottom += Math.ceil(yShift);

    Bitmap finalImage = Bitmap.createBitmap(iconSize, iconSize, Bitmap.Config.ARGB_8888);
    canvas.setBitmap(finalImage);
    float scale = iconSize / size;

    canvas.scale(scale, scale);
    canvas.drawBitmap(tmp, -crop.left, -crop.top, new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG));
    canvas.setBitmap(null);
    return finalImage;
}

From source file:Main.java

/**
 * Combines multiple bitmaps together into one
 * @param aParts An array of bitmaps/* www. ja va  2s. co  m*/
 * @return Returns a bitmap image
 */
public static Bitmap combineBitmaps(Bitmap[] aParts) {
    Bitmap[] parts = aParts;
    Bitmap result = Bitmap.createBitmap(parts[0].getWidth() * 2, parts[0].getHeight() * 2,
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(result);
    Paint paint = new Paint();
    for (int i = 0; i < parts.length; i++) {
        canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
    }
    return result;
}

From source file:Main.java

public static Bitmap createPngScreenshot2(View view, int thumbnailWidth, int thumbnailHeight, int top) {
    if (view != null) {
        Bitmap mCapture;/*from w w  w. ja  v  a  2 s  . c  o m*/
        try {
            mCapture = Bitmap.createBitmap(thumbnailWidth, thumbnailHeight, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError e) {
            return null;
        }
        Canvas c = new Canvas(mCapture);
        c.drawColor(Color.BLACK);
        //            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 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  ww  .ja  v  a  2  s. co m
    canvas.drawARGB(0, 0, 0, 0);
    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 toRoundCorner(Bitmap bitmap, int pixels) {

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

    int color = 0xff424242;
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    RectF rectF = new RectF(rect);
    float roundPx = pixels;

    paint.setAntiAlias(true);/*  w w w.  ja v a  2s .  co  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);
    bitmap.recycle();
    return output;
}

From source file:Main.java

public static Bitmap getWebViewShotshatByPicture(WebView webView) {
    Picture picture = webView.capturePicture();
    int width = picture.getWidth();
    int height = picture.getHeight();
    Bitmap bw = null;//  ww w. ja v  a 2  s  .  c  o m
    if (width > 0 && height > 0) {
        bw = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565);
        Canvas canvas = new Canvas(bw);
        //picture.draw(canvas);
        canvas.drawPicture(picture);
    }
    return bw;
}

From source file:Main.java

public static Bitmap toGrey(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);// ww w  .  j  av a 2s.  c o m
    p.setColorFilter(new ColorMatrixColorFilter(cm));
    c.drawBitmap(bitmap, 0, 0, p);
    return grey;
}

From source file:Main.java

public static Bitmap setupFrame(Bitmap bitmap, int width, int color) {
    if (bitmap.getWidth() <= width * 2 || bitmap.getHeight() <= width * 2) {
        return bitmap;
    }/*from ww  w.java2  s  .c o m*/

    Bitmap bp = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);

    Canvas canvas = new Canvas(bp);
    canvas.drawBitmap(bitmap, 0, 0, new Paint());
    Paint paint = new Paint();
    paint.setColor(color);
    paint.setStrokeWidth(width);
    paint.setStyle(Style.STROKE);
    canvas.drawRect(0, 0, canvas.getWidth() - width, canvas.getHeight() - width, paint);

    bitmap.recycle();
    return bp;
}

From source file:Main.java

public static Bitmap makeRoundedCornerBitmap(Bitmap bitmap, float roundPx) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    Bitmap output = Bitmap.createBitmap(w, h, 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 ww . ja va2s  .  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;
}