Example usage for android.graphics Canvas drawBitmap

List of usage examples for android.graphics Canvas drawBitmap

Introduction

In this page you can find the example usage for android.graphics Canvas drawBitmap.

Prototype

public void drawBitmap(@NonNull Bitmap bitmap, @Nullable Rect src, @NonNull Rect dst, @Nullable Paint paint) 

Source Link

Document

Draw the specified bitmap, scaling/translating automatically to fill the destination rectangle.

Usage

From source file:com.nextgis.mobile.map.MapBase.java

@Override
protected void onDraw(Canvas canvas) {
    if (mDisplay != null) {
        canvas.drawBitmap(mDisplay.getDisplay(true), 0, 0, null);
    } else {//from   w ww. j  a  v a  2  s  . c om
        super.onDraw(canvas);
    }
}

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);//  w ww.  j a v  a  2  s  . 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:com.concavenp.artistrymuse.fragments.BaseFragment.java

private BitmapImageViewTarget createBitmapImageViewTarget(final ImageView imageView) {

    return new BitmapImageViewTarget(imageView) {
        @Override//from   www  .j  a va  2s .com
        protected void setResource(Bitmap bitmap) {

            int bitmapWidth = bitmap.getWidth();
            int bitmapHeight = bitmap.getHeight();
            int borderWidthHalf = 5;
            int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);
            int newBitmapSquare = bitmapSquareWidth + borderWidthHalf;

            Bitmap roundedBitmap = Bitmap.createBitmap(newBitmapSquare, newBitmapSquare,
                    Bitmap.Config.ARGB_8888);

            // Initialize a new Canvas to draw empty bitmap
            Canvas canvas = new Canvas(roundedBitmap);

            // Calculation to draw bitmap at the circular bitmap center position
            int x = borderWidthHalf + bitmapSquareWidth - bitmapWidth;
            int y = borderWidthHalf + bitmapSquareWidth - bitmapHeight;

            canvas.drawBitmap(bitmap, x, y, null);

            // Initializing a new Paint instance to draw circular border
            Paint borderPaint = new Paint();
            borderPaint.setStyle(Paint.Style.STROKE);
            borderPaint.setStrokeWidth(borderWidthHalf * 2);
            borderPaint.setColor(ResourcesCompat.getColor(getResources(), R.color.myApp_accent_700, null));

            canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2, newBitmapSquare / 2, borderPaint);

            RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(),
                    roundedBitmap);
            circularBitmapDrawable.setCircular(true);
            imageView.setImageDrawable(circularBitmapDrawable);

        }
    };
}

From source file:com.example.customview.DynamicListView.java

/** Draws a black border over the screenshot of the view passed in. */
private Bitmap getBitmapWithBorder(View v) {
    Bitmap bitmap = getBitmapFromView(v);
    Canvas can = new Canvas(bitmap);
    can.drawBitmap(bitmap, 0, 0, null);
    return bitmap;
}

From source file:com.bytebit.classicbyte.ScreenMainMenu.java

