简体   繁体   English

Android是否RectF坐标与触摸坐标不同?

[英]Android Are RectF coordinates differ from touch coordinates?

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

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. 如果由于某种原因您想再次显示标题栏,则需要获取视图画布区域的X和Y坐标,而不是整个屏幕。

You should use event.getX() and .getY() rather than .getRawX() and .getRawY(). 您应该使用event.getX()和.getY()而不是.getRawX()和.getRawY()。

Solved!! 解决了!!
It was the title bar offset :) I've added setTheme(android.R.style.Theme_Light_NoTitleBar_Fullscreen); 这是标题栏偏移:)我添加了setTheme(android.R.style.Theme_Light_NoTitleBar_Fullscreen); to MainActivity and it worked perfectly 到MainActivity,它完美地运作

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM