2013-08-29 337 views
0

我正在開發一個繪製屏幕表面的程序,但我只能讓它畫點。我怎樣才能使它連接dots.I意味着繪製形狀不dots.Here是我的代碼手機屏幕上的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; 
} 
} 

我只想把它畫不點,但連接線,以允許用戶在屏幕上畫

+2

的'Canvas'類也有一個['drawLine'方法](http://developer.android.com/reference/android/graphics/Canvas.html) – Michael

回答