安卓(Android) 怎么查找最近的预览尺寸

2019年10月27日22:36:51安卓(Android) 怎么查找最近的预览尺寸已关闭评论 245 931字阅读3分6秒

1.简介

安卓(Android) 怎么查找最近的预览尺寸?——请看下文。

2.示例代码

/**
 * 版权所有 编程十万个怎么办(www.tah1986.com)
 */
import java.util.List;

import android.graphics.Point;
import android.hardware.Camera;
import android.util.Log;

public class Main {
  private static final String TAG = "";

  public static Camera.Size findClosetPreviewSize(Camera camera,
      Point preferSize) {
    int preferX = preferSize.x;
    int preferY = preferSize.y;
    Camera.Parameters parameters = camera.getParameters();
    List<Camera.Size> allSupportSizes = parameters.getSupportedPreviewSizes();

    int minDiff = Integer.MAX_VALUE;
    int index = 0;
    for (int i = 0; i < allSupportSizes.size(); i++) {
      Camera.Size size = allSupportSizes.get(i);
      int x = size.width;
      int y = size.height;

      int diff = Math.abs(x - preferX) + Math.abs(y - preferY);
      if (diff < minDiff) {
        minDiff = diff;
        index = i;
      }
    }

    Camera.Size size = allSupportSizes.get(index);
    return size;
  }

  private static String dumpPreviewSizeList(List<Camera.Size> sizes) {
    String result = "";
    for (Camera.Size size : sizes) {
      result += "(" + size.width + "," + size.height + ") ";
    }
    return result;
  }
}

 
继续阅读
编程十万个怎么办