gpt4 book ai didi

java - 无法在 fragment 中创建surfaceView

转载 作者:行者123 更新时间:2023-12-01 11:08:24 25 4
gpt4 key购买 nike

有人可以帮我在 fragment 中创建 SurfaceView 吗?下面是我的代码。它总是停在第三行: if (!surfaceHolder.getSurface().isValid()) 我不知道为什么。

fragment 代码:

<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()注册回调接口(interface)。当表面可用、被破坏以及发生变化时,它会通知您。

关于java - 无法在 fragment 中创建surfaceView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32673734/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com