public void draw(Canvas c) {
    this.parent.standard_paint.setColor(Color.WHITE);

    for (int i = 0; i != 9; i++) {
        for (int k = 0; k != 10; k++) {
            c.drawBitmap(TextureManager.getBitmap(3), k * (this.parent.renderer.width / 10),
                    (i + 1) * (this.parent.renderer.width / 10), this.parent.standard_paint);
        }//from  ww w.j  av a  2  s  .  c  om
    }

    if (this.menu_current_tab == 1) {
        Rect bounds = new Rect();
        String g;
        for (int k = 0; k != this.server_list_name.size(); k++) {
            if (k >= this.server_list_name.size()) {
                break;
            }
            if ((this.parent.renderer.width * 0.12F + this.parent.renderer.width * 0.1F * k
                    + this.server_list_y_offset) >= 0
                    && (this.parent.renderer.width * 0.12F + this.parent.renderer.width * 0.1F * k
                            + this.server_list_y_offset) <= this.parent.renderer.height) {
                this.parent.standard_paint.setColor(Color.WHITE);
                this.parent.standard_paint.setTextSize(this.parent.renderer.width * 0.04F);
                c.drawBitmap(TextureManager.getBitmap(10),
                        (this.parent.renderer.width - TextureManager.getBitmap(10).getWidth()) / 2,
                        this.parent.renderer.width * 0.02F + this.parent.renderer.width * 0.12F
                                + this.parent.renderer.width * 0.1F * k + this.server_list_y_offset,
                        this.parent.standard_paint);
                g = this.server_list_name.get(k);
                this.parent.standard_paint.getTextBounds(this.server_list_name.get(k), 0,
                        this.server_list_name.get(k).length(), bounds);
                if (bounds.width() > TextureManager.getBitmap(10).getWidth() * 0.85F) {
                    while (bounds.width() > TextureManager.getBitmap(10).getWidth() * 0.85F) {
                        this.parent.standard_paint.getTextBounds(g + " ..", 0, (g + " ..").length(), bounds);
                        g = g.substring(0, g.length() - 1);
                    }
                    g = g + " ..";
                }
                c.drawText(g,
                        (this.parent.renderer.width - TextureManager.getBitmap(10).getWidth()) / 2
                                + this.parent.renderer.width * 0.02F,
                        this.parent.renderer.width * 0.08F + this.parent.renderer.width * 0.12F
                                + this.parent.renderer.width * 0.1F * k + this.server_list_y_offset,
                        this.parent.standard_paint);
                this.parent.standard_paint.setColor(Color.YELLOW);
                this.parent.standard_paint.getTextBounds(this.server_list_online_data.get(k), 0,
                        this.server_list_online_data.get(k).length(), bounds);
                c.drawText(this.server_list_online_data.get(k),
                        this.parent.renderer.width
                                - (this.parent.renderer.width - TextureManager.getBitmap(10).getWidth()) / 2
                                - this.parent.renderer.width * 0.02F - bounds.width(),
                        this.parent.renderer.width * 0.08F + this.parent.renderer.width * 0.12F
                                + this.parent.renderer.width * 0.1F * k + this.server_list_y_offset,
                        this.parent.standard_paint);
            }
        }
        c.drawBitmap(
                TextureManager.getBitmap(26), 0, TextureManager.getBitmap(2).getHeight()
                        + TextureManager.getBitmap(22).getHeight() + this.scrollbar_offset,
                this.parent.standard_paint);
        if (this.menu_button_highlighted == 3) {
            c.drawBitmap(TextureManager.getBitmap(25), 0, TextureManager.getBitmap(2).getHeight(),
                    this.parent.standard_paint);
        } else {
            c.drawBitmap(TextureManager.getBitmap(24), 0, TextureManager.getBitmap(2).getHeight(),
                    this.parent.standard_paint);
        }
        if (this.menu_button_highlighted == 4) {
            c.drawBitmap(TextureManager.getBitmap(23), 0,
                    this.parent.renderer.height - TextureManager.getBitmap(22).getHeight(),
                    this.parent.standard_paint);
        } else {
            c.drawBitmap(TextureManager.getBitmap(22), 0,
                    this.parent.renderer.height - TextureManager.getBitmap(22).getHeight(),
                    this.parent.standard_paint);
        }
    }
    if (this.menu_current_tab == 2) {
        Rect bounds = new Rect();
        this.parent.standard_paint.setColor(Color.WHITE);
        this.parent.standard_paint.setTextSize(this.parent.renderer.height * 0.25F);
        this.parent.standard_paint.getTextBounds("Nothing here", 0, "Nothing here".length(), bounds);
        c.drawText("Nothing here", (this.parent.renderer.width - bounds.width()) / 2,
                this.parent.renderer.height * 0.5F, this.parent.standard_paint);
    }

    for (int k = 0; k != 10; k++) {
        c.drawBitmap(TextureManager.getBitmap(2), k * TextureManager.getBitmap(2).getWidth(), 0,
                this.parent.standard_paint);
    }

    c.drawBitmap(TextureManager.getBitmap(4), this.parent.renderer.width * 0.05F, 0,
            this.parent.standard_paint);
    if (this.menu_button_highlighted == 1) {
        c.drawBitmap(TextureManager.getBitmap(8), this.parent.renderer.width * 0.1F * 6.5F,
                this.parent.renderer.width * 0.01F, this.parent.standard_paint);
    } else {
        c.drawBitmap(TextureManager.getBitmap(7), this.parent.renderer.width * 0.1F * 6.5F,
                this.parent.renderer.width * 0.01F, this.parent.standard_paint);
    }
    if (this.menu_button_highlighted == 2) {
        c.drawBitmap(TextureManager.getBitmap(6), this.parent.renderer.width * 0.1F * 8.75F,
                this.parent.renderer.width * 0.01F, this.parent.standard_paint);
    } else {
        c.drawBitmap(TextureManager.getBitmap(5), this.parent.renderer.width * 0.1F * 8.75F,
                this.parent.renderer.width * 0.01F, this.parent.standard_paint);
    }

    //c.drawBitmap(TextureManager.getBitmap(9), this.parent.renderer.width-TextureManager.getBitmap(9).getWidth(), this.parent.renderer.height-TextureManager.getBitmap(9).getHeight(), this.parent.standard_paint);
}

