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

public static CharSequence highlightText(String search, String originalText) {
    if (search != null && !search.equalsIgnoreCase("")) {
        String normalizedText = Normalizer.normalize(originalText, Normalizer.Form.NFD)
                .replaceAll("\\p{InCombiningDiacriticalMarks}+", "").toLowerCase();
        int start = normalizedText.indexOf(search);
        if (start < 0) {
            return originalText;
        } else {/*w  w w.j ava2s.c om*/
            Spannable highlighted = new SpannableString(originalText);
            while (start >= 0) {
                int spanStart = Math.min(start, originalText.length());
                int spanEnd = Math.min(start + search.length(), originalText.length());
                highlighted.setSpan(new ForegroundColorSpan(Color.BLUE), spanStart, spanEnd,
                        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
                start = normalizedText.indexOf(search, spanEnd);
            }
            return highlighted;
        }
    }
    return originalText;
}

From source file:Main.java

/**
 * Method returning color between start and end color proportional to given values.
 *
 * @param colorStart start color//from w w w  .ja va  2 s.  c o  m
 * @param colorEnd   end color
 * @param fullValue  total value
 * @param partValue  part of fullValue. When partValue equals 0, returning color is colorStart,
 *                   when partValue is fullValue returning color is endColor. Otherwise returning
 *                   color is from between those, relative to partValue/fullValue ratio.
 * @return color from between start and end color relative to partValue/fullValue ratio.
 */
public static int getProportionalColor(int colorStart, int colorEnd, float fullValue, float partValue) {
    float progress = Math.min(Math.max(partValue, 0f), fullValue) / fullValue;
    return Color.argb(Math.round(Color.alpha(colorStart) * (1 - progress) + Color.alpha(colorEnd) * progress),
            Math.round(Color.red(colorStart) * (1 - progress) + Color.red(colorEnd) * progress),
            Math.round(Color.green(colorStart) * (1 - progress) + Color.green(colorEnd) * progress),
            Math.round(Color.blue(colorStart) * (1 - progress) + Color.blue(colorEnd) * progress));
}

From source file:Main.java

public static float getAnimatedFraction(ValueAnimator animator) {
    float fraction = ((float) animator.getCurrentPlayTime()) / animator.getDuration();
    fraction = Math.min(fraction, 1f);
    fraction = animator.getInterpolator().getInterpolation(fraction);
    return fraction;
}

From source file:Main.java

public static boolean pointAtELine(double x, double y, double x1, double y1, double x2, double y2) {
    double result = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1);
    if (result == 0) {
        return x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && y >= Math.min(y1, y2) && y <= Math.max(y1, y2);
    } else {//from  ww w.  ja  va  2s.c  o m
        return false;
    }
}

From source file:Main.java

/**
 * Helper method to calculate the proper size limit of a cache instance.
 *///  w  w w. j  a  va2s .com
public static long getCacheSizeInBytes(File dir, float cacheSizePercent, long maxSizeInBytes) {
    if (dir == null || (!dir.exists() && !dir.mkdir())) {
        return 0;
    }
    try {
        StatFs stat = new StatFs(dir.getPath());
        long totalBytes = stat.getBlockCountLong() * stat.getBlockSizeLong();
        long freeBytes = stat.getAvailableBlocksLong() * stat.getBlockSizeLong();
        long desiredBytes = Math.min((long) (totalBytes * cacheSizePercent), maxSizeInBytes);
        // If free space is less than desired, use half of the free disk space instead.
        desiredBytes = (desiredBytes > freeBytes) ? freeBytes / 2 : desiredBytes;
        return desiredBytes;
    } catch (IllegalArgumentException e) {
        return 0;
    }
}

From source file:Main.java

public static boolean pointAtELine(double x, double y, double x1, double y1, double x2, double y2) {
    double result = (x - x1) * (y2 - y1) - (y - y1) * (x2 - x1);
    if (result == 0) {
        if (x >= Math.min(x1, x2) && x <= Math.max(x1, x2) && y >= Math.min(y1, y2) && y <= Math.max(y1, y2)) {
            return true;
        } else {/*  w w w . j  a v a 2s  . c o  m*/
            return false;
        }
    } else {
        return false;
    }
}

From source file:Main.java

private static int findBestSampleSize(int width, int height, int desiredWidth, int desiredHeight) {
    double wr = (double) width / desiredWidth;
    double hr = (double) height / desiredHeight;
    double ratio = Math.min(wr, hr);
    float n = 1.0f;
    while ((n * 2) <= ratio) {
        n *= 2;/* w  w w.j av a 2  s .c  o m*/
    }
    return (int) n;
}

From source file:Main.java

public static <T> ArrayList<T> truncateList(List<? extends T> input, int maxSize) {
    if (input == null) {
        input = new ArrayList<T>();
    }/*  ww  w .j a v a  2 s  .  c  o m*/
    int endIndex = Math.min(input.size(), maxSize);
    ArrayList<T> result = new ArrayList<T>(endIndex);
    for (int i = 0; i < endIndex; i++) {
        result.add(input.get(i));
    }
    return result;
}

From source file:Main.java

/** Returns a Bitmap from the given URI that may be scaled by an integer factor to reduce its size,
 * while staying as least as large as the width and height parameters.
 *//*from  www.  ja v  a2s.  com*/
public static Bitmap scaledBitmapFromURIWithMinimumSize(Context context, Uri imageURI, int width, int height)
        throws FileNotFoundException {
    BitmapFactory.Options options = computeBitmapSizeFromURI(context, imageURI);
    options.inJustDecodeBounds = false;

    float wratio = 1.0f * options.outWidth / width;
    float hratio = 1.0f * options.outHeight / height;
    options.inSampleSize = (int) Math.min(wratio, hratio);

    return BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageURI), null, options);
}

From source file:Main.java

private static List<String> splitEqually(String text, int size) {
    List<String> ret = new ArrayList<String>((text.length() + size - 1) / size);
    for (int start = 0; start < text.length(); start += size) {
        ret.add(text.substring(start, Math.min(text.length(), start + size)));
    }/*from w  w  w  . ja  v  a  2 s  .co  m*/
    return ret;
}