get Best Surface View Size - Android android.hardware

Android examples for android.hardware:Camera Size

Description

get Best Surface View Size

Demo Code

import android.graphics.Point;
import android.hardware.Camera.Size;
import android.view.SurfaceView;

public class Main {

  public static Point getBestSurfaceViewSize(SurfaceView preview, Size psize) {
    Point bestSize = new Point(0, 0);
    if (preview.getWidth() < preview.getHeight()) {
      // portrait
      int cw = Math.min(psize.height, psize.width);
      int ch = Math.max(psize.height, psize.width);
      float aspect = (float) cw / ch;
      if (aspect < 0.0F) {
        bestSize.x = Math.round(preview.getHeight() * aspect);
        bestSize.y = preview.getHeight();
      } else {//from   w  w  w  .  java2 s.c  o  m
        bestSize.x = preview.getWidth();
        bestSize.y = Math.round(preview.getWidth() / aspect);
      }
    } else {
      // landscape
      int cw = Math.max(psize.height, psize.width);
      int ch = Math.min(psize.height, psize.width);
      float aspect = (float) cw / ch;
      if (aspect < 0.0F) {
        bestSize.x = preview.getWidth();
        bestSize.y = Math.round(preview.getWidth() / aspect);
      } else {
        bestSize.x = Math.round(preview.getHeight() * aspect);
        bestSize.y = preview.getHeight();
      }
    }
    return bestSize;
  }

}

Related Tutorials