Android examples for android.util:Size
Given a range of sizes, find the one that is largest while fitting in the given width and height
//package com.java2s; import android.annotation.TargetApi; import android.graphics.Point; import android.hardware.Camera; import android.os.Build; import android.support.annotation.NonNull; import android.util.Log; import android.util.Size; import java.util.List; public class Main { public static final String TAG = "CameraUtils"; public static Camera.Size getBiggestSize(List<Camera.Size> sizes, int width, int height, boolean isSideways) { if (sizes == null || width <= 0 || height <= 0) { return null; }/*from w w w . j a v a 2 s.com*/ Point[] points = new Point[sizes.size()]; for (int i = 0; i < points.length; i++) { points[i] = new Point(sizes.get(i).width, sizes.get(i).height); } return sizes.get(getBiggestSizeIdx(points, width, height, isSideways)); } public static Camera.Size getBiggestSize(List<Camera.Size> sizes) { return getBiggestSize(sizes, Integer.MAX_VALUE, Integer.MAX_VALUE, false); } @TargetApi(21) public static Size getBiggestSize(Size[] sizes, int width, int height, boolean isSideways) { if (sizes == null || width <= 0 || height <= 0) { return null; } Point[] points = new Point[sizes.length]; for (int i = 0; i < points.length; i++) { points[i] = new Point(sizes[i].getWidth(), sizes[i].getHeight()); } return sizes[getBiggestSizeIdx(points, width, height, isSideways)]; } @TargetApi(21) public static Size getBiggestSize(Size[] sizes) { return getBiggestSize(sizes, Integer.MAX_VALUE, Integer.MAX_VALUE, false); } public static int getBiggestSizeIdx(@NonNull Point[] sizes, int width, int height, boolean isSideways) { if (sizes == null || width <= 0 || height <= 0) { return 0; } String model = Build.MODEL; if (model != null && model.equals("Nexus 4")) { width = height = Integer.MAX_VALUE; } if (isSideways) { int temp = width; width = height; height = temp; } int biggestScore = 0; int biggestIdx = 0; for (int i = 0; i < sizes.length; i++) { int score = sizes[i].x * sizes[i].y; if (sizes[i].x <= width && sizes[i].y <= height && score > biggestScore) { biggestScore = score; biggestIdx = i; } } return biggestIdx; } public static void log(String msg) { Log.v(TAG, msg); } }