我想在用户触摸屏幕时将Rect绘制到SurfaceView上。当他们移动手指时,它会随之移动Rect。用户还可以调整Rect的大小。基本上它是一个选择工具。
到目前为止,我有以下代码:
private void selectPoints(){
//retrieve X and Y values from touch
surfaceView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent pos) {
int eventAction = pos.getAction();
switch (eventAction) {
case MotionEvent.ACTION_DOWN:
Log.d("X",String.valueOf(pos.getX()));
Log.d("Y",String.valueOf(pos.getY()));
break;
case MotionEvent.ACTION_UP:
Log.d("X",String.valueOf(pos.getX()));
Log.d("Y",String.valueOf(pos.getY()));
break;
}
return true;
}
});
}
但是我不确定如何绘制到SurfaceView上。有什么建议/帮助吗?
谢谢!
答案 0 :(得分:1)
从GLSurfaceView.Renderer接口实现渲染器,并为以下方法提供实现:
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
// TDOD
}
public void onSurfaceChanged(GL10 gl, int w, int h) {
// TDOD
}
public void onDrawFrame(GL10 gl) {
// You would draw here
}
你的surfaceView.requestRender()可以调用onDrawFrame()方法;
答案 1 :(得分:1)
知道了。我在Android中使用SurfaceView作为我的自定义相机。我不确定这是否会对你有所帮助。但这就是我的方式:
public class CameraPreview extends SurfaceView{
private SurfaceHolder mHolder;
private Camera mCamera;
public CameraPreview(Context context, Camera camera) {
super(context);
// Rest of the code for initialization
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
// set preview size and make any resize, rotate or
// reformatting changes here
// start preview with new settings
try {
mCamera.setPreviewDisplay(mHolder);
mCamera.startPreview();
} catch (Exception e){
Log.e("CameraPreview : surfaceChanged()", "Error starting camera preview: " + e.getMessage(),e);
}
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
try {
if (mCamera != null) {
mCamera.setPreviewDisplay(holder);
mCamera.startPreview();
}
} catch (IOException e) {
Log.e("CameraPreview : surfaceCreated()", "Error setting camera preview: " + e.getMessage(), e);
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
然后在我的活动中,我按如下方式初始化SurfaceView:
cameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
查看这段代码中是否有任何有用的摘录。