2011-01-21 64 views
0

我正试图在onTouch事件的画布上重新绘制图像。我可以在画布上绘制图像,但我希望图像在特定的x,y处重绘。在触摸事件上重绘画布上的图像?

protected void onDraw(Canvas canvas) 

{ 

    mBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ab); 
mBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.ab); 
this.canvas=canvas; 

    Paint p = new Paint(); 
    p.setColor(Color.parseColor("#FFFFFF")); 

    canvas.drawLine(x1, y1, x2 , y2, p); 
    canvas.drawBitmap(mBitmap1, 70, 60, null); 
    canvas.drawBitmap(mBitmap1, 185, 60, null); 
} 


    @Override 
    public boolean onTouch(View v, MotionEvent event) { 
     final int x=(int)event.getX(); 
     Log.i("***********xPos","="+x); 
     final int y=(int)event.getY(); 
     Log.i("***********yPos","="+y); 

     if(event.getAction()==MotionEvent.ACTION_UP) 
     { 

     } 
     if(event.getAction()==MotionEvent.ACTION_DOWN) 
     { 
      canvas.drawBitmap(mBitmap1,50+x,60,null); 
      this.postInvalidate(); 

     } 
     if(event.getAction()==MotionEvent.ACTION_MOVE) 
     { 

     } 
     return false; 
    } 

回答

3

好吧我想我得到了什么,你的问题是你每次调用postinvalidate()方法时,action.down调用它最终调用ondraw()。它会重绘它在位图上的parilular设置值你再次提取


所以你看起来它保持不变。


确定遵循这个步骤:


1:使用一些公共变量用于绘制的OnDraw方法位图的x和y轴,让说INITX和inity这


2 :比触摸事件:通过将您的x和y值添加到initx和inity resp来更新此值。


like:initx = initx + x; inity = inity + y; 最后在阳离子下行事件中调用post.invalidate或ondraw方法。它会为你工作

0

请参阅MotionEvent类,该类提供用户触摸屏幕的位置的坐标。