Getting 0 Value for both X and Y Coordinate of view(Imagebutton) in android

前端 未结 4 1417
失恋的感觉
失恋的感觉 2021-01-01 05:46

I want to get X and Y Point of View(ImageButton).

When I try to find X and Y on Click event with below code I get proper X and Y coordinate for View.



        
4条回答
  •  难免孤独
    2021-01-01 06:32

    Get the values on onWindowFocusChanged().

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
    
        ImageButton imagebutton = (ImageButton) findViewById(R.id.imagebutton);
        int[] posXY = new int[2];
        imagebutton.getLocationInWindow(posXY);
        int x = posXY[0];
        int y = posXY[1];
    
        Log.d("X and Y Point", x + " " + y);
    }
    

提交回复
热议问题