Reputation: 5971
I was playing around drawing some ovals in response to touch, I've got confused by the coordinates given by touch and translating it into some oval, I dont know why I got offset in Y, here an image that illustrate my problem, piece of code and some ouptut.
public class TestView extends View implements OnTouchListener
{
RectF rectf;
Paint paint;
public TestView(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
paint = new Paint();
rectf = new RectF();
rectf.set(30, 80, 80, 30);
this.setOnTouchListener(this);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.save();
canvas.drawOval(rectf, paint);
canvas.restore();
}
@Override
public boolean onTouch(View v, MotionEvent event)
{
float x=event.getRawX();
float y=event.getRawY();
rectf.set(x-2, y+2, x+2, y-2);
Log.v("", "****************");
Log.v("x "+x, "y "+y);
Log.v("rect", rectf.left +" " + rectf.right +" " +rectf.top +" " +rectf.bottom);
Log.v("center", rectf.centerX()+" "+rectf.centerY());
invalidate();//
return true;
}
}
output
"****************
x 211.0: y 560.0
rect: 209.0 213.0 562.0 558.0
center: 211.0 560.0
:****************
x 220.0: y 547.0
rect: 218.0 222.0 549.0 545.0
center: 220.0 547.0
Any help would be appreciated
Upvotes: 1
Views: 1748
Reputation: 961
If for some reason you want to show a title bar again then you'll need to get the X and Y coordinates for the view canvas area not the entire screen.
You should use event.getX() and .getY() rather than .getRawX() and .getRawY().
Upvotes: 3
Reputation: 5971
Solved!!
It was the title bar offset :) I've added setTheme(android.R.style.Theme_Light_NoTitleBar_Fullscreen);
to MainActivity and it worked perfectly
Upvotes: 0