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 void addStars(int cre, LinearLayout container, Activity activity, int drawableResId) {
    container.removeAllViews();/*from  w  w  w  .j  a v  a  2  s  . c  o  m*/
    int num = (int) Math.round(cre * 1.0 / 10);
    for (int i = 0; i < num; i++) {
        container.addView(newStar(activity, drawableResId));
    }
}

From source file:Main.java

public static String scaleImageFile(String filepath, int targetSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    File file = new File(filepath);
    float scaleFactor = file.length() / targetSize;
    options.inSampleSize = Math.round(scaleFactor);
    Bitmap bitmap = BitmapFactory.decodeFile(filepath, options);

    int dotPos = filepath.lastIndexOf('.');
    String newFilePath = String.format("%s_scaled.%s", filepath.substring(0, dotPos),
            filepath.substring(dotPos + 1, filepath.length()));
    FileOutputStream fos = null;/*from   ww  w  .  j  ava 2 s.co m*/
    try {
        fos = new FileOutputStream(newFilePath);
        if (!bitmap.compress(CompressFormat.JPEG, 90, fos)) {
            newFilePath = null;
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
        newFilePath = null;
    }
    bitmap.recycle();
    return newFilePath;
}

From source file:Main.java

public static double gps2m(double lat_a, double lng_a, double lat_b, double lng_b) {
    double radLat1 = (lat_a * Math.PI / 180.0);
    double radLat2 = (lat_b * Math.PI / 180.0);
    double a = radLat1 - radLat2;
    double b = (lng_a - lng_b) * Math.PI / 180.0;
    double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a / 2), 2)
            + Math.cos(radLat1) * Math.cos(radLat2) * Math.pow(Math.sin(b / 2), 2)));
    s = s * EARTH_RADIUS;//from w  w  w  . j  a v a 2  s.  co m
    s = Math.round(s * 10000) / 10000;
    return s;
}

From source file:Main.java

