Fix issue when fullscreen
[rtmpclient.git] / app / src / main / java / ai / suanzi / rtmpclient / CameraView.java
index ab3e145..e4bc71c 100644 (file)
@@ -1,27 +1,73 @@
 package ai.suanzi.rtmpclient;
 
-import android.os.Parcelable;
-import android.view.SurfaceView;
-import android.os.Parcel;
 import android.content.Context;
 import android.view.SurfaceHolder;
+import android.view.SurfaceView;
+import android.widget.FrameLayout;
+
+import org.apache.log4j.Logger;
 
+public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
 
-public class CameraView extends SurfaceView implements Parcelable {
+    private static Logger gLogger = Logger.getLogger("CameraView");
+    public static int SCREEN_WIDTH;
+    public static int SCREEN_HEIGHT;
 
+    private Context mContext;
     private SurfaceHolder mHolder;
+    private Callback mCallback;
+    private int width;
+    private int height;
 
-    public CameraView(Context context) {
+    public CameraView (Context context, Callback cb) {
         super(context);
-        mHolder = getHolder();
+        this.mContext = context;
+        mHolder = this.getHolder();
+        mHolder.addCallback(this);
+        this.mCallback = cb;
+    }
+
+    public void setLayout(int width, int heitht){
+        this.height = heitht;
+        this.width = width;
+        gLogger.error("setLayout, 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;
+        }
+        gLogger.error("width=" + lp.width + ", height=" + lp.height + ". leftMargin=" + lp.leftMargin + ", topMargin=" + lp.topMargin);
+        this.setLayoutParams(lp);
     }
 
+    // SurfaceHolder.Callback implementation
     @Override
-    public int describeContents() {
-        return 0;
+    public void surfaceCreated(final SurfaceHolder holder){
+        gLogger.debug("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
     }
 
     @Override
-    public void writeToParcel(Parcel dest, int flags) {
+    public void surfaceChanged(SurfaceHolder holder, int format, int widht, int height){
+        gLogger.debug("surfaceChanged");
+        mHolder = holder;
+        mCallback.onSurfaceChanged(holder, format, widht, height);
+    }
+
+    @Override
+    public void surfaceDestroyed(SurfaceHolder holder){ gLogger.debug("surfaceDestroyed");
+    }
+
+    public interface Callback {
+        void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height);
     }
 }