Android中surfaceView画直线断断续续

使用surfaceView做了一个画画板的练习,可是画出来的线断断续续,是为什么?
下面是mainactivity
package com.wxh.mywall;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class MainActivity extends Activity {

private SurfaceView mSurfaceView = null;       
private SurfaceHolder mSurfaceHolder = null;       
private Button cleanButton = null;       
private Button colorButton = null; 

private float oldX = 0f;       
private float oldY = 0f;

private boolean canDraw = false;      
private Paint mPaint = null; 

//用来记录当前是哪一种颜色       
private int whichColor = 0;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mSurfaceView = (SurfaceView)this.findViewById(R.id.surfaceview);      
    mSurfaceHolder = mSurfaceView.getHolder();      

    mPaint = new Paint();       
    //画笔的颜色       
    mPaint.setColor(Color.RED);       
    //画笔的粗细       
    mPaint.setStrokeWidth(2.0f);      
    cleanButton = (Button)this.findViewById(R.id.flushbutton);

    //按钮监听       
    cleanButton.setOnClickListener(new View.OnClickListener()        
    {       
        @Override       
        public void onClick(View v)        
        {       
        //锁定整个SurfaceView  

        Canvas mCanvas = mSurfaceHolder.lockCanvas();       
        mCanvas.drawColor(Color.BLACK);       
        //绘制完成,提交修改      
        mSurfaceHolder.unlockCanvasAndPost(mCanvas);       
        //重新锁一次       
        mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));       
        mSurfaceHolder.unlockCanvasAndPost(mCanvas);       
        }      
    }); 
    colorButton = (Button)this.findViewById(R.id.colorbutton);  

    //按钮监听       
    colorButton.setOnClickListener(new View.OnClickListener()        
    {     
    @Override       
    public void onClick(View v)       
    {       
     // TODO Auto-generated method stub      

    Dialog mDialog = new AlertDialog.Builder(MainActivity.this)       
    .setTitle("颜色设置")       
    .setSingleChoiceItems(new String[]{"红色","绿色","蓝色"}, whichColor, new DialogInterface.OnClickListener()        
    {       
    @Override      
    public void onClick(DialogInterface dialog, int which)        
    {

     // TODO Auto-generated method stub       
        switch(which)       
        {       
            case 0:       
            {       
                //画笔的颜色       
                mPaint.setColor(Color.RED);       
                whichColor = 0;       
                break;       
            }       
            case 1:       
            {       
                //画笔的颜色      
                mPaint.setColor(Color.GREEN);      
                whichColor = 1;      
                break;      
            }      
            case 2:      
            {      
                //画笔的颜色106                                 
                mPaint.setColor(Color.BLUE);      
                whichColor = 2;                                 
                break;                                  
            }      
        }      
    }      
    })      
    .setPositiveButton("确定", new DialogInterface.OnClickListener()      
    {      
        @Override      
        public void onClick(DialogInterface dialog, int which)       
        {      
            // TODO Auto-generated method stub      
            dialog.dismiss();      
        }      
    })      
    .create();      
    mDialog.show();      
    }      
    });  

}       
    @Override      
    public boolean onTouchEvent(MotionEvent event)      
    {            
        //获取x坐标      
        float x = event.getX();      
        //获取y坐标(不知道为什么要减去两个偏移值才对得准屏幕)      
        float y = event.getY()-100;      
        //第一次进来先不管      
        if(canDraw)      
        {           
            //获取触屏事件      
            switch(event.getAction())      
            {      
                //如果是拖动事件      
                case MotionEvent.ACTION_MOVE:      
                {      
                    //锁定整个SurfaceView      
                    Canvas mCanvas = mSurfaceHolder.lockCanvas();          
                    mCanvas.drawLine(x, y, oldX, oldY, mPaint);      
                    mSurfaceHolder.unlockCanvasAndPost(mCanvas);      
                    //重新锁一次      
                    mSurfaceHolder.lockCanvas(new Rect(0, 0, 0, 0));      
                    mSurfaceHolder.unlockCanvasAndPost(mCanvas);      
                    break;      
                }      
            }      
        }      
        //保存目前的x坐标值      
        oldX = x;      
        //保存目前的y坐标值      
        oldY = y;      

        canDraw = true;      

        return true;      
    }      

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}


2个回答

SurfaceView是双缓冲机制,有front和back,这两个交替显示,每post一次交替一次
所以你的代码是交替画在两个画布上的,多画几次,确保每个上面都画上了
//获取触屏事件

switch(event.getAction())

{

//如果是拖动事件

case MotionEvent.ACTION_MOVE:

{

//锁定整个SurfaceView

Canvas mCanvas = mSurfaceHolder.lockCanvas();

mCanvas.drawLine(x, y, oldX, oldY, mPaint);

mSurfaceHolder.unlockCanvasAndPost(mCanvas);

//重新锁一次

mCanvas = mSurfaceHolder.lockCanvas();

mCanvas.drawLine(x, y, oldX, oldY, mPaint);

mSurfaceHolder.unlockCanvasAndPost(mCanvas);

                mCanvas = mSurfaceHolder.lockCanvas();          
                mCanvas.drawLine(x, y, oldX, oldY, mPaint);      
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);

                mCanvas = mSurfaceHolder.lockCanvas();          
                mCanvas.drawLine(x, y, oldX, oldY, mPaint);      
                mSurfaceHolder.unlockCanvasAndPost(mCanvas);
                break;      
            }      
        }

再把画笔该粗点,效果会好很多
//画笔的粗细

mPaint.setStrokeWidth(6.0f);

    ![图片说明](https://img-ask.csdn.net/upload/201706/06/1496723048_402721.png)

图片说明

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问