0

我正在开发一个在屏幕表面上绘图的程序。但我只能让它画点。我怎样才能让它连接点。我的意思是画一个形状而不是点。这是我的代码

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  /* super.onCreate(savedInstanceState);
   mySurfaceView = new MySurfaceView(this);
   setContentView(mySurfaceView);*/
    super.onCreate(savedInstanceState);
    init();
    mySurfaceView.setOnTouchListener(this);
    setContentView(mySurfaceView);
  }

private void init(){

   mySurfaceView = new MySurfaceView(this);
x = 0;
y = 0;

 }

   class MySurfaceView extends SurfaceView implements Runnable{

Thread thread = null;
SurfaceHolder surfaceHolder;
volatile boolean running = false;

private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Random random;

 public MySurfaceView(Context context) {
 super(context);
 // TODO Auto-generated constructor stub
 surfaceHolder = getHolder();
 random = new Random();
 } 
 @Override
 public void run() {
 // TODO Auto-generated method stub
 while(running){
  if(surfaceHolder.getSurface().isValid()){
 Canvas canvas = surfaceHolder.lockCanvas();
 //... actual drawing on canvas

 paint.setStyle(Paint.Style.STROKE);
 paint.setStrokeWidth(3);

 int w = canvas.getWidth();
 int h = canvas.getHeight();
//  int x = random.nextInt(w-1); 
// int y = random.nextInt(h-1);
 int r = random.nextInt(255);
 int g = random.nextInt(255);
 int b = random.nextInt(255);
 paint.setColor(0xff000000 + (r << 16) + (g << 8) + b);
 canvas.drawPoint(x, y, paint);

 surfaceHolder.unlockCanvasAndPost(canvas);
  }
 }
}

 }

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
   x = event.getX();
   y = event.getY();
return false;
}
}

我只是想让它绘制的不是点而是连接的线,以允许用户在屏幕上绘制

4

1 回答 1

0

您可以使用:

drawLine(oldX, oldY, x, y, paint);

也许您还需要检查用户是否将手指放在屏幕上或手指是否抬起。对于此检查:

http://developer.android.com/reference/android/view/MotionEvent.html

于 2013-08-29T11:35:24.640 回答