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 decodeFile(String fPath) {
    BitmapFactory.Options opts = new BitmapFactory.Options();
    opts.inJustDecodeBounds = true;//from ww  w. j a  v a2s.  c om
    opts.inDither = false; // Disable Dithering mode
    opts.inPurgeable = true; // Tell to gc that whether it needs free
    opts.inInputShareable = true; // Which kind of reference will be used to
    BitmapFactory.decodeFile(fPath, opts);
    final int REQUIRED_SIZE = 200;
    int scale = 1;
    if (opts.outHeight > REQUIRED_SIZE || opts.outWidth > REQUIRED_SIZE) {
        final int heightRatio = Math.round((float) opts.outHeight / (float) REQUIRED_SIZE);
        final int widthRatio = Math.round((float) opts.outWidth / (float) REQUIRED_SIZE);
        scale = heightRatio < widthRatio ? heightRatio : widthRatio;//
    }
    Log.i("scale", "scal =" + scale);
    opts.inJustDecodeBounds = false;
    opts.inSampleSize = scale;
    Bitmap bm = BitmapFactory.decodeFile(fPath, opts).copy(Config.ARGB_8888, false);
    return bm;
}

From source file:Main.java

public static int getMaxDeltaVsync() {
    return Math.round((float) mMaxDeltaVsync / mRefreshPeriod);
}

From source file:Main.java

public static void rectFToRect(RectF rectF, Rect rect) {
    rect.left = Math.round(rectF.left);
    rect.top = Math.round(rectF.top);
    rect.right = Math.round(rectF.right);
    rect.bottom = Math.round(rectF.bottom);
}

From source file:Main.java

/**
 * Sanitize the provided zoom level//from w ww. j a  v a2  s  . co m
 *
 * @param val The value
 * @return If the value lies outsize the range 1-16, it will be set to 1 or 16 (whichever is closest).  Within
 *         that range, the value will rounded to the nearest multiple of 2 (eg 2, 4, 6,..).
 */
public static int sanitizeZoom(int val) {

    int step = 2, min = 1, max = 16;

    val = (Math.round(val / step)) * step;

    if (val <= min) {
        return min;
    }
    if (val >= max) {
        return max;
    }

    return val;
}

From source file:Main.java

private static Pair<Integer, Integer> clampDimensions(int inWidth, int inHeight, int maxWidth, int maxHeight) {
    if (inWidth > maxWidth || inHeight > maxHeight) {
        final float aspectWidth, aspectHeight;

        if (inWidth == 0 || inHeight == 0) {
            aspectWidth = maxWidth;/*from  w ww  . j  av  a  2s .  com*/
            aspectHeight = maxHeight;
        } else if (inWidth >= inHeight) {
            aspectWidth = maxWidth;
            aspectHeight = (aspectWidth / inWidth) * inHeight;
        } else {
            aspectHeight = maxHeight;
            aspectWidth = (aspectHeight / inHeight) * inWidth;
        }

        return new Pair<>(Math.round(aspectWidth), Math.round(aspectHeight));
    } else {
        return new Pair<>(inWidth, inHeight);
    }
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth, int reqHeight) { // BEST QUALITY MATCH

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//from   w ww.  j a  va 2s .  co  m
    BitmapFactory.decodeFile(path, options);

    // Calculate inSampleSize, Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    int inSampleSize = 1;

    if (height > reqHeight) {
        inSampleSize = Math.round((float) height / (float) reqHeight);
    }
    int expectedWidth = width / inSampleSize;

    if (expectedWidth > reqWidth) {
        // if(Math.round((float)width / (float)reqWidth) > inSampleSize) //
        // If bigger SampSize..
        inSampleSize = Math.round((float) width / (float) reqWidth);
    }

    options.inSampleSize = inSampleSize;

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;

    return BitmapFactory.decodeFile(path, options);
}

From source file:Main.java

public static int dp2Px(int dp) {
    return Math.round(dp * DENSITY);
}

From source file:Main.java

public static Bitmap decodeFileFromDrawable(int id, int maxSize, Context context) {
    Bitmap b = null;//  ww w .j a  va2s .  co  m
    try {
        // Decode image size
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        InputStream inputStream = context.getResources().openRawResource(id);
        BitmapFactory.decodeStream(inputStream, null, o);
        inputStream.close();

        int scale = 1;
        if (o.outHeight > maxSize || o.outWidth > maxSize) {
            scale = (int) Math.pow(2, (int) Math
                    .round(Math.log(maxSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
        }

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        inputStream = context.getResources().openRawResource(id);
        b = BitmapFactory.decodeStream(inputStream, null, o2);
        inputStream.close();
    } catch (IOException e) {
    }
    return b;
}

From source file:Main.java

/**
 * Rounds a number to a given number of significant decimal digits.
 * Note that the number will be left with *only* this number of
 * significant digits regardless of magnitude, e.g. 12345 to 3 digits
 * will be 12300, whereas 0.12345 will be 0.123.
 *
 * @param value the value to round off./*from  w w  w.j  av a 2 s  .  com*/
 * @param n     the number of significant decimal digits desired.
 * @return a rounded off number.
 */
public static double roundToSignificantDigits(double value, int n) {
    if (value == 0.0) {
        return 0.0;
    }

    final double d = Math.ceil(Math.log10(value < 0.0 ? -value : value));
    final int power = n - (int) d;

    final double magnitude = Math.pow(10.0, power);
    final long shifted = Math.round(value * magnitude);
    return shifted / magnitude;
}

From source file:Main.java

public static int convertDpToPixel(Context ctx, int dp) {
    float density = ctx.getResources().getDisplayMetrics().density;
    return Math.round((float) dp * density);
}