2

我有一个使用 drawRect(Rect, Paint) 绘制的 Rect 对象。我有一个 OnTouchEvent 来检查触摸位置是否在 Rect 内。如果触摸在 Rect 内部,那么我屏幕上的某些内容会发生变化。我遇到的问题是:我必须触摸才能执行触摸语句的位置比 Rect 对象高约 50 像素。

这是我的代码的相关部分:

public class MainActivity extends Activity {
    DrawView drawView;
    RelativeLayout fLY;
    static Rect newGame = new Rect(LEFT, 165 + 35, 320, 200 + 35);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        fLY = new RelativeLayout(this);
        drawView = new DrawView(this);
        fLY.addView(drawView);
        setContentView(fLY);
    }

    @Override
   public boolean onTouchEvent(MotionEvent event) {
        int touchX = (int)event.getX();
        int touchY = (int)event.getY();
        switch(event.getAction()){
            case MotionEvent.ACTION_DOWN:
            if(newGame.contains(touchX, touchY))reset();
        }
    }

还有我的 DrawView 类:

public class DrawView extends View{
    Paint paint = new Paint();
    int COLORS[] = Constants.COLORS;
    int left = 250;
    int size = 35;

    public DrawView(Context context) {
        super(context);   
    }

    @Override
    public void onDraw(Canvas canvas) {
        canvas.drawRect(MainActivity.newGame, paint);
    }
 }
4

1 回答 1

0

它可能正好偏离状态栏的高度。您可以通过这个答案获得状态栏的高度:状态栏的高度?

然后将其添加到您的getY,它应该工作。

于 2013-04-04T05:04:11.327 回答