hoho_12 2017-06-03 05:12 采纳率: 89.3%
浏览 2137
已采纳

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条回答 默认 最新

  • Tauren2614 2017-06-06 04:24
    关注

    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)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 ansys fluent计算闪退
  • ¥15 有关wireshark抓包的问题
  • ¥15 需要写计算过程,不要写代码,求解答,数据都在图上
  • ¥15 向数据表用newid方式插入GUID问题
  • ¥15 multisim电路设计
  • ¥20 用keil,写代码解决两个问题,用库函数
  • ¥50 ID中开关量采样信号通道、以及程序流程的设计
  • ¥15 U-Mamba/nnunetv2固定随机数种子
  • ¥15 vba使用jmail发送邮件正文里面怎么加图片
  • ¥15 vb6.0如何向数据库中添加自动生成的字段数据。