2013-01-05 26 views
0

有谁知道如何循环画布的画布?我想稍后添加一个onTouch方法并获取X位置。谢谢。在Android中循环画布

@Override 
protected void onDraw(Canvas canvas) { 
    //Canvas canvas= new Canvas(); 
    xp1 = canvas.getWidth()/2; 
    Log.d("test1", "It went pass onDraw"); 
    xp2 = canvas.getWidth()/2; 
    yp1 = 25; 
    yp2 = 760; 
    canvas.drawColor(Color.BLACK); 
    canvas.drawBitmap(paddle1, xp1,yp1, null); 
    canvas.drawBitmap(paddle2,xp2,yp2, null); 
    Paint white = new Paint(); 
    white.setColor(Color.WHITE); 
    canvas.drawText("Score P1:"+ p1Score +" P2: " + p2Score , 700, 20,white); 
    Log.d("test1", "It's done with onDraw"); 

} 
+1

什么意思_loop_一个_canvas_? –

+0

就像继续画画布 –

回答

1

调用方法

invalidate(); 

,只要你想回调'onDraw'方法

aSampleCode(){ 
     image.setOnTouchListener(new View.OnTouchListener() { 
      @Override 
      public boolean onTouch(View v, MotionEvent event) { 

       switch(event.getAction()) 
       { 
       case MotionEvent.ACTION_MOVE: 

        x= event.getRawX() 

        break; 
       case MotionEvent.ACTION_UP: 
            invalidate(); 
        break; 
       case MotionEvent.ACTION_DOWN: 
        break; 

       default: 
        break; 
       } 
       return true; 
      } 
     }); 
    } 
} 
+0

即时得到空指针异常。 –

+0

异常日志? –