错误的x和y坐标Android [英] Getting wrong x and y coordinates Android

查看:50
本文介绍了错误的x和y坐标Android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void setQuickActionListener(View button) {

    float density = mActivity.getResources().getDisplayMetrics().density;
    WindowManager.LayoutParams wmlp = mActivity.getWindow().getAttributes();


    button.getLocationInWindow(location);
    Toast.makeText(mActivity, " l1 " + location[0] + " l2 " + location[1],Toast.LENGTH_LONG).show();
    wmlp.gravity = Gravity.TOP | Gravity.LEFT;
    wmlp.x = location[0]+ (int) (((button.getWidth() / 2) + button.getHeight() / 4) / density);
    wmlp.y = location[1] + (int) ((button.getHeight() - 60) / density);
    mQuickAction.show(button, (int) wmlp.x, (int) wmlp.y);

}

在这里,我用于获取ImageButton单击侦听器的x y值的方法以显示快速的动作视图,而这是用于动作视图的show方法的代码.

here the method i am using to get x y values for an ImageButton click listener to show quick action views and this the code for show method for action views.

    public void show(View anchor, int xPos, int yPos) {
    preShow();

    mDidAction          = false;

    int[] location      = new int[2];

    anchor.getLocationOnScreen(location);

    Rect anchorRect     = new Rect(location[0], location[1], location[0] + anchor.getWidth(), location[1] 
                        + anchor.getHeight());

    mRootView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    mRootView.measure(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);

    int rootHeight      = mRootView.getMeasuredHeight();
    int rootWidth       = mRootView.getMeasuredWidth();
    if (rootWidth == 0) {
        rootWidth       = mRootView.getMeasuredWidth();
    }

    int screenWidth     = mWindowManager.getDefaultDisplay().getWidth();
    int screenHeight    = mWindowManager.getDefaultDisplay().getHeight();

        arrowPos    = anchorRect.centerX()-xPos;

    int dytop       = anchorRect.top;
    int dybottom    = screenHeight/4;
    boolean onTop       = (dytop > dybottom) ? true : false;
    LinearLayout.LayoutParams rlpms;



//  xPos > (screenWidth - 100

    int dyleft      = xPos;
    int dyright     = screenWidth - screenWidth/10;

    boolean onRight     = (dyleft > dyright) ? true : false;

    if (onRight) {
        if(onTop){
            yPos = anchorRect.top - rootHeight/3;
            im1 =(ImageView) mRootView.findViewById(R.id.arrow_down);
            rlpms=new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
            lp.setMargins(0,rootHeight/3,0 ,0);
            im1.setLayoutParams(lp);
            Log.d("ddsaf", ""+onTop);
        }
        else
        {
            im1 =(ImageView) mRootView.findViewById(R.id.arrow_down);
            rlpms=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
            lp.setMargins(0,5,0 ,0);
            im1.setLayoutParams(lp);
            //Log.d("ddsaf", ""+onTop);

        }
            xPos = anchorRect.left - rootWidth;
    } else {
        if(onTop){
            yPos = anchorRect.top - rootHeight/3;
            im1 =(ImageView) mRootView.findViewById(R.id.arrow_up);
            rlpms=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
            lp.setMargins(0,rootHeight/3,0 ,0);
            im1.setLayoutParams(lp);
            //Log.d("ddsaf", "top "+onTop);
        }
        else
        {
            im1 =(ImageView) mRootView.findViewById(R.id.arrow_up);
            rlpms=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
            android.widget.LinearLayout.LayoutParams lp = (android.widget.LinearLayout.LayoutParams) im1.getLayoutParams();
            lp.setMargins(0,5,0 ,0);
            im1.setLayoutParams(lp);
            //Log.d("ddsaf", ""+onTop);

        }
        xPos = xPos+rootWidth/4;
    }
    Log.d("ddsaf", ""+anchorRect.left);
    showArrow(((onRight) ? R.id.arrow_down : R.id.arrow_up), arrowPos);

    setAnimationStyle(screenWidth, anchorRect.centerX(), onRight);

    mWindow.showAtLocation(anchor, Gravity.NO_GRAVITY, xPos, yPos);}

问题是我在错误的位置上获得了快速的操作视图,即在infalted布局中一次出现在0,0时,为什么在下面发生这种情况的任何想法都是使用 *

problem is i am getting quick action views at wrong postions ie at 0,0 once in while on infalted layout any idea why this happens below is where it has been infalted to with *

   <LinearLayout
        android:id="@+id/lyt_xx"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="60"
        android:orientation="horizontal"
        android:padding="@dimen/padding_small" >

        <ImageView
            android:id="@+id/img_separator"
            android:layout_width="30dp"
            android:layout_height="fill_parent"
            android:layout_marginLeft="@dimen/padding_small"
            android:layout_marginRight="@dimen/padding_small"
            android:background="#333333" />

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="horizontal" >

            ***<com.apps.horizontalgrid.TwoWayGridView
                xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:app="http://schemas.android.com/apk/res-auto"
                android:id="@+id/xx_gridview"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/background"
                android:padding="@dimen/padding_small"
                app:cacheColorHint="#00000000"
                app:gravity="fill"
                app:horizontalSpacing="@dimen/padding_small"
                app:numColumns="auto_fit"
                app:numRows="2"
                app:rowHeight="260dp"
                app:scrollDirectionLandscape="horizontal"
                app:scrollDirectionPortrait="horizontal"
                app:verticalSpacing="@dimen/padding_small" >
            </com.apps.horizontalgrid.TwoWayGridView>***
        </LinearLayout>
   <!--  -->
    </LinearLayout>

        <!--  -->

推荐答案

每个View对象/子类都可以访问执行此操作的方法.

Each View object/subclass has access to methods which do exactly this.

View.getTop();     // Top position of this view relative to its parent.
View.getLeft();    // Left position of this view relative to its parent.
View.getRight();   // Right position of this view relative to its parent.
View.getBottom();  // Bottom position of this view relative to its parent.

在调用任何这些方法之前,必须确保已显示View,否则您将获得0或最糟糕的是NPE.

You have to ensure that the View has been displayed prior to calling any of these methods, otherwise you'll get 0 or at worst an NPE.

请记住,这些返回相对于View父级的位置.如果您想要视图的实际绝对位置,则需要使用以下方法:

Bear in mind, these return positions relative to a View's parent. If you want the actual absolute position of the View, then you will need to use the method(s):

View.getLocationInWindow(); // Computes the coordinates of this view in its window.
or
View.getLocationOnScreen(); // Computes the coordinates of this view on the screen.

无需重新发明轮子.可以在这里找到更多信息: http://developer.android.com/reference/android/view/View.html

There is no need to reinvent the wheel. Further information can be found here: http://developer.android.com/reference/android/view/View.html

这篇关于错误的x和y坐标Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