【问题标题】:Android : get location of object for the screenAndroid:获取屏幕对象的位置
【发布时间】:2013-11-25 10:27:17
【问题描述】:

我试图为布局内的任何对象获取 x,y,但是当对象位于屏幕的末端角落时我得到了这个值:

对象 X:266

对象 Y:361

屏幕宽度:320

屏幕高度:480

我怎样才能知道屏幕上的对象到底在哪里? (end,top,left,center)。

【问题讨论】:

    标签: android dimensions


    【解决方案1】:

    希望这有帮助..

    public Rect locateView(View view) {
        Rect loc = new Rect();
        int[] location = new int[2];
        if (view == null) {
            return loc;
        }
        view.getLocationOnScreen(location);
    
        loc.left = location[0];
        loc.top = location[1];
        loc.right = loc.left + view.getWidth();
        loc.bottom = loc.top + view.getHeight();
        return loc;
    }
    

    我是这样使用方法的:

      Rect r = TVGridUtils.locateView(activity.findViewById(R.id.imageview));                 
    
            float touchX=r.left+((r.right-r.left)/2);
            float touchY=(r.bottom);    
    

    注意:我必须得到我触摸的图像视图的中间点

    【讨论】:

      【解决方案2】:

      我认为您需要将对象的widthheight 添加到xy

      要准确了解屏幕上的对象在哪里

      X= 0,Y= 0 左上角

      X= screenWidth-objectWidth,Y= 0 右上

      X= 0,Y= screenHeight-objectHeight 左下角

      X= screenWidth-objectWidth,Y= screenHeight-objectHeight 右下角

      【讨论】:

        【解决方案3】:
        Button b;
        int x1 = b.getX();
        int x2 = x1 + b.getWidth();
        int y1 = b.getY();
        int y2 = y1 + b.getHeight(); 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2019-08-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-02-06
          • 1970-01-01
          • 2017-06-17
          • 1970-01-01
          相关资源
          最近更新 更多