Example usage for java.lang Math min

List of usage examples for java.lang Math min

Introduction

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

Prototype

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

Source Link

Document

Returns the smaller of two double values.

Usage

From source file:Main.java

/**
 * Returns the most reasonable position for the specified rectangle to be placed at so as to
 * maximize its containment by the specified bounding rectangle while still placing it as near
 * its original coordinates as possible.
 *
 * @param rect the rectangle to be positioned.
 * @param bounds the containing rectangle.
 *//*from   w w w  .  ja va2  s . c o m*/
public static Point fitRectInRect(Rectangle rect, Rectangle bounds) {
    // Guarantee that the right and bottom edges will be contained and do our best for the top
    // and left edges.
    return new Point(Math.min(bounds.x + bounds.width - rect.width, Math.max(rect.x, bounds.x)),
            Math.min(bounds.y + bounds.height - rect.height, Math.max(rect.y, bounds.y)));
}

From source file:Main.java

static int computeListCapacity(int arraySize) {
    return (int) Math.min(5L + arraySize + (arraySize / 10), Integer.MAX_VALUE);
}

From source file:Main.java

public static <O> List<O> safeSubList(List<O> l, int offset, int count) {
    if (l == null) {
        return null;
    }//from   w  w w .  j  a  v a2s .  com

    if (offset >= l.size())
        return Collections.emptyList();

    if (count == 0)
        count = l.size();

    return l.subList(offset, Math.min(offset + count, l.size()));
}

From source file:Main.java

public static Bitmap getPic(File file, int width, int height) {
    String uri = file.getAbsolutePath();
    // Get the dimensions of the bitmap
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(uri, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    // Determine how much to scale down the image
    int scaleFactor = Math.min(photoW / width, photoH / height);

    // Decode the image file into a Bitmap sized to fill the View
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;/*from   w ww  .  j a  va2 s  .  c o m*/

    return BitmapFactory.decodeFile(uri, bmOptions);
}

From source file:Main.java

public static Bitmap decodeBitmap(String imagePath, int targetWidth, int targetHeight) {
    BitmapFactory.Options bmOptions = new BitmapFactory.Options();
    bmOptions.inJustDecodeBounds = true;
    BitmapFactory.decodeFile(imagePath, bmOptions);
    int photoW = bmOptions.outWidth;
    int photoH = bmOptions.outHeight;

    /* Figure out which way needs to be reduced less */
    int scaleFactor = 1;
    if ((targetWidth > 0) || (targetHeight > 0)) {
        scaleFactor = Math.min(photoW / targetWidth, photoH / targetHeight);
    }/*  w ww  .j a  v  a  2 s  .  com*/

    /* Set bitmap options to scale the image decode target */
    bmOptions.inJustDecodeBounds = false;
    bmOptions.inSampleSize = scaleFactor;
    bmOptions.inPurgeable = true;

    /* Decode the JPEG file into a Bitmap */
    return BitmapFactory.decodeFile(imagePath, bmOptions);

}

From source file:Main.java

public static void drawToCenterOfCanvas(Canvas canvas, Bitmap bp, int max_width, int max_height,
        Rect cacheRect) {/*from  www  .  j ava 2s.c  o m*/
    final int b_w = bp.getWidth();
    final int b_h = bp.getHeight();
    if (b_w <= max_width && b_h <= max_height) { // center aligned
        canvas.drawBitmap(bp, (max_width - b_w) / 2, (max_height - b_h) / 2, null);
    } else { // scaled fix given rect size
        final float s_w = 1.0f * max_width / b_w;
        final float s_h = 1.0f * max_height / b_h;
        final float f_s = Math.min(s_w, s_h);
        final int f_w = (int) (b_w * f_s);
        final int f_h = (int) (b_h * f_s);
        cacheRect.set(0, 0, f_w, f_h);
        cacheRect.offset((max_width - f_w) / 2, (max_height - f_h) / 2);
        canvas.drawBitmap(bp, null, cacheRect, null);
    }
}

From source file:Main.java

/**
 * Creates an animation to fade the dialog opacity from 0 to 1.
 *///from   ww w. j  a va2s  .c om
public static void fadeIn(final JDialog dialog) {
    final Timer timer = new Timer(10, null);
    timer.setRepeats(true);
    timer.addActionListener(new ActionListener() {

        private float opacity = 0;

        @Override
        public void actionPerformed(ActionEvent e) {
            opacity += 0.15f;
            dialog.setOpacity(Math.min(opacity, 1));
            if (opacity >= 1) {
                timer.stop();
            }
        }
    });

    dialog.setOpacity(0);
    timer.start();
    dialog.setVisible(true);
}

From source file:Main.java

public static Bitmap getRoundedShape(Bitmap scaleBitmapImage, int width) {
    // TODO Auto-generated method stub
    int targetWidth = width;
    int targetHeight = width;
    Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, Bitmap.Config.ARGB_8888);

    Canvas canvas = new Canvas(targetBitmap);
    Path path = new Path();
    path.addCircle(((float) targetWidth - 1) / 2, ((float) targetHeight - 1) / 2,
            (Math.min(((float) targetWidth), ((float) targetHeight)) / 2), Path.Direction.CCW);
    canvas.clipPath(path);//from  www.jav a 2  s  .  com
    Bitmap sourceBitmap = scaleBitmapImage;
    canvas.drawBitmap(sourceBitmap, new Rect(0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight()),
            new Rect(0, 0, targetWidth, targetHeight), null);
    return targetBitmap;
}

From source file:Main.java

static int getAlpha(int preferenceValue) {
    preferenceValue = 100 - Math.min(preferenceValue, 100);
    return (preferenceValue * 255) / 100;
}

From source file:Main.java

static float getPercentage(int preferenceValue) {
    preferenceValue = Math.min(preferenceValue, 100);
    return (preferenceValue) / 100f;
}