public static Bitmap fastblur(Bitmap sentBitmap, int radius) {
    int width = Math.round(sentBitmap.getWidth());
    int height = Math.round(sentBitmap.getHeight());
    sentBitmap = Bitmap.createScaledBitmap(sentBitmap, width, height, false);

    Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);

    if (radius < 1) {
        return (null);
    }/*from ww w. jav a 2  s.c  o m*/

    int w = bitmap.getWidth();
    int h = bitmap.getHeight();

    int[] pix = new int[w * h];
    bitmap.getPixels(pix, 0, w, 0, 0, w, h);

    int wm = w - 1;
    int hm = h - 1;
    int wh = w * h;
    int div = radius + radius + 1;

    int r[] = new int[wh];
    int g[] = new int[wh];
    int b[] = new int[wh];
    int a[] = new int[wh];
    int rsum, gsum, bsum, asum, x, y, i, p, yp, yi, yw;
    int vmin[] = new int[Math.max(w, h)];

    int divsum = (div + 1) >> 1;
    divsum *= divsum;
    int dv[] = new int[256 * divsum];
    for (i = 0; i < 256 * divsum; i++) {
        dv[i] = (i / divsum);
    }

    yw = yi = 0;

    int[][] stack = new int[div][4];
    int stackpointer;
    int stackstart;
    int[] sir;
    int rbs;
    int r1 = radius + 1;
    int routsum, goutsum, boutsum, aoutsum;
    int rinsum, ginsum, binsum, ainsum;

    for (y = 0; y < h; y++) {
        rinsum = ginsum = binsum = ainsum = routsum = goutsum = boutsum = aoutsum = rsum = gsum = bsum = asum = 0;
        for (i = -radius; i <= radius; i++) {
            p = pix[yi + Math.min(wm, Math.max(i, 0))];
            sir = stack[i + radius];
            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            sir[3] = 0xff & (p >> 24);

            rbs = r1 - Math.abs(i);
            rsum += sir[0] * rbs;
            gsum += sir[1] * rbs;
            bsum += sir[2] * rbs;
            asum += sir[3] * rbs;
            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
                ainsum += sir[3];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
                aoutsum += sir[3];
            }
        }
        stackpointer = radius;

        for (x = 0; x < w; x++) {

            r[yi] = dv[rsum];
            g[yi] = dv[gsum];
            b[yi] = dv[bsum];
            a[yi] = dv[asum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;
            asum -= aoutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];
            aoutsum -= sir[3];

            if (y == 0) {
                vmin[x] = Math.min(x + radius + 1, wm);
            }
            p = pix[yw + vmin[x]];

            sir[0] = (p & 0xff0000) >> 16;
            sir[1] = (p & 0x00ff00) >> 8;
            sir[2] = (p & 0x0000ff);
            sir[3] = 0xff & (p >> 24);

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];
            ainsum += sir[3];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;
            asum += ainsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[(stackpointer) % div];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];
            aoutsum += sir[3];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];
            ainsum -= sir[3];

            yi++;
        }
        yw += w;
    }
    for (x = 0; x < w; x++) {
        rinsum = ginsum = binsum = ainsum = routsum = goutsum = boutsum = aoutsum = rsum = gsum = bsum = asum = 0;
        yp = -radius * w;
        for (i = -radius; i <= radius; i++) {
            yi = Math.max(0, yp) + x;

            sir = stack[i + radius];

            sir[0] = r[yi];
            sir[1] = g[yi];
            sir[2] = b[yi];
            sir[3] = a[yi];

            rbs = r1 - Math.abs(i);

            rsum += r[yi] * rbs;
            gsum += g[yi] * rbs;
            bsum += b[yi] * rbs;
            asum += a[yi] * rbs;

            if (i > 0) {
                rinsum += sir[0];
                ginsum += sir[1];
                binsum += sir[2];
                ainsum += sir[3];
            } else {
                routsum += sir[0];
                goutsum += sir[1];
                boutsum += sir[2];
                aoutsum += sir[3];
            }

            if (i < hm) {
                yp += w;
            }
        }
        yi = x;
        stackpointer = radius;
        for (y = 0; y < h; y++) {
            pix[yi] = (dv[asum] << 24) | (dv[rsum] << 16) | (dv[gsum] << 8) | dv[bsum];

            rsum -= routsum;
            gsum -= goutsum;
            bsum -= boutsum;
            asum -= aoutsum;

            stackstart = stackpointer - radius + div;
            sir = stack[stackstart % div];

            routsum -= sir[0];
            goutsum -= sir[1];
            boutsum -= sir[2];
            aoutsum -= sir[3];

            if (x == 0) {
                vmin[y] = Math.min(y + r1, hm) * w;
            }
            p = x + vmin[y];

            sir[0] = r[p];
            sir[1] = g[p];
            sir[2] = b[p];
            sir[3] = a[p];

            rinsum += sir[0];
            ginsum += sir[1];
            binsum += sir[2];
            ainsum += sir[3];

            rsum += rinsum;
            gsum += ginsum;
            bsum += binsum;
            asum += ainsum;

            stackpointer = (stackpointer + 1) % div;
            sir = stack[stackpointer];

            routsum += sir[0];
            goutsum += sir[1];
            boutsum += sir[2];
            aoutsum += sir[3];

            rinsum -= sir[0];
            ginsum -= sir[1];
            binsum -= sir[2];
            ainsum -= sir[3];

            yi += w;
        }
    }
    bitmap.setPixels(pix, 0, w, 0, 0, w, h);
    return (bitmap);
}

From source file:Main.java

public static int HSLtoRGB(float[] hsl) {
    final float h = hsl[0];
    final float s = hsl[1];
    final float l = hsl[2];

    final float c = (1f - Math.abs(2 * l - 1f)) * s;
    final float m = l - 0.5f * c;
    final float x = c * (1f - Math.abs((h / 60f % 2f) - 1f));

    final int hueSegment = (int) h / 60;

    int r = 0, g = 0, b = 0;

    switch (hueSegment) {
    case 0:/*from   w w  w  . j  a v a  2s  . c  o m*/
        r = Math.round(255 * (c + m));
        g = Math.round(255 * (x + m));
        b = Math.round(255 * m);
        break;
    case 1:
        r = Math.round(255 * (x + m));
        g = Math.round(255 * (c + m));
        b = Math.round(255 * m);
        break;
    case 2:
        r = Math.round(255 * m);
        g = Math.round(255 * (c + m));
        b = Math.round(255 * (x + m));
        break;
    case 3:
        r = Math.round(255 * m);
        g = Math.round(255 * (x + m));
        b = Math.round(255 * (c + m));
        break;
    case 4:
        r = Math.round(255 * (x + m));
        g = Math.round(255 * m);
        b = Math.round(255 * (c + m));
        break;
    case 5:
    case 6:
        r = Math.round(255 * (c + m));
        g = Math.round(255 * m);
        b = Math.round(255 * (x + m));
        break;
    }

    r = Math.max(0, Math.min(255, r));
    g = Math.max(0, Math.min(255, g));
    b = Math.max(0, Math.min(255, b));

    return Color.rgb(r, g, 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 (reqWidth == 0 || reqHeight == 0)
        return 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;
    }//from w  ww .  j  a  va 2s. c  o  m

    return inSampleSize;
}

