问题描述
我想重新绘制图像上的 onTouch
事件在画布上。我能够绘制图像在画布上,但我希望图像重绘在一个特定的 X,Y
。
保护无效的OnDraw(帆布油画)
{
mBitmap1 = BitmapFactory.de codeResource(getResources(),R.drawable.ab);
mBitmap2 = BitmapFactory.de codeResource(getResources(),R.drawable.ab);
this.canvas =帆布;
涂料P =新的油漆();
p.setColor(Color.parseColor(#FFFFFF));
canvas.drawLine(X1,Y1,X2,Y2,p)的;
canvas.drawBitmap(mBitmap1,70,60,NULL);
canvas.drawBitmap(mBitmap1,185,60,NULL);
}
@覆盖
公共布尔onTouch(视图V,MotionEvent事件){
最终诠释X =(INT)event.getX();
Log.i(*********** XPOS,=+ x)的;
最终诠释Y =(INT)event.getY();
Log.i(*********** yPos,=+ y)基
如果(event.getAction()== MotionEvent.ACTION_UP)
{
}
如果(event.getAction()== MotionEvent.ACTION_DOWN)
{
canvas.drawBitmap(mBitmap1,50 + X,60,NULL);
this.postInvalidate();
}
如果(event.getAction()== MotionEvent.ACTION_MOVE)
{
}
返回false;
}
确定我想我得到了什么乌尔概率is.you AE每次当action.down称为所以调用最终调用OnDraw的时间(调用postinvalidate()方法)。 okso将重新绘制它的位图上,你投入的OnDraw再次paricaular设定值不变
让你看起来谓也保持不变。
确定按照此步骤:
1:使用一些公共变量在X和Y轴的OnDraw方法绘制位图,可以说INITX和inity这
2:不是触摸事件:通过添加x和y的值INITX和更新此值inity这RESP
这样的:INITX = INITX + X;inity这= inity这+ Y;和上次在阳离子关闭事件只是叫post.invalidate或OnDraw的方法。它会为你工作。
I am trying to redraw an image on the canvas on an onTouch
event. I am able to draw the image on the canvas, but I want the image to redraw at a particular 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;
}
ok i think i got whats ur prob is.you ae calling postinvalidate() method each time when action.down called so it calls ultimately call ondraw().okso it will redraw it on bitmap for paricaular setted value at which you put in ondraw again
so you looks tht it remain unchanged.
ok follow this steps:
1:use some public variables for drawing bitmaps in ondraw method for x and y axis,lets say initx and inity
2:than on touch event:update this value by adding your x and y value to initx and inity resp.
like:initx=initx+x;inity=inity+y;and last in cation down event just call post.invalidate or ondraw method. it will work for you
这篇关于重绘上触摸事件画布上的形象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!