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:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap srcBitmap, float radius) {
    Bitmap resultBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(),
            Bitmap.Config.ARGB_8888);/*from w  w w. ja v a 2s . co  m*/
    Canvas canvas = new Canvas(resultBitmap);
    Paint paint = new Paint();
    Rect rect = new Rect(0, 0, srcBitmap.getWidth(), srcBitmap.getHeight());
    RectF rectF = new RectF(rect);
    paint.setAntiAlias(true);
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(0xBDBDBE);
    canvas.drawRoundRect(rectF, radius, radius, paint);
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(srcBitmap, rect, rect, paint);
    return resultBitmap;
}

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 www .j  a v  a  2 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 flipAnimation(Bitmap animationTexture, int subImageWidth, int subImageHeight) {
    Bitmap animationTextureFlipped = Bitmap.createBitmap(animationTexture.getWidth(),
            animationTexture.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(animationTextureFlipped);
    Bitmap b;/*from w  w  w. ja v a 2s  .  co  m*/
    for (int i = 0; (i + 1) * subImageHeight <= animationTexture.getHeight(); i++) {
        for (int j = 0; (j + 1) * subImageWidth <= animationTexture.getWidth(); j++) {
            b = Bitmap.createBitmap(animationTexture, j * subImageWidth, i * subImageHeight, subImageWidth,
                    subImageHeight);
            b = flip(b);
            canvas.drawBitmap(b, j * subImageWidth, i * subImageHeight, null);
        }
    }
    return animationTextureFlipped;
}

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  w  w . j  a  v a  2s  . c o m

    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 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);/*ww w.  j  a v  a2 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

/**
 * convert Bitmap to round corner/*from  w  ww . ja v a2  s .co m*/
 *
 * @param bitmap
 * @return
 */
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);
    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

/**
 * This is only used when the launcher shortcut is created.
 *
 * @param bitmap The artist, album, genre, or playlist image that's going to
 *               be cropped.//from   w  ww .j  a  v a  2  s. c  om
 * @param size   The new size.
 * @return A {@link Bitmap} that has been resized and cropped for a launcher
 * shortcut.
 */
public static final Bitmap resizeAndCropCenter(final Bitmap bitmap, final int size) {
    final int w = bitmap.getWidth();
    final int h = bitmap.getHeight();
    if (w == size && h == size) {
        return bitmap;
    }

    final float mScale = (float) size / Math.min(w, h);

    final Bitmap mTarget = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888);
    final int mWidth = Math.round(mScale * bitmap.getWidth());
    final int mHeight = Math.round(mScale * bitmap.getHeight());
    final Canvas mCanvas = new Canvas(mTarget);
    mCanvas.translate((size - mWidth) / 2f, (size - mHeight) / 2f);
    mCanvas.scale(mScale, mScale);
    final Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    mCanvas.drawBitmap(bitmap, 0, 0, paint);
    return mTarget;
}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    float roundPx = w / 2;
    Bitmap output = Bitmap.createBitmap(w, h, Bitmap.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.j ava2s .c  o m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPx, roundPx, 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 getRoundedCornerBitmap(Bitmap bitmap, float ratio) {

    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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);

    paint.setAntiAlias(true);//from  w  w  w  .  ja  v  a2 s . co m
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, bitmap.getWidth() / ratio, bitmap.getHeight() / ratio, paint);

    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);
    return output;
}

From source file:com.balieiro.facebook.FriendItem.java

/**
  * The credit of the image processing performed by this method belongs
  * to the author of this site://from   w  w w .java 2  s .  c  o  m
  * http://www.piwai.info/transparent-jpegs-done-right/
  * There is an amazing explanation on how to perform this kind of 
  * images transformations.
  */
private static Bitmap getRoundedBitmap(Bitmap source, int pictureMask) {

    BitmapFactory.Options options = new BitmapFactory.Options();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // Starting with Honeycomb, we can load the bitmap as mutable.
        options.inMutable = true;
    }
    // We could also use ARGB_4444, but not RGB_565 (we need an alpha layer).
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap;
    if (source.isMutable()) {
        bitmap = source;
    } else {
        bitmap = source.copy(Bitmap.Config.ARGB_8888, true);
        source.recycle();
    }
    // The bitmap is opaque, we need to enable alpha compositing.
    bitmap.setHasAlpha(true);

    Canvas canvas = new Canvas(bitmap);
    Bitmap mask = BitmapFactory.decodeResource(MyFacebookApp.getContext().getResources(), pictureMask);
    Paint paint = new Paint();
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
    canvas.drawBitmap(mask, 0, 0, paint);
    // We do not need the mask bitmap anymore.
    mask.recycle();

    return bitmap;
}