ITKeyword,专注技术干货聚合推荐

注册 | 登录

How to draw on a SurfaceView in Android

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

推荐:android Draw Rect 坐标图示

前两天在博客发了在例子 android Canvas类介绍  http://byandby.iteye.com/blog/825330  建议大家 点进去 看一看 不然下边没办法 继续啊。 我还是把这个例子的代

I'm wanting to draw a Rect onto a SurfaceView when a user touches the screen. When they move their finger, it moves the Rect along with it. The user can also size the Rect. Basically it's a selection tool.

So far I have the following code:

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;                        
        }
    });     
}

However I'm unsure of how to draw onto the SurfaceView. Any advice/help?

Thank you!

android draw surfaceview rect

|
  this question
asked Jul 3 '13 at 1:01 LKB 360 1 12 32


 | 

2 Answers
2

Implement a renderer from GLSurfaceView.Renderer interface and provide implementation for the following methods:

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
}

The onDrawFrame() method can be called by your surfaceView.requestRender();

推荐:SurfaceView的LockCanvas(Rect rect)的方法总结

搜到: 在刚开始学习SurfaceView的时候,关注它的原因其中一个也是因为能够按区域刷新,不用像View一样整个屏幕全部重画。但当时在测试后的时候发现了问题。发现


|
  this answer
answered Jul 3 '13 at 1:09 Raj 316 1 6 21
    
Thanks for your response @Raj. I won't be using GLSurfaceView though as I do not need to draw 3D images. I believe I can achieve my goal using SurfaceView and the OnDraw() method... it's just about HOW to accomplish this. –  LKB Jul 3 '13 at 2:12


 | 

Got it. I am using SurfaceView for my custom camera in Android. I am not sure whether this will help you or not. But this is how I do it:

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) {
  }

Then in my activity I initialize the SurfaceView as follows:

cameraPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);

See if there is any useful extract from this piece of code.


|
  this answer
answered Jul 3 '13 at 15:10 Raj 316 1 6 21


 | 

推荐:Android Canvas类介绍和Android Draw Rect 坐标图示

       当我们调整好画笔之后,现在需要绘制到画布上,这就得用Canvas类了。在Android中既然把Canvas当做画布,那么就可以在画布上绘制我们想要的任何东西。除了


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。