get Best Size By Area - Android android.util

Android examples for android.util:Size

Description

get Best Size By Area

Demo Code


import android.content.Context;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.util.Log;
import android.view.OrientationEventListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class Main{
    public static Size getBestByArea(Size predicate, Collection<Size> sizes) {
        final int area = predicate.getArea();
        Size best = null;/*from   w w  w. j  av a2  s . co m*/
        int diff = Integer.MAX_VALUE;
        for (Size size : sizes) {
            if (size.getWidth() == predicate.getWidth()
                    || size.getHeight() == predicate.getHeight()
                    || size.getWidth() == predicate.getHeight()
                    || size.getHeight() == predicate.getWidth()) {
                return size;
            }
            int areaDiff = Math.abs(area - size.getArea());
            if (areaDiff < diff) {
                diff = areaDiff;
                best = size;
            }
        }
        return best;
    }
}

Related Tutorials