List of usage examples for java.lang Math round
public static long round(double a)
From source file:Main.java
private 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; }// www .ja v a 2s .co m return inSampleSize; }
From source file:Main.java
/** * Decode a file from the path and return a bitmap * @param uri/*from w w w .j ava2 s. com*/ * @param context * @return */ public static Bitmap decodeFileFromPath(Uri uri, Context context) { InputStream in = null; try { in = context.getContentResolver().openInputStream(uri); //Decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inJustDecodeBounds = true; BitmapFactory.decodeStream(in, null, o); in.close(); int scale = 1; int inSampleSize = 1024; if (o.outHeight > inSampleSize || o.outWidth > inSampleSize) { scale = (int) Math.pow(2, (int) Math.round( Math.log(inSampleSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5))); } BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize = scale; in = context.getContentResolver().openInputStream(uri); Bitmap b = BitmapFactory.decodeStream(in, null, o2); in.close(); return b; } catch (FileNotFoundException e) { //e.printStackTrace(); } catch (IOException e) { //e.printStackTrace(); } return null; }
From source file:Main.java
@SuppressWarnings("deprecation") public static int PutImageScale(Canvas canvas, Drawable image, double Angle, int x, int y, double scale) { if (scale == 0.0) return 0; float newWidth = (int) Math.round((float) image.getIntrinsicWidth() * scale); float newHeight = (int) Math.round((float) image.getIntrinsicHeight() * 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);/* w w w. j a v a 2 s. com*/ return bmd.getIntrinsicWidth(); }
From source file:Main.java
/** * Standardize the compression ratio i.e round off the compression ratio to * only 2 significant digits.// ww w . j a v a2 s . c o m */ static float standardizeCompressionRatio(float ratio) { // round off to 2 significant digits int significant = (int) Math.round(ratio * 100); return ((float) significant) / 100; }
From source file:Main.java
/** * @param context/*from www . j av a 2 s.co m*/ * @param height * @return */ public static int getMetricsDensity(Context context, float height) { DisplayMetrics localDisplayMetrics = new DisplayMetrics(); ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay() .getMetrics(localDisplayMetrics); return Math.round(height * localDisplayMetrics.densityDpi / 160.0F); }
From source file:Main.java
/** * Get a BitmapDrawable from a local file that is scaled down * to fit the current Window size./* w w w. j a v a 2 s. co m*/ */ @SuppressWarnings("deprecation") public static BitmapDrawable getScaledDrawable(Activity a, String path) { Display display = a.getWindowManager().getDefaultDisplay(); float destWidth = display.getWidth(); float destHeight = display.getHeight(); // read in the dimensions of the image on disk BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); float srcWidth = options.outWidth; float srcHeight = options.outHeight; int inSampleSize = 1; if (srcHeight > destHeight || srcWidth > destWidth) { if (srcWidth > srcHeight) { inSampleSize = Math.round((float) srcHeight / ((float) destHeight) * 3); } else { inSampleSize = Math.round((float) srcWidth / ((float) destWidth) * 3); } } options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; Bitmap bitmap = BitmapFactory.decodeFile(path, options); return new BitmapDrawable(a.getResources(), bitmap); }
From source file:Main.java
/** * Convert a dp float value to pixels/*from w w w . ja va2 s .com*/ * @param context * @param dp * @return the responsive pixels */ public static int dp2px(Context context, float dp) { float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics()); return Math.round(px); }
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) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else {/*from w w w .j a v a2 s .co m*/ inSampleSize = Math.round((float) width / (float) reqWidth); } return inSampleSize; } return inSampleSize; }
From source file:Main.java
/** * Get a BitmapDrawable from a local file that is scaled down * to fit the current Window size.//from w w w .jav a 2s . c o m */ @SuppressWarnings("deprecation") static BitmapDrawable getScaledBitmap(String path, Activity a) { Display display = a.getWindowManager().getDefaultDisplay(); float destWidth = display.getWidth(); float destHeight = display.getHeight(); // read in the dimensions of the image on disk BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); float srcWidth = options.outWidth; float srcHeight = options.outHeight; int inSampleSize = 1; if (srcHeight > destHeight || srcWidth > destWidth) { if (srcWidth > srcHeight) { inSampleSize = Math.round(srcHeight / destHeight); } else { inSampleSize = Math.round(srcWidth / destWidth); } } options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; Bitmap bitmap = BitmapFactory.decodeFile(path, options); return new BitmapDrawable(a.getResources(), bitmap); }
From source file:Main.java
/** Modify the colors translucency by alpha [0..1] with respect to the original color alpha. */ public static int applyColorAlpha(int color, float alpha) { final int originalAlpha = Color.alpha(color); // Return the color, multiplying the original alpha by the disabled value return (color & 0x00ffffff) | (Math.round(originalAlpha * alpha) << 24); }