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 如何构建全国统一的物流管理平台?
  • ¥100 ijkplayer使用AndroidStudio/CMake编译,如何支持 rtsp 直播流?
  • ¥20 和学习数据的传参方式,选择正确的传参方式有关
  • ¥15 这是网络安全里面的poem code
  • ¥15 用js遍历数据并对非空元素添加css样式
  • ¥15 使用autodl云训练,希望有直接运行的代码(关键词-数据集)
  • ¥50 python写segy数据出错
  • ¥20 关于线性结构的问题:希望能从头到尾完整地帮我改一下,困扰我很久了
  • ¥30 3D多模态医疗数据集-视觉问答
  • ¥20 设计一个二极管稳压值检测电路