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 resizeAndCropCenterExt(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    if (width > height) {
        int largeSize = (int) (width <= size * 1.5 ? width : size * 1.5);
        Bitmap target = Bitmap.createBitmap(largeSize, size, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((largeSize - width) / 2f, (size - height) / 2f);
        canvas.scale(scale, scale);//from ww w. jav a  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;
    } else {
        int largeSize = (int) (height <= size * 1.5 ? height : size * 1.5);
        Bitmap target = Bitmap.createBitmap(size, largeSize, getConfig(bitmap));
        Canvas canvas = new Canvas(target);
        canvas.translate((size - width) / 2f, (largeSize - height) / 2f);
        canvas.scale(scale, scale);
        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 resizeAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    if (w == size && h == size)
        return bitmap;

    // scale the image so that the shorter side equals to the target;
    // the longer side will be center-cropped.
    float scale = (float) size / Math.min(w, h);

    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2f, (size - height) / 2f);
    canvas.scale(scale, scale);//  w w w  .j  a  v  a  2 s.c o  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

private static Rect calculateTapArea(float x, float y, int width, int height, float coefficient) {
    float focusAreaSize = 200;
    int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue();
    int centerX = (int) ((x / width) * 2000 - 1000);
    int centerY = (int) ((y / height) * 2000 - 1000);
    int left = clamp(centerX - (areaSize / 2), -1000, 1000);
    int top = clamp(centerY - (areaSize / 2), -1000, 1000);
    RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
    return new Rect(Math.round(rectF.left), Math.round(rectF.top), Math.round(rectF.right),
            Math.round(rectF.bottom));
}

From source file:Main.java

public static void fillInverseHorGradient(Graphics g, Color[] colors, int x, int y, int w, int h) {
    int steps = colors.length;
    double dy = (double) h / (double) steps;
    if (dy <= 3.001) {
        int y1 = y;
        for (int i = 0; i < steps; i++) {
            int y2 = y + (int) Math.round((double) i * dy);
            g.setColor(colors[colors.length - i - 1]);
            if (i == (steps - 1)) {
                g.fillRect(x, y1, w, y + h - y1);
            } else {
                g.fillRect(x, y1, w, y2 - y1);
            }/*w  w  w  . j a  v  a  2s . c om*/
            y1 = y2;
        }
    } else {
        smoothFillInverseHorGradient(g, colors, x, y, w, h);
    }

}

From source file:Main.java

public static Bitmap resizeDownAndCropCenter(Bitmap bitmap, int size, boolean recycle) {
    int w = bitmap.getWidth();
    int h = bitmap.getHeight();
    int minSide = Math.min(w, h);
    if (w == h && minSide <= size)
        return bitmap;
    size = Math.min(size, minSide);

    float scale = Math.max((float) size / bitmap.getWidth(), (float) size / bitmap.getHeight());
    Bitmap target = Bitmap.createBitmap(size, size, getConfig(bitmap));
    int width = Math.round(scale * bitmap.getWidth());
    int height = Math.round(scale * bitmap.getHeight());
    Canvas canvas = new Canvas(target);
    canvas.translate((size - width) / 2f, (size - height) / 2f);
    canvas.scale(scale, scale);// w  w w  .j a v  a  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

/**
 * This method calculates a lighter color provided a factor of increase in lightness.
 *
 * @param color A color value/* www . j a v  a2 s.  c  om*/
 * @param factor Factor of increase in lightness, range can be between 0 - 1.0
 * @return  The calculated darker color
 */
public static int calculateLighterColor(final int color, final float factor) {
    final int a = Color.alpha(color);
    final int r = Color.red(color);
    final int g = Color.green(color);
    final int b = Color.blue(color);

    final int lightnessLevel = Math.round(RGB_TOTAL_COLORS * factor);

    return Color.argb(a, Math.min(r + lightnessLevel, 255), Math.min(g + lightnessLevel, 255),
            Math.min(b + lightnessLevel, 255));
}

From source file:Main.java

/**
 * This method calculates a darker color provided a factor of reduction in lightness.
 *
 * @param color The original color value
 * @param factor Factor of lightness reduction, range can be between 0 - 1.0
 * @return  The calculated darker color/*  ww w  . j a v a2s  .c  om*/
 */
public static int calculateDarkerColor(final int color, final float factor) {
    final int a = Color.alpha(color);
    final int r = Color.red(color);
    final int g = Color.green(color);
    final int b = Color.blue(color);

    final int lightnessLevel = Math.round(RGB_TOTAL_COLORS * factor);

    return Color.argb(a, Math.max(r - lightnessLevel, 0), Math.max(g - lightnessLevel, 0),
            Math.max(b - lightnessLevel, 0));
}

From source file:Main.java

/**
 * Scale down the bitmap in order to make color analysis faster. Taken from Palette.
 *//*from  w w w.j a  va  2 s  .  c  om*/
private static Bitmap scaleBitmapDown(Bitmap bitmap) {
    final int CALCULATE_BITMAP_MIN_DIMENSION = 100;
    final int minDimension = Math.min(bitmap.getWidth(), bitmap.getHeight());

    if (minDimension <= CALCULATE_BITMAP_MIN_DIMENSION) {
        // If the bitmap is small enough already, just return it
        return bitmap;
    }

    final float scaleRatio = CALCULATE_BITMAP_MIN_DIMENSION / (float) minDimension;
    return Bitmap.createScaledBitmap(bitmap, Math.round(bitmap.getWidth() * scaleRatio),
            Math.round(bitmap.getHeight() * scaleRatio), false);
}

From source file:Main.java

public static int convertDoubleToPluralInteger(double value) {
    double absoluteValue = Math.abs(value);
    if (absoluteValue > 2.5) {
        return (int) Math.round(absoluteValue);
    } else {//from   w w  w.j a  v a2s.c  om
        if (absoluteValue == 0.0 || absoluteValue == 1.0 || absoluteValue == 2.0) {
            return (int) absoluteValue;
        } else {
            // Random Number to get into the "other" keyword for values like 0.99 or 2.001 seconds or degrees
            // in hopefully all possible languages
            return TRANSLATION_PLURAL_OTHER_INTEGER;
        }
    }
}

From source file:Main.java

/**
 * @author chri//from  w  w  w .  j  a  v a 2  s. c  o  m
 */
public static String roundDecimalsToString(double d) {
    // using DecimalFormat we get i18n issues.
    if (d < 10)
        return String.valueOf(Math.ceil(d * 100) / 100);
    if (d < 100)
        return String.valueOf(Math.ceil(d * 10) / 10);
    else
        return String.valueOf(Math.round(d));
}