Example usage for android.graphics Paint FILTER_BITMAP_FLAG

List of usage examples for android.graphics Paint FILTER_BITMAP_FLAG

Introduction

In this page you can find the example usage for android.graphics Paint FILTER_BITMAP_FLAG.

Prototype

int FILTER_BITMAP_FLAG

To view the source code for android.graphics Paint FILTER_BITMAP_FLAG.

Click Source Link

Document

Paint flag that enables bilinear sampling on scaled bitmaps.

Usage

From source file:Main.java

public static Bitmap combineTwoBitmaps(Bitmap background, Bitmap foreground) {
    Bitmap combinedBitmap = Bitmap.createBitmap(background.getWidth(), background.getHeight(),
            background.getConfig());//from   w w w  .  j  a v a2  s  .c  o  m
    Canvas canvas = new Canvas(combinedBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(background, 0, 0, paint);
    canvas.drawBitmap(foreground, 0, 0, paint);
    return combinedBitmap;
}

From source file:Main.java

private static Bitmap getResizeBitmap(View view, Bitmap bitmap) {
    Bitmap overlay = Bitmap.createBitmap((int) (view.getMeasuredWidth() / sScaleFactor),
            (int) (view.getMeasuredHeight() / sScaleFactor), Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(overlay);
    canvas.translate(-view.getLeft() / sScaleFactor, -view.getTop() / sScaleFactor);
    canvas.scale(1 / sScaleFactor, 1 / sScaleFactor);
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    return overlay;
}

From source file:Main.java

public static Bitmap createScaledBitmap(Bitmap unscaledBitmap, int dstWidth, int dstHeight,
        ScaleType scalingLogic) {/*from w ww .  j  a v  a2s .co m*/
    Rect srcRect = calculateSrcRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Rect dstRect = calculateDstRect(unscaledBitmap.getWidth(), unscaledBitmap.getHeight(), dstWidth, dstHeight,
            scalingLogic);
    Bitmap scaledBitmap = Bitmap.createBitmap(dstRect.width(), dstRect.height(), Config.ARGB_8888);
    Canvas canvas = new Canvas(scaledBitmap);
    canvas.drawBitmap(unscaledBitmap, srcRect, dstRect, new Paint(Paint.FILTER_BITMAP_FLAG));
    return scaledBitmap;
}

From source file:Main.java

public static Bitmap BITMAP_RESIZER(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);/*ww  w  .  j av a2 s .c o m*/
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;

}

From source file:Main.java

public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, float cornerRadius) {
    if (bitmap == null) {
        return null;
    }/*  w w  w.  ja v a2 s  .  c  om*/
    Bitmap result = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(result);

    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);
    canvas.setDrawFilter(new PaintFlagsDrawFilter(0, Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, cornerRadius, cornerRadius, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(result, rect, rect, paint);

    return result;
}

From source file:Main.java

/**
 * Method to scale {@code sourceBitmap}, maintaining the same original size of the bitmap,
 * but with a transparent frame and the scaled and centered {@code sourceBitmap} inside.
 *
 * @return/*from   w  ww  .ja  v  a  2  s. c  o m*/
 */
public static Bitmap scaleInsideWithFrame(Bitmap mutableBitmap, float factor, int color) {
    Bitmap clearBitmap = mutableBitmap.copy(Bitmap.Config.ARGB_8888, true);
    clearBitmap.eraseColor(color);

    Bitmap resizedInsideBitmap = scaleBitmapByFactor(mutableBitmap, factor);

    int frameWidth = clearBitmap.getWidth();
    int frameHeight = clearBitmap.getHeight();
    int imageWidth = resizedInsideBitmap.getWidth();
    int imageHeight = resizedInsideBitmap.getHeight();

    Canvas canvas = new Canvas(clearBitmap);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    paint.setAntiAlias(true);
    canvas.drawBitmap(resizedInsideBitmap, (frameWidth - imageWidth) / 2, (frameHeight - imageHeight) / 2,
            paint);
    return clearBitmap;
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    if (bitmap == null) {
        return null;
    }/*from w ww  .j a  v  a  2s  .c  o m*/
    /**
     * http://stackoverflow.com/questions/4821488/bad-image-quality-after-resizing-scaling-bitmap#7468636
     */
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888);

    float ratioX = newWidth / (float) bitmap.getWidth();
    float ratioY = newHeight / (float) bitmap.getHeight();
    float middleX = newWidth / 2.0f;
    float middleY = newHeight / 2.0f;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, middleX - bitmap.getWidth() / 2, middleY - bitmap.getHeight() / 2,
            new Paint(Paint.FILTER_BITMAP_FLAG));

    return scaledBitmap;
}

From source file:Main.java

public static Bitmap drawViewToBitmap(View view, int width, int height, float translateX, float translateY,
        int downSampling, String color) {
    float scale = 1f / downSampling;
    int bmpWidth = (int) (width * scale - translateX / downSampling);
    int bmpHeight = (int) (height * scale - translateY / downSampling);
    Bitmap dest = Bitmap.createBitmap(bmpWidth, bmpHeight, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(dest);
    canvas.translate(-translateX / downSampling, -translateY / downSampling);
    if (downSampling > 1) {
        canvas.scale(scale, scale);/*w w w  . j av  a  2s.c  o m*/
    }
    Paint paint = new Paint();
    paint.setFlags(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG);
    PorterDuffColorFilter filter = new PorterDuffColorFilter(Color.parseColor(color), PorterDuff.Mode.SRC_ATOP);
    paint.setColorFilter(filter);
    view.buildDrawingCache();
    Bitmap cache = view.getDrawingCache();
    canvas.drawBitmap(cache, 0, 0, paint);
    cache.recycle();
    view.destroyDrawingCache();

    return dest;
}

From source file:Main.java

public static Bitmap cropCenter(Bitmap bitmap, boolean recycle) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    if (width == height)
        return bitmap;
    int size = Math.min(width, height);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2, (size - height) / 2);
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)//from w w w . j ava 2 s .c  om
        bitmap.recycle();
    return target;
}

From source file:Main.java

/**
 * Scales the provided bitmap to the height and width provided (using antialiasing).
 * (Alternative method for scaling bitmaps since Bitmap.createScaledBitmap(...) produces low quality bitmaps.)
 *
 * @param bitmap    is the bitmap to scale.
 * @param newWidth  is the desired width of the scaled bitmap.
 * @param newHeight is the desired height of the scaled bitmap.
 * @return the scaled bitmap.//from  w w w  .j  a v  a  2s  . co  m
 */
public static Bitmap scaleBitmap(Bitmap bitmap, int newWidth, int newHeight) {
    Bitmap scaledBitmap = Bitmap.createBitmap(newWidth, newHeight, Bitmap.Config.ARGB_8888);

    float scaleX = newWidth / (float) bitmap.getWidth();
    float scaleY = newHeight / (float) bitmap.getHeight();
    float pivotX = 0;
    float pivotY = 0;

    Matrix scaleMatrix = new Matrix();
    scaleMatrix.setScale(scaleX, scaleY, pivotX, pivotY);

    Canvas canvas = new Canvas(scaledBitmap);
    canvas.setMatrix(scaleMatrix);
    canvas.drawBitmap(bitmap, 0, 0, new Paint(Paint.FILTER_BITMAP_FLAG | Paint.ANTI_ALIAS_FLAG));

    return scaledBitmap;
}