Example usage for java.lang Math max

List of usage examples for java.lang Math max

Introduction

In this page you can find the example usage for java.lang Math max.

Prototype

@HotSpotIntrinsicCandidate
public static double max(double a, double b) 

Source Link

Document

Returns the greater of two double values.

Usage

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  v  a  2  s  .  c  o  m*/
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

/**
 * Creates an approximated cubic gradient using a multi-stop linear gradient.
 *//*from   ww w.j ava2 s . c  o  m*/
public static Drawable makeCubicGradientScrimDrawable(int baseColor, int numStops, int gravity) {

    // Generate a cache key by hashing together the inputs, based on the method described in the Effective Java book
    int cacheKeyHash = baseColor;
    cacheKeyHash = 31 * cacheKeyHash + numStops;
    cacheKeyHash = 31 * cacheKeyHash + gravity;

    Drawable cachedGradient = cubicGradientScrimCache.get(cacheKeyHash);
    if (cachedGradient != null) {
        return cachedGradient;
    }

    numStops = Math.max(numStops, 2);

    PaintDrawable paintDrawable = new PaintDrawable();
    paintDrawable.setShape(new RectShape());

    final int[] stopColors = new int[numStops];

    int red = Color.red(baseColor);
    int green = Color.green(baseColor);
    int blue = Color.blue(baseColor);
    int alpha = Color.alpha(baseColor);

    for (int i = 0; i < numStops; i++) {
        float x = i * 1f / (numStops - 1);
        float opacity = constrain(0, 1, (float) Math.pow(x, 3));
        stopColors[i] = Color.argb((int) (alpha * opacity), red, green, blue);
    }

    final float x0, x1, y0, y1;
    switch (gravity & Gravity.HORIZONTAL_GRAVITY_MASK) {
    case Gravity.LEFT:
        x0 = 1;
        x1 = 0;
        break;
    case Gravity.RIGHT:
        x0 = 0;
        x1 = 1;
        break;
    default:
        x0 = 0;
        x1 = 0;
        break;
    }
    switch (gravity & Gravity.VERTICAL_GRAVITY_MASK) {
    case Gravity.TOP:
        y0 = 1;
        y1 = 0;
        break;
    case Gravity.BOTTOM:
        y0 = 0;
        y1 = 1;
        break;
    default:
        y0 = 0;
        y1 = 0;
        break;
    }

    paintDrawable.setShaderFactory(new ShapeDrawable.ShaderFactory() {
        @Override
        public Shader resize(int width, int height) {
            LinearGradient linearGradient = new LinearGradient(width * x0, height * y0, width * x1, height * y1,
                    stopColors, null, Shader.TileMode.CLAMP);
            return linearGradient;
        }
    });

    cubicGradientScrimCache.put(cacheKeyHash, paintDrawable);
    return paintDrawable;
}

From source file:Main.java

/**
 * Sets the JButtons inside a JPanelto be the same size.
 * This is done dynamically by setting each button's preferred and maximum
 * sizes after the buttons are created. This way, the layout automatically
 * adjusts to the locale-specific strings.
 *
 * @param jPanelButtons JPanel containing buttons
 *//*from   www . j a  v a 2  s  .  c o m*/
public static void equalizeButtonSizes(JPanel jPanelButtons) {
    ArrayList<JButton> lbuttons = new ArrayList<JButton>();
    for (int i = 0; i < jPanelButtons.getComponentCount(); i++) {
        Component c = jPanelButtons.getComponent(i);
        if (c instanceof JButton) {
            lbuttons.add((JButton) c);
        }
    }

    // Get the largest width and height
    Dimension maxSize = new Dimension(0, 0);
    for (JButton lbutton : lbuttons) {
        Dimension d = lbutton.getPreferredSize();
        maxSize.width = Math.max(maxSize.width, d.width);
        maxSize.height = Math.max(maxSize.height, d.height);
    }

    for (JButton btn : lbuttons) {
        btn.setPreferredSize(maxSize);
        btn.setMinimumSize(maxSize);
        btn.setMaximumSize(maxSize);
    }
}

From source file:Main.java

/**
 * <p>Gets the maximum of three <code>float</code> values.</p>
 * //from  w  ww  .j av  a2  s  .co  m
 * <p>If any value is <code>NaN</code>, <code>NaN</code> is
 * returned. Infinity is handled.</p>
 *
 * @param a  value 1
 * @param b  value 2
 * @param c  value 3
 * @return  the largest of the values
 * @see IEEE754rUtils#max(float, float, float) for a version of this method that handles NaN differently
 */
