有人可以帮我在片段中创建SurfaceView吗?以下是我的代码。它始终停在thr行: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>
感谢您的帮助。
答案 0 :(得分:1)
使用SurfaceHolder.addCallback()
注册回调接口。当表面可用,被破坏以及何时发生变化时,它会通知您。
答案 1 :(得分:0)
我在帖子中看不到任何引用XML声明的SurfaceView或以编程方式将SurfaceView添加到布局中的代码。您需要执行其中任何一项,否则无法绘制SurfaceView。
要以编程方式添加,请在onCreateView(...)
中执行:
mCameraView = new CameraView(...);
ViewGroup myRootLayout = (ViewGroup)findViewById(...)
myRootLayout.addView(mCameraView);
答案 2 :(得分:0)
我的onCreateView功能看起来:
Default Web Site
我可以毫无问题地创建和添加textView。但如果我试图添加cameraView应用程序崩溃。为什么?我在onActivityCreated函数中创建了cameraView