From source file:Main.java

public static Bitmap decodeBitmap(File file, int reqHeight, int reqWidth) {
    // Get image size of file
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;//  w w w  . ja v  a 2  s .com
    BitmapFactory.decodeFile(file.getAbsolutePath(), options);
    final int height = options.outHeight;
    final int width = options.outWidth;

    // Calculate sample size
    if (height > reqHeight || width > reqWidth) {
        final int heightRatio = Math.round((float) height / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);
        options.inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    // Decode bitmap
    options.inJustDecodeBounds = false;
    options.inPreferredConfig = Bitmap.Config.RGB_565;
    options.inPurgeable = true;
    options.inDither = false;
    return BitmapFactory.decodeFile(file.getAbsolutePath(), options);
}

From source file:Main.java

/**
 * Method returning color with modified alpha proportional to given values.
 *
 * @param color     color to modify/*from ww  w .java  2s.c  o  m*/
 * @param fullValue total value
 * @param partValue part of fullValue. When partValue equals fullValue, the alpha is 255.
 * @return color with alpha relative to partValue/fullValue ratio.
 */
public static int getColorWithProportionalAlpha(int color, float fullValue, float partValue) {
    float progress = Math.min(Math.max(partValue, 0), fullValue) / fullValue;
    return Color.argb(Math.round(Color.alpha(color) * progress), Color.red(color), Color.green(color),
            Color.blue(color));
}

From source file:Main.java

/** Returns an array which partitions the interval [0, max) into n approximately equal integer segments.
 * The returned array will have n+1 elements, with the first always 0 and the last always max.
 * If max=100 and n=3, the returned array will be [0, 33, 67, 100] representing the intervals
 * [0, 33), [33, 67), and [67, 100). /*w w w.  j a va2  s  . c om*/
 */
public static int[] equalIntPartitions(int max, int n) {
    int[] result = new int[n + 1];
    result[0] = 0;
    for (int i = 1; i < n; i++) {
        float val = 1.0f * i * max / n;
        result[i] = Math.round(val);
    }
    result[n] = max;
    return result;
}

From source file:Main.java

public static HashMap<String, Bitmap> splitNumAnimBitmap(Bitmap bitmap, int xPiece, int yPiece) {
    HashMap<String, Bitmap> pieces = new HashMap<String, Bitmap>();
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    float pieceWidth = (float) width / xPiece;
    float pieceHeight = (float) height / yPiece;
    for (int i = 0; i < yPiece; i++) {
        for (int j = 0; j < xPiece; j++) {
            int cellWidth = Math.round(pieceWidth);
            int cellHeight = Math.round(pieceHeight);
            int imgWidth = Math.round(j * pieceWidth);
            int imgHeight = Math.round(i * pieceHeight);
            Bitmap piece = Bitmap.createBitmap(bitmap, imgWidth, imgHeight, cellWidth, cellHeight);
            String bitmapTag = null;
            switch (j) {
            case 0:
                bitmapTag = "+";
                break;
            case 1:
                bitmapTag = 0 + "";
                break;
            case 2:
                bitmapTag = 1 + "";
                break;
            case 3:
                bitmapTag = 2 + "";
                break;
            case 4:
                bitmapTag = 3 + "";
                break;
            case 5:
                bitmapTag = 4 + "";
                break;
            case 6:
                bitmapTag = 5 + "";
                break;
            case 7:
                bitmapTag = 6 + "";
                break;
            case 8:
                bitmapTag = 7 + "";
                break;
            case 9:
                bitmapTag = 8 + "";
                break;
            case 10:
                bitmapTag = 9 + "";
                break;
            default:
                break;
            }//w  w w .  jav a 2 s . c  o  m
            pieces.put(bitmapTag, piece);
        }
    }
    return pieces;
}