Example usage for java.lang Math round

List of usage examples for java.lang Math round

Introduction

In this page you can find the example usage for java.lang Math round.

Prototype

public static long round(double a) 

Source Link

Document

Returns the closest long to the argument, with ties rounding to positive infinity.

Usage

From source file:Main.java

public static Bitmap getCircleBitmap(Context context, Bitmap src, float radius) {
    radius = dipTopx(context, radius);/*  ww w .  j  a  va 2  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;
}

From source file:Main.java

private static Bitmap bitmapCrop(Bitmap rawBitmap, int width, int height, int resWidth, int resHeight) {

    int cropX, cropY, cropWidth, cropHeight;

    float xScale = (float) width / (float) resWidth;
    float yScale = (float) height / (float) resHeight;
    float scale = Math.max(xScale, yScale);

    if (xScale >= yScale) {
        cropWidth = Math.round(resWidth);
        cropX = 0;/*from w ww  .j a va2 s . c  om*/
        cropHeight = Math.round(height / scale);
        cropY = (resHeight - cropHeight) / 2;
    } else {
        cropWidth = Math.round(width / scale);
        cropX = (resWidth - cropWidth) / 2;
        cropHeight = Math.round(resHeight);
        cropY = 0;
    }

    return Bitmap.createBitmap(rawBitmap, cropX, cropY, cropWidth, cropHeight);
}

From source file:Main.java

public static Bitmap byteToBitmap(byte[] bitmapData) {
    if (bitmapData == null)
        return null;
    Options options = new Options();
    options.inJustDecodeBounds = true;//from w ww  . j a  v  a 2  s .  c  o  m
    int srcWidth = options.outWidth;
    options.inJustDecodeBounds = false;
    int be = 0;
    be = (int) Math.round(((double) srcWidth) / ((double) 80));
    options = new Options();
    options.inSampleSize = be;
    try {
        return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (height > reqHeight || width > reqWidth) {
        // Calculate ratios of height and width to requested height and width
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }/*  ww w. j  ava 2  s .c  o m*/

    return inSampleSize;
}

From source file:Main.java

public static int dpToPx(int dp, Context ctx) {
    DisplayMetrics displayMetrics = ctx.getResources().getDisplayMetrics();
    int px = Math.round(dp * (displayMetrics.xdpi / DisplayMetrics.DENSITY_DEFAULT));
    return px;/*from   w  w w  . ja  v  a  2  s  .c  o m*/
}

From source file:Main.java

/**
 * Get a rectangle for the given 4 points (x0,y0,x1,y1,x2,y2,x3,y3) by finding the min/max 2 points that
 * contains the given 4 points and is a stright rectangle.
 */// www . java  2  s  .  com
static Rect getRectFromPoints(float[] points, int imageWidth, int imageHeight, boolean fixAspectRatio,
        int aspectRatioX, int aspectRatioY) {
    int left = Math.round(Math.max(0, getRectLeft(points)));
    int top = Math.round(Math.max(0, getRectTop(points)));
    int right = Math.round(Math.min(imageWidth, getRectRight(points)));
    int bottom = Math.round(Math.min(imageHeight, getRectBottom(points)));

    Rect rect = new Rect(left, top, right, bottom);
    if (fixAspectRatio) {
        fixRectForAspectRatio(rect, aspectRatioX, aspectRatioY);
    }

    return rect;
}

From source file:Main.java

/**
 * @param pX/*w ww .  j av  a2s .  co  m*/
 * @param pY
 * @param pMix [0...1]
 * @return (int)Math.round(pX * (1 - pMix) + pY * pMix)
 */
public static final int mix(final int pX, final int pY, final float pMix) {
    return (int) Math.round(pX * (1 - pMix) + pY * pMix);
}

From source file:Main.java

/**
 * caculate the bitmap sampleSize/*  ww w  .j av  a 2 s . co  m*/
 *
 * @return inSampleSize
 */
private static int caculateInSampleSize(BitmapFactory.Options options, int rqsW, int rqsH) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;
    if (rqsW == 0 || rqsH == 0)
        return 1;
    if (height > rqsH || width > rqsW) {
        final int heightRatio = Math.round((float) height / (float) rqsH);
        final int widthRatio = Math.round((float) width / (float) rqsW);
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
}

From source file:Main.java

private static int calculateInSampleSize(Options options, int reqWidth, int reqHeight) {
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        // Calculate ratios of height and width to requested height and
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }//  www .  jav a 2 s  . c o m
    if (inSampleSize < 1) {
        inSampleSize = 1;
    }

    return inSampleSize;
}

From source file:Main.java

private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    if (reqWidth == 0 || reqHeight == 0) {
        return 1;
    }//from   ww w. ja v a 2 s  .c  o m

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }
    return inSampleSize;
}