From source file:com.bamobile.fdtks.util.Tools.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    Bitmap output;// w  ww  . j  a  va  2  s  . c  om

    if (bitmap.getWidth() > bitmap.getHeight()) {
        output = Bitmap.createBitmap(bitmap.getHeight(), bitmap.getHeight(), Config.ARGB_8888);
    } else {
        output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getWidth(), 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());

    float r = 0;

    if (bitmap.getWidth() > bitmap.getHeight()) {
        r = bitmap.getHeight() / 2;
    } else {
        r = bitmap.getWidth() / 2;
    }

    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawCircle(r, r, r, paint);
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);

    return output;

}

From source file:com.facebook.imagepipeline.animated.impl.AnimatedDrawableCachingBackendImpl.java

/**
 * Copies the source bitmap for the specified frame and caches it.
 *
 * @param frameNumber the frame number/*  ww  w. ja va  2  s .c  o  m*/
 * @param sourceBitmap the rendered bitmap to be cached (after copying)
 */
private void copyAndCacheBitmapDuringRendering(int frameNumber, Bitmap sourceBitmap) {
    CloseableReference<Bitmap> destBitmapReference = obtainBitmapInternal();
    try {
        Canvas copyCanvas = new Canvas(destBitmapReference.get());
        copyCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.SRC);
        copyCanvas.drawBitmap(sourceBitmap, 0, 0, null);
        maybeCacheRenderedBitmap(frameNumber, destBitmapReference);
    } finally {
        destBitmapReference.close();
    }
}

From source file:edu.cloud.iot.reception.ocr.FaceRecognitionActivity.java

public Bitmap toGrayscale(Bitmap bmpOriginal) {
    int height = bmpOriginal.getHeight();
    int 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);/*from  w  w w. j  ava2s .co m*/

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

From source file:com.android.leanlauncher.IconCache.java

private void renderIconBackground(Bitmap icon, Bitmap maskImage, Canvas tempCanvas, int w, int h,
        boolean drawOver) {
    // draw the scaled bitmap with mask
    Bitmap mutableMask = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);

    Canvas maskCanvas = new Canvas(mutableMask);
    maskCanvas.drawBitmap(maskImage, 0, 0, new Paint());

    // paint the bitmap with mask into the result
    Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    paint.setXfermode(new PorterDuffXfermode(drawOver ? DST_OUT : DST_IN));
    tempCanvas.drawBitmap(icon, (w - icon.getWidth()) / 2, (h - icon.getHeight()) / 2, null);
    tempCanvas.drawBitmap(mutableMask, 0, 0, paint);
    paint.setXfermode(null);/*  w  ww  .j  av a 2  s .c  om*/
}