简体   繁体   English

如何在onCreate上获取android中图像的x和y位置?

[英]How can get x and y position of an image in android on onCreate?

I want current X & Y co ordinates of an ImageView on onCreate of Activity , is there any solution for the same ? 我想在Activity的 onCreate上使用ImageView的当前X和Y坐标,是否有相同的解决方案? Please share your idea on same. 请在同一点上分享您的想法。

Actually, when you call setContentView() the Android nutshell starts the views drawing on surface, Which you can observe on using viewTrewwObserve So you can not get the height and width of ImageView in onCreate() as its not currently draw yet. 实际上,当您调用setContentView() ,Android外壳程序会开始在表面上绘制视图,您可以使用viewTrewwObserve进行观察,因此您无法在onCreate()获得ImageView的高度和宽度,因为它目前尚未绘制。

You can use, (Not tried) 您可以使用,(未尝试)

imageView.getViewTreeObserver().addOnGlobalLayoutListener(
 new ViewTreeObserver.OnGlobalLayoutListener() {
  @Override
  public void onGlobalLayout() {
    // Get ImageView height width here <------------
  }
});

It will Register a callback to be invoked when the global layout state or the visibility of views within the view tree changes. 当全局布局状态或视图树中视图的可见性更改时,它将注册要调用的回调。

Update: 更新:

Or you can use onWindowFocusChanged (boolean hasFocus) . 或者,您可以使用onWindowFocusChanged (boolean hasFocus)

This is the best indicator of whether this activity is visible to the user. 这是该活动是否对用户可见的最佳指示。 The default implementation clears the key tracking state, so should always be called. 默认实现清除键跟踪状态,因此应始终调用它。

(1)You can get it using the getViewTreeObserver() or (2)you can add an asynchronous Task on your onCreate() (1)您可以使用getViewTreeObserver()获得它,或(2)您可以在onCreate()上添加异步Task

@Override
protected void onCreate(Bundle savedInstanceState)
{
  //SIMPLY CALL YOUR ASYN TASK WITH DELAY OVER HERE LIKE THIS.

  new MyImageDrawClass().execute();
}



class MyImageDrawClass extends AsyncTask<Void, Void, Void>
{
    @Override
    protected Void doInBackground(Void... params)
    {
        try
        {
            Thread.sleep(100);
        }
        catch(InterruptedException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result)
    {
        LayoutParams lp = mContent.getLayoutParams();
        value_wdth = mContent.getWidth();
        value_hght = mContent.getHeight();

        //PERFORM YOUR OTHER DRAWING STUFF..
        super.onPostExecute(result);
    }

}

let me know if you find issue with this, you can also reduce the time delay to Thread.sleep(10); 让我知道如果您发现与此有关的问题,还可以减少Thread.sleep(10);的时间延迟Thread.sleep(10); I generally use this method to get height and to get width. 我通常使用这种方法来获取高度和宽度。

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

相关问题 android - 如何在imageview中获取图像边缘x / y位置 - android - how to get the image edge x/y position inside imageview 如何在 Android 上使用 x 和 y position 获取视图 - How to get view with x and y position on Android 如何在Android中获取图像的(x,y)坐标? - How to get (x,y) cordinates of image in android? 如何在oncreate(android)中以编程方式移动到屏幕上的其他x,y位置 - How to move to a different x,y location on a screen programatically in oncreate(android) 在Android中放大后如何获取视图的xy位置 - How to get the x y position of a view after zoom-in in android 如何在android TextView中获取点击跨度的x,y位置? - How to get x,y position of clicked span in android TextView? 如何在EditText android中获取光标位置(x,y) - How to get cursor position (x,y) in EditText android 如何在Android中的画布上获取圆形drwan的位置(x和y坐标)? - how to get position(x and y coordinates) of circle drwan on canvas in android? Java Android:如何在 GLSurfaceView 的位置(X,Y)处获取像素颜色? - Java Android: how to get pixel color at position (X, Y) of GLSurfaceView? Android onAccessibilityEvent获取TextView的X和Y位置 - Android onAccessibilityEvent get X and Y position of TextView
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM