List of usage examples for java.lang Math round
public static long round(double a)
From source file:Main.java
/** * Creates a mutable bitmap from subset of source bitmap, transformed by the optional matrix. *///from w w w . j a v a2 s. c om private static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m) { // Re-implement Bitmap createBitmap() to always return a mutable bitmap. Canvas canvas = new Canvas(); Bitmap bitmap; Paint paint; if ((m == null) || m.isIdentity()) { bitmap = Bitmap.createBitmap(width, height, source.getConfig()); paint = null; } else { RectF rect = new RectF(0, 0, width, height); m.mapRect(rect); bitmap = Bitmap.createBitmap(Math.round(rect.width()), Math.round(rect.height()), source.getConfig()); canvas.translate(-rect.left, -rect.top); canvas.concat(m); paint = new Paint(Paint.FILTER_BITMAP_FLAG); if (!m.rectStaysRect()) { paint.setAntiAlias(true); } } bitmap.setDensity(source.getDensity()); canvas.setBitmap(bitmap); Rect srcBounds = new Rect(x, y, x + width, y + height); RectF dstBounds = new RectF(0, 0, width, height); canvas.drawBitmap(source, srcBounds, dstBounds, paint); return bitmap; }
From source file:Main.java
public static final String formatSpace(final long limit) { if (limit < 1024L) { return limit + " B"; } else if (limit < (1024L * 1024)) { return (Math.round(limit / 10.24) / 100.0) + " KB"; } else if (limit < (1024L * 1024 * 1024)) { return (Math.round(limit / 1024 / 10.24) / 100.0) + " MB"; } else if (limit < (1024L * 1024 * 1024 * 1024)) { return (Math.round(limit / 1024.0 / 1024.0 / 10.24) / 100.0) + " GB"; } else {// w w w . ja v a2s .c om return (Math.round(limit / 1024.0 / 1024.0 / 1024.0 / 10.24) / 100.0) + " TB"; } }
From source file:Main.java
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) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else {//from ww w . ja va 2s . c om inSampleSize = Math.round((float) width / (float) reqWidth); } } return inSampleSize; }
From source file:Main.java
/** * Convert touch position x:y to {@link Camera.Area} position -1000:-1000 to 1000:1000. *//*from w w w .java2s.c o m*/ private static Rect calculateTapArea(Camera camera, float x, float y, float coefficient) { float focusAreaSize = 300; int areaSize = Float.valueOf(focusAreaSize * coefficient).intValue(); int centerX = (int) (x / getResolution(camera).width - 1000); int centerY = (int) (y / getResolution(camera).height - 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:br.com.diegosilva.jsfcomponents.util.Utils.java
public static int generateRandomNumber() { return (int) Math.round(1 + (Math.random() * 1000000)); }
From source file:Main.java
/** * For a string containing a number as judged by * <code>isNumber()</code>, return the numerical value, rounded to * an integer./*w w w . jav a 2 s. co m*/ * * @return the numeric value, rounded to an integer, or 0 * if the string is not a valid number. */ public static int getNumber(String string) { String s = string.trim(); if (!isNumber(s)) return 0; double value = 0; try { value = Double.parseDouble(s); } catch (NumberFormatException e) { //logger.warn("Unexpected number value `" + s + "'"); } return (int) Math.round(value); }
From source file:Main.java
private 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; // This offers some additional logic in case the image has a strange // aspect ratio. For example, a panorama may have a much larger // width than height. In these cases the total pixels might still // end up being too large to fit comfortably in memory, so we should // be more aggressive with sample down the image (=larger inSampleSize). final float totalPixels = width * height; // Anything more than 2x the requested pixels we'll sample down further final float totalReqPixelsCap = reqWidth * reqHeight * 2; while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) { inSampleSize++;/*from w w w.j a v a2s . c om*/ } } return inSampleSize; }
From source file:testes.Histograma.java
private static IntervalXYDataset crearDataset() { //guarda os dados do histograma HistogramDataset dados = new HistogramDataset(); int classes, min, max; //valores da amos //double amostra[] = {7,8,9,10,10,10.2,10.8,11,12,12}; double amostra[] = { 89, 85, 93, 81, 77, 83, 87, 81, 83, 89, 81, 86, 80, 84, 82 }; if (amostra.length <= 25) { classes = 5;/*from w w w.j ava 2 s . c om*/ } else { classes = (int) Math.round(Math.sqrt(amostra.length)); } dados.addSeries("Frequncias da Amostra", amostra, classes, 77, 95); return dados; }
From source file:Main.java
public static void setBestExposure(Camera.Parameters parameters, boolean lightOn) { int minExposure = parameters.getMinExposureCompensation(); int maxExposure = parameters.getMaxExposureCompensation(); float step = parameters.getExposureCompensationStep(); if ((minExposure != 0 || maxExposure != 0) && step > 0.0f) { // Set low when light is on float targetCompensation = lightOn ? MIN_EXPOSURE_COMPENSATION : MAX_EXPOSURE_COMPENSATION; int compensationSteps = Math.round(targetCompensation / step); float actualCompensation = step * compensationSteps; // Clamp value: compensationSteps = Math.max(Math.min(compensationSteps, maxExposure), minExposure); if (parameters.getExposureCompensation() == compensationSteps) { Log.i(TAG,//from w w w .j a v a2 s. c o m "Exposure compensation already set to " + compensationSteps + " / " + actualCompensation); } else { Log.i(TAG, "Setting exposure compensation to " + compensationSteps + " / " + actualCompensation); parameters.setExposureCompensation(compensationSteps); } } else { Log.i(TAG, "Camera does not support exposure compensation"); } }
From source file:Main.java
private static Drawable getMoreSuggestionsHint(final Resources res, final float textSize, final int color) { final Paint paint = new Paint(); paint.setAntiAlias(true);/* w w w .j a v a 2 s.c om*/ paint.setTextAlign(Align.CENTER); paint.setTextSize(textSize); paint.setColor(color); final Rect bounds = new Rect(); paint.getTextBounds(MORE_SUGGESTIONS_HINT, 0, MORE_SUGGESTIONS_HINT.length(), bounds); final int width = Math.round(bounds.width() + 0.5f); final int height = Math.round(bounds.height() + 0.5f); final Bitmap buffer = Bitmap.createBitmap(width, (height * 3 / 2), Bitmap.Config.ARGB_8888); final Canvas canvas = new Canvas(buffer); canvas.drawText(MORE_SUGGESTIONS_HINT, width / 2, height, paint); return new BitmapDrawable(res, buffer); }