有人可以帮助我在片段中创建SurfaceView吗?下面是我的代码。它总是停在三号线:if(!surfaceHolder.getSurface()。isValid())而且我不知道为什么。

片段代码:

<pre>
public class FirstActivity extends Fragment/* implements OnTouchListener*/ {

    CameraView cameraView;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        /* instantiating the surface view */
        cameraView = new CameraView(this.getActivity());
        /* setting the listener - this, because it is defined within the activity */
//      cameraView.setOnTouchListener(this);

    }

//  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//      View v = inflater.inflate(R.layout.lin, null);
//
//      cameraView = (CameraView) v.findViewById(R.id.cameraView);
//
//      return v;
//
//  }
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return new CameraView(getActivity());
    }

//  @Override
//  public boolean onTouch(View arg0, MotionEvent arg1) {
//      // TODO Auto-generated method stub
//      return false;
//  }

    @Override
    public void onStart() {
        super.onStart();

    }

    @Override
    public void onResume() {
        super.onResume();
        cameraView.onResumeCameraView();
    }

    @Override
    public void onPause() {
        super.onPause();
        cameraView.onPauseCameraView();
    }

}
</pre>


CameraView代码:

<pre>
public class CameraView extends SurfaceView implements Runnable {
    Thread thread = null;

    SurfaceHolder surfaceHolder;
    private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
    Bitmap bitmap;

    int WIDTH = 320;
    int HEIGHT = 240;

    volatile boolean running = false;


    public CameraView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        surfaceHolder = getHolder();
        bitmap = Bitmap.createBitmap(WIDTH, HEIGHT, Bitmap.Config.ARGB_8888/*Bitmap.Config.ALPHA_8*//*Bitmap.Config.RGB_565*/);
        Log.d("S3", "stworzono bitmape");
    }

    public void onResumeCameraView() {
        running = true;
        thread = new Thread(this);
        thread.start();
    }

    public void onPauseCameraView() {
        boolean retry = true;
        running = false;
        while (retry) {
            try {
                thread.join();
                retry = false;
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        while (running) {

            if (!surfaceHolder.getSurface().isValid()) {
                Log.d("S3", "blad");
                continue;
            }

            Log.d("S3", "dalej");
            Canvas canvas = surfaceHolder.lockCanvas();

            canvas.drawColor(Color.WHITE);
            paint.setColor(Color.RED);
            canvas.drawRect(0, 0, 100, 100, paint);
            surfaceHolder.unlockCanvasAndPost(canvas);

        }
    }
}
</pre>


谢谢你的帮助。

最佳答案

使用SurfaceHolder.addCallback()注册回调接口。当表面可用,被破坏以及表面发生变化时,它将通知您。

10-08 08:13