2015-09-19 60 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