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:visual.FinalToolTipChart.java

@Override
public String generateToolTip(XYDataset xyd, int i, int i1) {
    String s = Double.toString((double) Math.round(xyd.getYValue(i, i1) * 100000) / 100000);
    String actName = actValCont.getActionName(i1);
    return actName + " (" + String.valueOf(i1) + ", " + s + ")";
}

From source file:Main.java

/**
 * Blend two colors./*from  w  ww .jav a 2  s.  c  om*/
 * 
 * @param color1
 *            First color to blend.
 * @param color2
 *            Second color to blend.
 * @param ratio
 *            Blend ratio. 0.5 will give even blend, 1.0 will return color1,
 *            0.0 will return color2 and so on.
 * @return Blended color.
 */
public static int blend(final int color1, final int color2, final double ratio) {
    final float r = (float) ratio;
    final float ir = (float) 1.0 - r;

    final float rgb1[] = new float[3];
    final float rgb2[] = new float[3];

    rgb1[0] = Color.red(color1);
    rgb1[1] = Color.green(color1);
    rgb1[2] = Color.blue(color1);
    rgb2[0] = Color.red(color2);
    rgb2[1] = Color.green(color2);
    rgb2[2] = Color.blue(color2);

    final int color = Color.rgb(Math.round(rgb1[0] * r + rgb2[0] * ir), Math.round(rgb1[1] * r + rgb2[1] * ir),
            Math.round(rgb1[2] * r + rgb2[2] * ir));

    return color;
}

From source file:com.strider.datadefender.extensions.BiographicFunctions.java

private static int randBetween(final int start, final int end) {
    return start + (int) Math.round(Math.random() * (end - start));
}

From source file:com.github.jgility.core.util.ReleasePlanningUtils.java

private static long calculateNewDayRange(long oldRange, long newRange, long iterationRange) {
    double tmpIterationRange = iterationRange;
    double tmpOldRange = oldRange;
    double tmpNewRange = newRange;
    return Math.round((tmpIterationRange / tmpOldRange) * tmpNewRange);
}

From source file:Main.java

@SuppressWarnings("deprecation")
public static int PutImageTargetHeight(Canvas canvas, Drawable image, double Angle, int x, int y,
        int newHeight) {

    float scale = (float) newHeight / (float) image.getIntrinsicHeight();
    float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale);

    Bitmap bmp = ((BitmapDrawable) image).getBitmap();
    int width = bmp.getWidth();
    int height = bmp.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;
    // createa matrix for the manipulation
    Matrix matrix = new Matrix();
    // resize the bit map
    matrix.postScale(scaleWidth, scaleHeight);
    // rotate the Bitmap
    matrix.postRotate((float) Angle);
    // recreate the new Bitmap
    Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width, height, matrix, true);
    // make a Drawable from Bitmap to allow to set the BitMap
    // to the ImageView, ImageButton or what ever
    BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);

    bmd.setBounds(x, y, x + bmd.getIntrinsicWidth(), y + bmd.getIntrinsicHeight());
    bmd.draw(canvas);/*from  www.  j  av a2s  .  c  o m*/

    return bmd.getIntrinsicWidth();

}

From source file:Main.java

private static int calculateSampleSize(final int reqWidth, final int reqHeight,
        final BitmapFactory.Options options) {

    int actualImageHeight = options.outHeight;
    int actualImageWidth = options.outWidth;

    int sampleSize = 1;

    if (reqWidth < actualImageWidth || reqHeight < actualImageHeight) {
        if (actualImageWidth > actualImageHeight) {
            sampleSize = Math.round((float) actualImageHeight / (float) reqHeight);
        } else {//from  w w w .  j  a va  2  s . c o m
            sampleSize = Math.round((float) actualImageWidth / (float) reqWidth);
        }
    }
    return sampleSize;
}

From source file:Main.java

/**
 * See: http://stackoverflow.com/questions/477572/android-strange
 *       -out-of-memory-issue-while-loading-an-image
 *       -to-a-bitmap-object/823966#823966
 * Thanks to StackOverflow user named Fedor.
 *///from  w  w w  .  j  a  va 2 s  .com
public static Bitmap decodeFile(File f, int size) {
    Bitmap b = null;
    try {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;

        FileInputStream fis = new FileInputStream(f);
        BitmapFactory.decodeStream(fis, null, o);
        fis.close();

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

        // Decode with inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inTempStorage = new byte[32 * 1024];
        o2.inPurgeable = true;
        o2.inSampleSize = scale;
        fis = new FileInputStream(f);
        b = BitmapFactory.decodeStream(fis, null, o2);
        fis.close();
    } catch (IOException e) {
    }
    return b;
}

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);

        // 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;
    }/*  w  w w. ja va  2 s .co m*/

    return inSampleSize;
}

From source file:Main.java

public static Bitmap tryToGetBitmapFromInternet(String bitmapUrl, Context context, int imageSize) {
    if (null != bitmapUrl) {
        InputStream inputStream = null;
        try {/*from  ww  w .  j a  v a 2 s .c o  m*/
            URL url = new URL(bitmapUrl);

            URLConnection connection = url.openConnection();

            connection.connect();

            inputStream = connection.getInputStream();
            ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
            byte[] buffer = new byte[1024];
            int read;

            while ((read = inputStream.read(buffer)) != -1) {
                byteArrayOutputStream.write(buffer, 0, read);
            }
            inputStream.close();
            byteArrayOutputStream.close();

            buffer = byteArrayOutputStream.toByteArray();

            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;

            BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);

            int maxSize = Math.max(options.outWidth, options.outHeight);
            float newImageScale = 1f;
            if (-1 != imageSize) {
                newImageScale = maxSize / (imageSize * context.getResources().getDisplayMetrics().density);
            }

            options.inJustDecodeBounds = false;
            options.inSampleSize = Math.round(newImageScale);

            return BitmapFactory.decodeByteArray(buffer, 0, buffer.length, options);
        } catch (Throwable e) {
            // pass
        } finally {
            if (null != inputStream) {
                try {
                    inputStream.close();
                } catch (IOException e) {
                    // pass
                }
                inputStream = null;
            }
            System.gc();
        }
    }

    return null;
}

From source file:Main.java

/**
 * @param options/*from  ww w. ja v  a 2  s .  co  m*/
 * @param reqWidth
 * @param reqHeight
 * @return
 */
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) {
        if (width > height) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
    return inSampleSize;
}