1 package ai.suanzi.rtmpclient;
3 import android.content.Context;
4 import android.view.SurfaceHolder;
5 import android.view.SurfaceView;
6 import android.widget.FrameLayout;
8 import org.apache.log4j.Logger;
10 public class CameraView extends SurfaceView implements SurfaceHolder.Callback {
12 private static Logger gLogger = Logger.getLogger("CameraView");
13 public static int SCREEN_WIDTH;
14 public static int SCREEN_HEIGHT;
16 private Context mContext;
17 private SurfaceHolder mHolder;
18 private Callback mCallback;
22 public CameraView (Context context, Callback cb) {
24 this.mContext = context;
25 mHolder = this.getHolder();
26 mHolder.addCallback(this);
30 public void setLayout(int width, int heitht){
33 gLogger.error("setLayout, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
35 float ratio = (float) height / width; // 3/4,,, 640x480 1008x1344
36 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams)getLayoutParams();
37 gLogger.error("ratio = " + ratio + ". xxx=" + (float)(SCREEN_HEIGHT / SCREEN_HEIGHT));
38 if(ratio > (float)(SCREEN_HEIGHT / SCREEN_WIDTH) ){
39 lp.width = SCREEN_HEIGHT * width / height;
40 lp.height = SCREEN_HEIGHT;
41 lp.leftMargin = (SCREEN_WIDTH - lp.width ) / 2;
44 lp.width = SCREEN_WIDTH;
45 lp.height = SCREEN_WIDTH * height / width;
47 lp.topMargin = (SCREEN_HEIGHT - lp.height) / 2;
49 gLogger.error("width=" + lp.width + ", height=" + lp.height + ". leftMargin=" + lp.leftMargin + ", topMargin=" + lp.topMargin);
50 this.setLayoutParams(lp);
53 // SurfaceHolder.Callback implementation
55 public void surfaceCreated(final SurfaceHolder holder){
56 gLogger.debug("SurfacedCreated, screen size w: " + SCREEN_WIDTH + ", h:" + SCREEN_HEIGHT + ". preview size, w: " + width + ", h: " + height);
60 public void surfaceChanged(SurfaceHolder holder, int format, int widht, int height){
61 gLogger.debug("surfaceChanged");
63 mCallback.onSurfaceChanged(holder, format, widht, height);
67 public void surfaceDestroyed(SurfaceHolder holder){ gLogger.debug("surfaceDestroyed");
70 public interface Callback {
71 void onSurfaceChanged(SurfaceHolder holder, int format, int width, int height);