【问题标题】:Android How to get exact coordinate x and y values of image from the screenAndroid 如何从屏幕上获取图像的精确坐标 x 和 y 值
【发布时间】:2014-06-15 05:55:50
【问题描述】:

我在屏幕上有一个遥控器图像,我想当我当时单击任何特定按钮时发生任何特定事件,这意味着图像有一些按钮,当我准确触摸当时该图像中按住的按钮时任何事件,我如何获取仅在图像上按住此按钮的位置,并且屏幕点 x 和 y 也会在不同的屏幕上发生变化。我知道getX() 和 `getY()' 用于查找触摸位置,但如何获取图像中的任何特定位置,请快速帮助我,提前谢谢。

在图像中看到,当我触摸图像中间时,任何事件都会发生。

【问题讨论】:

    标签: android image position touch coordinate


    【解决方案1】:

    试试这个:

    anchorView.getLocationOnScreen(location);
    

    位置是一个数组:

     int location[] = new int[2];
    

    anchorView 是您的视图。

    所以你的函数可能看起来像:

    public getLocation(View anchorView){
     int location[] = new int[2];
     anchorView.getLocationOnScreen(location);
    
    }
    

    如果您想在该位置显示不同的视图,只需执行以下操作:

     yourView.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
                    location[0], location[1] + anchorView.getHeight());
    

    另外请注意,您可以操作 location[0] 和 location[1] 中的值。祝你好运:)

    编辑

    这是一个完整的例子,我用它在一个按钮下面显示一个片段,这个过程在按钮被点击时发生:

    public void showPopup(View anchorView) {
    
            View popupView = getLayoutInflater().inflate(R.layout.popup_layout, null);
    
            popupWindow = new PopupWindow(popupView, 
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    
    
            Button proceed = (Button)popupView.findViewById(R.id.button1);
            Button cancel = (Button)popupView.findViewById(R.id.button2);
    
            cb = (CheckBox)popupView.findViewById(R.id.checkBox1); 
    
            proceed.setOnClickListener(onProceed); 
            cancel.setOnClickListener(onCan); 
    
            popupWindow.setFocusable(true);
    
    
            popupWindow.setBackgroundDrawable(new ColorDrawable());
    
            int location[] = new int[2];
    
    
            anchorView.getLocationOnScreen(location);
    
            popupWindow.showAtLocation(anchorView, Gravity.NO_GRAVITY, 
                    location[0], location[1] + anchorView.getHeight());
    
        }
    

    上面将导致一个弹出窗口,就在点击的视图下方。

    popup_layout 是要膨胀的 XML 布局文件。

    【讨论】:

    • 在哪里调用这个方法?
    • 看你需要的地方!!
    • 你举几个例子
    • 编辑了我的答案,您可以在编辑部分查看。
    • 但是我只有一张遥控器图片,图片有按钮图片,不是原始按钮,它只是图片,所以我怎么能找到它们的位置
    猜你喜欢
    • 1970-01-01
    • 2021-06-11
    • 2021-07-10
    • 1970-01-01
    • 2012-07-16
    • 1970-01-01
    • 1970-01-01
    • 2022-11-13
    • 2017-08-09
    相关资源
    最近更新 更多