我正在玩画一些椭圆以响应触摸,我对触摸给出的坐标感到困惑并将其转换为一些椭圆,我不知道为什么我在 Y 中出现偏移,这里有一张说明我的问题的图像,一块代码和一些输出。
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;
}
}
输出
"****************
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
任何帮助,将不胜感激