2015-09-19 59 views
0

有人可以幫助我在片段中創建SurfaceView嗎?以下是我的代碼。它總是停在thr行:if(!surfaceHolder.getSurface()。isValid()),我不知道爲什麼。無法在片段中創建surfaceView

片段代碼:

<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> 

感謝您的幫助。

+2

如果通過「停止」,你的意思是你的應用程序崩潰,使用LogCat來檢查與您的崩潰相關的Java堆棧跟蹤:https://stackoverflow.com/questions/23353173/很抱歉-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

不,停止,我的意思是說,surfaceHolder.getSurface()。isValid()總是返回false。所以它停止在這個'如果'。 – user5354725

回答

1

使用SurfaceHolder.addCallback()註冊一個回調接口。當表面可用時,它會通知你,銷燬以及它何時發生變化。

0

我的帖子中沒有看到任何代碼引用在XML中聲明的SurfaceView或以編程方式將SurfaceView添加到佈局。你需要做那些,或者不能繪製SurfaceView。

要progammatically增加,在onCreateView(...),做到:

mCameraView = new CameraView(...); 
ViewGroup myRootLayout = (ViewGroup)findViewById(...) 
myRootLayout.addView(mCameraView); 
+0

謝謝你的回答。你能告訴我我加了什麼,因爲我是java和android的初學者。 – user5354725

+0

我加了一些解釋。 SurfaceView是一個視圖。它需要像所有其他視圖一樣添加到XML中。用XML聲明它,或按照我在答案中顯示的內容並以編程方式添加它。可能還有其他的錯誤,因爲它是你自己的自定義類擴展了框架的SurfaceView,但這是一般的想法。如果持續存在問題,請簡化問題並學習如何將基本SurfaceView添加到佈局,然後將其擴展到您自己的類中並以相同方式添加。 –

+0

你能告訴我我寫的代碼有什麼問題嗎?我仍在嘗試,但它不起作用:/ XML文件應該是什麼? – user5354725

0

我onCreateView函數如下:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     RelativeLayout relativeLayout = new RelativeLayout(this.getActivity()); 
     TextView textView = new TextView(this.getActivity()); 
     textView.setText("Simply dummy text"); 

     relativeLayout.addView(textView); 
     relativeLayout.addView(cameraView); 

     return relativeLayout; 
} 

我可以創建並沒有任何問題添加TextView的。但如果我試圖添加cameraView應用程序崩潰。爲什麼?我在創建cameraView功能onActivityCreated