resize the preview size as the ratio of picture size
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / CameraView.java
index 5ddcb5e..71a684d 100644 (file)
@@ -3,6 +3,7 @@ package ai.suanzi.rtmpclient;
 import android.content.Context;
 import android.view.SurfaceHolder;
 import android.view.SurfaceView;
+import android.widget.FrameLayout;
 
 import org.apache.log4j.Logger;
 
@@ -15,6 +16,8 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
     private Context mContext;
     private SurfaceHolder mHolder;
     private Callback mCallback;
+    private int width;
+    private int height;
 
     public CameraView (Context context, Callback cb) {
         super(context);
@@ -24,10 +27,32 @@ public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
         this.mCallback = cb;
     }
 
+    public void setLayout(int width, int heitht){
+        this.height = heitht;
+        this.width = width;
+        gLogger.error("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
+
+        float ratio = (float) height / width; // 3/4,,,  640x480  1008x1344
+        FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)getLayoutParams();
+        gLogger.error("ratio = " + ratio + ". xxx=" + (float)(SCREEN_HEIGHT / SCREEN_HEIGHT));
+        if(ratio > (float)(SCREEN_HEIGHT / SCREEN_WIDTH) ){
+            lp.width = SCREEN_HEIGHT * width / height;
+            lp.height = SCREEN_HEIGHT;
+            lp.leftMargin = (SCREEN_WIDTH - lp.width ) / 2;
+            lp.topMargin = 0;
+        } else {
+            lp.width = SCREEN_WIDTH;
+            lp.height = SCREEN_WIDTH * height / width;
+            lp.leftMargin = 0;
+            lp.topMargin = (SCREEN_HEIGHT - lp.height) / 2;
+        }
+        this.setLayoutParams(lp);
+    }
+
     // SurfaceHolder.Callback implementation
     @Override
     public void surfaceCreated(final SurfaceHolder holder){
-        gLogger.error("SurfacedCreated");
+        gLogger.error("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
     }
 
     @Override