0

当用户触摸屏幕时,我想在 SurfaceView 上绘制一个 Rect 。当他们移动手指时,它会随之移动 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 上绘图。有什么建议/帮助吗?

谢谢!

4

2 回答 2

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() 方法;

于 2013-07-03T01:09:45.367 回答
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);

看看这段代码是否有任何有用的摘录。

于 2013-07-03T15:10:18.227 回答