Example usage for android.graphics Bitmap getWidth

List of usage examples for android.graphics Bitmap getWidth

Introduction

In this page you can find the example usage for android.graphics Bitmap getWidth.

Prototype

public final int getWidth() 

Source Link

Document

Returns the bitmap's width

Usage

From source file:Main.java

public static Bitmap flipBitmap(Bitmap src) {
    Matrix m = new Matrix();
    m.preScale(-1, 1);/*from w  w w  . j  a  v  a2s .  co m*/
    Bitmap dst = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), m, false);
    dst.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    return dst;
}

From source file:Main.java

public static Bitmap resizeBitmapByScale(Bitmap bitmap, float scale, boolean recycle) {
    int width = Math.round(bitmap.getWidth() * scale);
    int height = Math.round(bitmap.getHeight() * scale);
    if (width == bitmap.getWidth() && height == bitmap.getHeight())
        return bitmap;
    Bitmap target = Bitmap.createBitmap(width, height, getConfig(bitmap));
    Canvas canvas = new Canvas(target);
    canvas.scale(scale, scale);//  w w  w  . j a va  2  s.  co m
    Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG | Paint.DITHER_FLAG);
    canvas.drawBitmap(bitmap, 0, 0, paint);
    if (recycle)
        bitmap.recycle();
    return target;
}

From source file:Main.java

public static Bitmap resizeBitmap(Bitmap source, int maxWidth, int maxHeight) {
    int outWidth;
    int outHeight;
    int inWidth = source.getWidth();
    int inHeight = source.getHeight();
    if (inWidth > inHeight) {
        outWidth = maxWidth;//from w ww  .j ava 2 s.co  m
        outHeight = (inHeight * maxWidth) / inWidth;
    } else {
        outHeight = maxHeight;
        outWidth = (inWidth * maxHeight) / inHeight;
    }

    return Bitmap.createScaledBitmap(source, outWidth, outHeight, false);
}

From source file:Main.java

public static Bitmap createReflectionImageWithOrigin(Bitmap bitmap) {
    final int reflectionGap = 4;
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();

    Matrix matrix = new Matrix();
    matrix.preScale(1, -1);//  w w w  .  j a  v  a 2s. com

    Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2, width, height / 2, matrix, false);

    Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888);

    Canvas canvas = new Canvas(bitmapWithReflection);
    canvas.drawBitmap(bitmap, 0, 0, null);
    Paint deafalutPaint = new Paint();
    canvas.drawRect(0, height, width, height + reflectionGap, deafalutPaint);

    canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

    Paint paint = new Paint();
    LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
            bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.CLAMP);
    paint.setShader(shader);
    // Set the Transfer mode to be porter duff and destination in   
    paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
    // Draw a rectangle using the paint with our linear gradient   
    canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint);

    return bitmapWithReflection;
}

From source file:Main.java

public static Bitmap zoomBitmap(Bitmap bitmap, int w, int h) {
    Bitmap newbmp = null;// w w w. j  a  v a 2  s.  c o m
    if (bitmap != null) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        Matrix matrix = new Matrix();
        float scaleWidht = ((float) w / width);
        float scaleHeight = ((float) h / height);
        matrix.postScale(scaleWidht, scaleHeight);
        newbmp = Bitmap.createBitmap(bitmap, 0, 0, width, height, matrix, true);
    }
    return newbmp;
}

From source file:Main.java

/** Return a bitmap identical to src with rounded corners. */
public static final Bitmap roundCornersRGB888(Bitmap src, int roundPixles) {
    final Bitmap output = Bitmap.createBitmap(src.getWidth(), src.getHeight(), Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    // TODO: document what is special about this color
    final int color = 0xff424242;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, src.getWidth(), src.getHeight());
    final RectF rectF = new RectF(rect);

    paint.setAntiAlias(true);//from  w w w .j a v  a2  s . co  m
    canvas.drawARGB(0, 0, 0, 0);
    paint.setColor(color);
    canvas.drawRoundRect(rectF, roundPixles, roundPixles, paint);

    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
    canvas.drawBitmap(src, 0, 0, paint);

    return output;
}

From source file:Main.java

/**
 * Convert bitmap to the grayscale/*from  www  .j a  v a 2s .com*/
 * http://androidsnippets.com/convert-bitmap-to-grayscale
 *
 * @param bmpOriginal Original bitmap
 * @return Grayscale bitmap
 */
public static Bitmap toGrayscale(Bitmap bmpOriginal) {
    final int height = bmpOriginal.getHeight();
    final int width = bmpOriginal.getWidth();

    final Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Config.ARGB_8888);
    final Canvas c = new Canvas(bmpGrayscale);
    final Paint paint = new Paint();
    final ColorMatrix cm = new ColorMatrix();
    cm.setSaturation(0);
    final ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm);
    paint.setColorFilter(f);
    c.drawBitmap(bmpOriginal, 0, 0, paint);
    return bmpGrayscale;
}

From source file:Main.java

/**
 * For rotating images/*from  ww w  .  ja va2  s . c o m*/
 * @param bitmap Bitmap to rotate
 * @param degree Degree amount to rotate
 * @return
 */
public static Bitmap rotate(Bitmap bitmap, int degree) {
    if (bitmap == null) {
        return null;
    }
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    Matrix mtx = new Matrix();
    mtx.setRotate(degree);

    return Bitmap.createBitmap(bitmap, 0, 0, w, h, mtx, true);
}

From source file:Main.java

public static Bitmap createScaledToCenterInsideBitmap(Bitmap bitmap, int reqWidth, int reqHeight) {
    final int height = bitmap.getHeight();
    final int width = bitmap.getWidth();

    float widthScaleF = (float) width / (float) reqWidth;
    float heightScaleF = (float) height / (float) reqHeight;
    if (widthScaleF > heightScaleF) {
        return Bitmap.createScaledBitmap(bitmap, reqWidth, (int) (height / widthScaleF), true);
    } else {//  w ww .  j  a  v a  2 s.c o  m
        return Bitmap.createScaledBitmap(bitmap, (int) (width / heightScaleF), reqHeight, true);
    }
}

From source file:Main.java

public static Bitmap getCircleBitmap(Context context, Bitmap src, float radius) {
    radius = dipTopx(context, radius);// w  ww  . ja va2 s  . c  o m
    int w = src.getWidth();
    int h = src.getHeight();
    int canvasW = Math.round(radius * 2);
    Bitmap bitmap = Bitmap.createBitmap(canvasW, canvasW, Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(bitmap);

    Path path = new Path();
    path.addCircle(radius, radius, radius, Path.Direction.CW);
    canvas.clipPath(path);

    Paint paint = new Paint();
    paint.setAntiAlias(true);

    Rect srcRect = new Rect(0, 0, w, h);
    Rect dstRect = new Rect(0, 0, canvasW, canvasW);

    canvas.drawBitmap(src, srcRect, dstRect, paint);

    return bitmap;
}