public static float max(float a, float b, float c) {
    return Math.max(Math.max(a, b), c);
}

From source file:annis.utils.Utils.java

public static String max(List<Long> runtimeList) {
    long max = Long.MIN_VALUE;
    for (long value : runtimeList) {
        max = Math.max(max, value);
    }//from w  ww . ja  va 2s.c om
    return String.valueOf(max);
}

From source file:Main.java

/**
 * <p>Gets the maximum of three <code>double</code> values.</p>
 * /*from w  w  w .  ja v a2s .  co  m*/
 * <p>If any value is <code>NaN</code>, <code>NaN</code> is
 * returned. Infinity is handled.</p>
 *
 * @param a  value 1
 * @param b  value 2
 * @param c  value 3
 * @return  the largest of the values
 * @see IEEE754rUtils#max(double, double, double) for a version of this method that handles NaN differently
 */
public static double max(double a, double b, double c) {
    return Math.max(Math.max(a, b), c);
}

From source file:Main.java

/**
 * Gets straighten matrix for the given bounds and degrees.
 *///from   ww  w .  ja v  a  2 s .  c  o m
public static void getStraightenMatrix(RectF bounds, float degrees, Matrix matrix) {
    matrix.reset();
    if ((degrees != 0) && !bounds.isEmpty()) {
        float w = bounds.width() / 2;
        float h = bounds.height() / 2;
        float adjustAngle;
        if ((degrees < 0 && w > h) || (degrees > 0 && w <= h)) {
            // The top left point is the boundary.
            adjustAngle = (float) Math.atan(h / -w) + MATH_PI + degrees * DEGREES_TO_RADIAN;
        } else {
            // The top right point is the boundary.
            adjustAngle = (float) Math.atan(h / w) - MATH_PI + degrees * DEGREES_TO_RADIAN;
        }
        float radius = (float) Math.hypot(w, h);
        float scaleX = (float) Math.abs(radius * Math.cos(adjustAngle)) / w;
        float scaleY = (float) Math.abs(radius * Math.sin(adjustAngle)) / h;
        float scale = Math.max(scaleX, scaleY);

        postRotateMatrix(degrees, new RectF(bounds), matrix);
        matrix.postScale(scale, scale);
    }
}

From source file:Main.java

public static Bitmap emboss(Bitmap bitmap) {
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    Bitmap newBitmap = Bitmap.createBitmap(width, height, Config.RGB_565);

    int pixR = 0;
    int pixG = 0;
    int pixB = 0;

    int pixColor = 0;

    int newR = 0;
    int newG = 0;
    int newB = 0;

    int[] pixels = new int[width * height];
    bitmap.getPixels(pixels, 0, width, 0, 0, width, height);
    int pos = 0;/*w  ww .j  a  v  a2s .c  o m*/
    for (int i = 1, length = height - 1; i < length; i++) {
        for (int k = 1, len = width - 1; k < len; k++) {
            pos = i * width + k;
            pixColor = pixels[pos];

            pixR = Color.red(pixColor);
            pixG = Color.green(pixColor);
            pixB = Color.blue(pixColor);

            pixColor = pixels[pos + 1];
            newR = Color.red(pixColor) - pixR + 127;
            newG = Color.green(pixColor) - pixG + 127;
            newB = Color.blue(pixColor) - pixB + 127;

            newR = Math.min(255, Math.max(0, newR));
            newG = Math.min(255, Math.max(0, newG));
            newB = Math.min(255, Math.max(0, newB));

            pixels[pos] = Color.argb(255, newR, newG, newB);
        }
    }

    newBitmap.setPixels(pixels, 0, width, 0, 0, width, height);
    return newBitmap;
}

From source file:net.mindengine.galen.specs.Range.java

public static Range between(double from, double to) {
    return new Range(Math.min(from, to), Math.max(from, to)).withType(RangeType.BETWEEN);
}

From source file:Main.java

/**
 * Returns maximum insets combined from the specified ones.
 *
 * @param insets1 first insets/*  ww w.  j  a  v  a 2  s .  c o m*/
 * @param insets2 second insets
 * @return maximum insets
 */
public static Insets max(final Insets insets1, final Insets insets2) {
    if (insets1 != null && insets2 != null) {
        return new Insets(Math.max(insets1.top, insets2.top), Math.max(insets1.left, insets2.left),
                Math.max(insets1.bottom, insets2.bottom), Math.max(insets1.right, insets2.right));
    } else if (insets1 != null) {
        return insets1;
    } else if (insets2 != null) {
        return insets2;
    } else {
        return null;
    }
}