Android 在触摸屏上重画画布上的图像事件?
我正在尝试在Android 在触摸屏上重画画布上的图像事件?,android,android-layout,Android,Android Layout,我正在尝试在onTouch事件的画布上重新绘制图像。我可以在画布上绘制图像,但我希望图像在特定的x,y处重新绘制 protected void onDraw(Canvas canvas) { mBitmap1 = BitmapFactory.decodeResource(getResources(), R.drawable.ab); mBitmap2 = BitmapFactory.decodeResource(getResources(), R.drawable.ab)
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;
}
请参阅MotionEvent类,该类提供用户触摸屏幕的坐标。请参阅MotionEvent类,该类提供用户触摸屏幕的坐标。我想我理解您的问题。每次调用
action.down
时,您都在调用postinvalidate()
方法,因此它最终调用ondraw()
。所以它将在位图上重新绘制它,以获得您再次输入ondraw的特定设置值
所以看起来它保持不变
遵循以下步骤:
like:initx=initx+x;
inity=inity+y;
最后一个事件是调用
post.invalidate
或ondraw
方法。我想我理解你的问题。每次调用action.down
时,您都在调用postinvalidate()
方法,因此它最终调用ondraw()
。所以它将在位图上重新绘制它,以获得您再次输入ondraw的特定设置值
所以看起来它保持不变
遵循以下步骤:
like:initx=initx+x;
inity=inity+y;
post.invalidate
或ondraw
方法