【问题标题】:Get Y coordinate of motion touch event android获取运动触摸事件android的Y坐标
【发布时间】:2016-07-09 09:04:42
【问题描述】:

有一个图像存在。我尝试按下图像的顶部。我得到的值来自运动事件:

e.getY() 

这个值和我从getTop()得到的值不一样;

textview.getTop();

下面是我的代码:

    public class level1 extends AppCompatActivity {

    private final float TOUCH_SCALE_FACTOR = 180.0f / 320;
    private float mPreviousX;
    private float mPreviousY;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_level1);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        Intent intent = getIntent();

    }

    @Override
    public boolean onTouchEvent(MotionEvent e) {
        MyDBHandler dbHandler = new MyDBHandler(this, null, null, 1);
        String firstWord = dbHandler.getWord(1);
        String[] firstWordArray = firstWord.split("-");
        ArrayList<String[]> finalWord = new ArrayList<>();

        for (int i = 0; i < firstWordArray.length; i++) {
            finalWord.add(firstWordArray[i].split(","));
        }

        TextView textview = (TextView) findViewById(R.id.textView2);

        int x1 = textview.getLeft();
        int y1 = textview.getTop();

        int x2 = textview.getRight();
        int y2 = textview.getBottom();

        float width = x2 - x1;
        float height = y2 - y1;

        Log.i("Height",""+y1);
        Log.i("Height",""+y2);
        Log.i("Height",""+e.getY());
        Log.i("Height",""+textview.getY());
        Log.i("Height",""+textview.getHeight());
        Log.i("Height",""+height);
        Log.i("Width",""+x1);
        Log.i("Width",""+x2);
        Log.i("Width",""+e.getX());
        Log.i("Width",""+textview.getWidth());
        Log.i("Width",""+width);

        final Dialog dialog = new Dialog(level1.this);
        dialog.setContentView(R.layout.custom);
        dialog.setTitle("Root Word");

        // set the custom dialog components - text, image and button
        TextView text = (TextView) dialog.findViewById(R.id.text);
        text.setText("Android custom dialog example!");
        ImageView image = (ImageView) dialog.findViewById(R.id.image);

        Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
        // if button is clicked, close the custom dialog
        dialogButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        if (finalWord.size() != 0) {
            for (int i = 0; i < finalWord.size(); i++) {
                float x = e.getX();
                float y = e.getY();
                float xVar = 100*(x-x1)/width;
                float yVar = 100*(y-y1)/height;
                if (xVar< (Integer.parseInt(finalWord.get(i)[0]))) {
                    if (yVar < (Integer.parseInt(finalWord.get(i)[1]))) {
                        dialogButton.setText(finalWord.get(i)[2]);
                        break;
                    }
                }
            }
        }
        dialog.show();
        return true;
    }

}

【问题讨论】:

    标签: android textview motionevent


    【解决方案1】:

    也许this 是你要找的?

    值的不同在于MotionEventgetY()返回事件Y坐标,而TextViewgetTop()方法返回Y位置相对于其父项强>。

    假设您的布局如下所示:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click me!"
            android:id="@+id/textView"/>
    
    </LinearLayout>
    

    在这种情况下,当您单击 TextView 时,getTop() 返回 0,因为这是其父级的第一个(也是顶部)视图。但是,如果您在布局中添加另一个 TextView,类似于:

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Another text"/>
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Click me!"
            android:id="@+id/textView"/>
    
    </LinearLayout>
    

    如果你现在用“点击我!”点击 TextView文本,getTop() 将返回非零值(在我的情况下为 38),因为另一个 TextView 的高度。

    您想重视您触摸图像的位置吗?

    【讨论】:

    • e.getX() 和 e.getRawX() 返回相同的值,但该值与 textView.getTop() 有很大不同
    • 我明白你的解释。这就是我正在寻找的解释。我为 textView 创建了一个 onTouch 事件侦听器。所以现在 e.getY() 将返回相对于 textView 的值。
    猜你喜欢
    • 2011-02-25
    • 1970-01-01
    • 2011-10-05
    • 1970-01-01
    • 2011-03-28
    • 1970-01-01
    • 2013-03-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多