Android在手机屏幕上绘制

时间:2013-08-29 11:19:00

标签: android

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

 /** 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;
}
}

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

1 个答案:

答案 0 :(得分:0)

您可以使用:

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

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

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