Camera预览尺寸和获取屏幕宽高。

算法需要将预览大小与手机的分辨率挂钩,才能够在不同分辨率的手机上面预览都不变形。


/**
 * 获取最佳预览大小
 * @param parameters 相机参数
 * @param screenResolution 屏幕宽高
 * @return
 */
private Point getBestCameraResolution(Camera.Parameters parameters, Point screenResolution) {
    float tmp = 0f;
    float mindiff = 100f;
    float x_d_y = (float) screenResolution.x / (float) screenResolution.y;
    Size best = null;
    List supportedPreviewSizes = parameters.getSupportedPreviewSizes();
    for (Size s : supportedPreviewSizes) {
        tmp = Math.abs(((float) s.height / (float) s.width) - x_d_y);
        if (tmp < mindiff) {
            mindiff = tmp;
            best = s;
        }
    }
    return new Point(best.width, best.height);
}


获取屏幕宽高的方法如下:

/**
 * 获取屏幕宽度和高度,单位为px
 * @param context
 * @return
 */
public static Point getScreenMetrics(Context context){
   DisplayMetrics dm =context.getResources().getDisplayMetrics();
   int w_screen = dm.widthPixels;
   int h_screen = dm.heightPixels;
   return new Point(w_screen, h_screen);
   
}




你可能感兴趣的:(Android,学习)