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

前端 未结 4 1427
失恋的感觉
失恋的感觉 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:22

    Two options:

    • Implementing your logics in onResume.
    • Using ViewTreeObserver.

      final ViewTreeObserver vto = findViewById(R.id.YOUR_VIEW_ID).getViewTreeObserver(); 
      vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener() { 
          @Override 
          public void onGlobalLayout() { 
              vto.removeOnGlobalLayoutListener(this);  
      
              // Get X, Y of the ImageView here
          } 
      });
      

提交回复
热议问题