当前位置: 代码迷 >> Android >> surfaceview画线解决思路
  详细解决方案

surfaceview画线解决思路

热度:48   发布时间:2016-04-28 04:40:53.0
surfaceview画线
最近在做你画我猜。。。用手指画画,使用view没问题 但是使用surfaceview有问题,图像好像有两层,而且线断断续续的。
代码如下
package com.DrawAndGuess;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class DrawAndGuessView extends SurfaceView {
   
/** Called when the activity is first created. */
private float startX;
private float startY;

    private Canvas canvas;
    private Paint paint;
    private SurfaceHolder holder;
    
    public DrawAndGuessView(Context context) {
super(context);
// TODO Auto-generated constructor stub
holder = this.getHolder();
paint = new Paint();
paint.setColor(Color.RED);

}
    
    public void drawLine(float startX2,float startY2,float endX,float endY)
    {
     canvas = this.holder.lockCanvas();
     canvas.drawLine(startX2, startY2, endX, endY, paint);
     this.holder.unlockCanvasAndPost(this.canvas);
    }
    
    public boolean onTouchEvent(MotionEvent event)
    {
       if (event.getAction() == MotionEvent.ACTION_MOVE) {    //拖动屏幕  
              drawLine(startX, startY, event.getX(), event.getY());   //画线,x,y是上次的坐标,event.getX(), event.getY()是当前坐标  
              invalidate();  
          }  
    
          if (event.getAction() == MotionEvent.ACTION_DOWN) {    //按下屏幕  
              startX = event.getX();                 
              startY = event.getY();  
              
              canvas = this.holder.lockCanvas();
              canvas.drawPoint(startX, startY, paint);                //画点  
              this.holder.unlockCanvasAndPost(this.canvas);
              invalidate();  
          }  
          startX = event.getX();   //记录坐标  
          startY = event.getY();  
          return true;  
    
    }
}
------解决方案--------------------

@Override
public void surfaceCreated(SurfaceHolder holder)
{
// 锁定整个SurfaceView
Canvas canvas = holder.lockCanvas();
// 绘制背景
Bitmap back = BitmapFactory.decodeResource(
SurfaceViewTest.this.getResources(), R.drawable.sun);
//绘制背景
canvas.drawBitmap(back, 0, 0, null);
// 绘制完成,释放画布,提交修改
holder.unlockCanvasAndPost(canvas);
//重新锁一次,"持久化"上次所绘制的内容
holder.lockCanvas(new Rect(0, 0, 0, 0));
holder.unlockCanvasAndPost(canvas);
}

希望这段代码对你有过帮助
------解决方案--------------------
我的SurfaceView用法:
public class HelloSurfaceView extends SurfaceView implements
SurfaceHolder.Callback {
    private SurfaceHolder helloSurfaceHolder = null;
    public HelloSurfaceView(Context context) {
super(context);
// 实例化SurfaceHolder
helloSurfaceHolder = this.getHolder();
// 添加回调
helloSurfaceHolder.addCallback(this);
    }
    private void draw() {
synchronized (helloSurfaceHolder) {
Canvas canvas = null;
if (null != (canvas = helloSurfaceHolder.lockCanvas())) {
canvas.drawBitmap(bitmap, rectData,
rectDisp, null);
clickSurfaceHolder.unlockCanvasAndPost(canvas);
}