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 int PutImageTargetHeight(Canvas canvas, Drawable image, int x, int y, int height) {
    // float scale = (float)height / (float)image.getBounds().height();
    // int width = (int)Math.round(image.getBounds().width() * scale);

    float scale = (float) height / (float) image.getIntrinsicHeight();
    int width = (int) Math.round((float) image.getIntrinsicWidth() * scale);

    Rect oldBounds = image.getBounds();//from   w  w  w.ja  v  a  2  s .  c  o m
    image.setBounds(x, y, x + width, y + height);
    image.draw(canvas);
    image.setBounds(oldBounds);

    return width;
}

From source file:Main.java

public static Bitmap decodeSampledBitmapFromResource(int resId, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  ww  w  .  j ava2  s  .  c o m*/
    BitmapFactory.decodeResource(context.getResources(), resId, options);

    // Calculate inSampleSize
    float sampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    Log.d("----> Sample Size:", sampleSize + "");
    if (sampleSize < 1) { // return a scaled UP version of image
        Bitmap bg = BitmapFactory.decodeResource(context.getResources(), resId);
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bg, (int) (bg.getWidth() / sampleSize),
                (int) (bg.getHeight() / sampleSize), true);
        bg.recycle();
        return scaledBitmap;
    } else { // return a scaled DOWN version of image
        options.inSampleSize = Math.round(sampleSize);
        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(context.getResources(), resId, options);
    }
}

From source file:Main.java

public static Date toModifiedTimeDate(double modDouble) {
    try {/*from  www .j a v  a  2  s . c om*/
        long mod = Math.round(modDouble * 1000);
        return new Date(mod);
    } catch (Exception e) {
        return null;
    }
}

From source file:Main.java

/***
 * Fetch the raw estimated time enroute given the input parameters
 * @param distance - how far to the target
 * @param speed - how fast we are moving
 * @param bearing - direction to target/*from  w  w  w.j ava 2 s .  c o  m*/
 * @param heading - direction of movement
 * @return int value of HR * 100 + MIN for the ete, -1 if not applicable
 */
private static int fetchRawEte(double distance, double speed, double bearing, double heading) {
    // We can't assume that we are heading DIRECTLY for the destination, so 
    // we need to figure out the multiply factor by taking the COS of the difference
    // between the bearing and the heading.
    double angDif = angularDifference(heading, bearing);

    // If the difference is 90 or greater, then ETE means nothing as we are not
    // closing on the target
    if (angDif >= 90)
        return -1;

    // Calculate the actual relative speed closing on the target
    double xFactor = Math.cos(angDif * Math.PI / 180);
    double eteTotal = distance / (speed * xFactor);

    // Break that down into hours and minutes
    int eteHr = (int) eteTotal;
    int eteMin = (int) Math.round((eteTotal - (double) eteHr) * 60);

    // account for the minutes being 60
    if (eteMin >= 60) {
        eteHr++;
        eteMin -= 60;
    }

    // Return with our estimate
    return eteHr * 100 + eteMin;
}

From source file:Main.java

public static int PutImageTargetHeightColor(Canvas canvas, Drawable image, int x, int y, int height, int color,
        Mode porterDuff) {//from  w w w .  j  av  a 2  s .  co  m

    float scale = (float) height / (float) image.getIntrinsicHeight();
    int width = (int) Math.round(image.getIntrinsicWidth() * scale);

    Rect oldBounds = image.getBounds();
    image.setBounds(x, y, x + width, y + height);
    image.setColorFilter(color, porterDuff);
    image.draw(canvas);
    image.setBounds(oldBounds);
    image.clearColorFilter();
    return width;
}

From source file:Main.java

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    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;
    }/* w  ww  .j a  v  a2s .  c  o m*/
    return inSampleSize;
}

From source file:Main.java

/**
 * format double value//from  www .  j av a 2 s  . c o m
 * @param d    the amount
 * @param len  the field len
 * @return a String of fieldLen characters (right justified)
 */
public static String formatDouble(double d, int len) {
    String prefix = Long.toString((long) d);
    String suffix = Integer.toString((int) ((Math.round(d * 100f)) % 100));
    try {
        if (len > 3)
            prefix = padleft(prefix, len - 3, ' ');
        suffix = zeropad(suffix, 2);
    } catch (Exception e) {
        // should not happen
    }
    return prefix + "." + suffix;
}

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) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        inSampleSize = heightRatio > widthRatio ? heightRatio : widthRatio;
    }//from w ww  .  j av  a 2 s . c  o m

    // Log.d(TAG, "height:" + height + "width" + width);

    return inSampleSize;
}

From source file:Main.java

public static Bitmap byteToBitmap(byte[] bitmapData, int size) {
    if (bitmapData == null)
        return null;
    Options options = new Options();
    options.inJustDecodeBounds = true;/* w  ww  .  j  a  va  2s. c  om*/
    BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
    int w = options.outWidth;
    int h = options.outHeight;
    int sc = 0;
    if (w > size || h > size)
        if (w > h) {
            sc = Math.round((float) w / (float) size);
        } else {
            sc = Math.round((float) h / (float) size);
        }
    options.inJustDecodeBounds = false;
    options.inSampleSize = sc;
    try {
        return BitmapFactory.decodeByteArray(bitmapData, 0, bitmapData.length, options);
    } catch (OutOfMemoryError e) {
        return null;
    }
}

From source file:Main.java

public static void fillHorGradient(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[i]);/*  w w w  .  java  2 s  .c o  m*/
            if (i == (steps - 1)) {
                g.fillRect(x, y1, w, y + h - y1);
            } else {
                g.fillRect(x, y1, w, y2 - y1);
            }
            y1 = y2;
        }
    } else {
        smoothFillHorGradient(g, colors, x, y, w, h);
    }
}