gpt4 book ai didi

android - 获取运动触摸事件android的Y坐标

转载 作者:行者123 更新时间:2023-11-30 01:12:36 27 4
gpt4 key购买 nike

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

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;
}

}

最佳答案

也许 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,因为这是其父 View 的第一个(也是顶部) View 。但是,如果您将另一个 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>

如果您现在单击带有“Click me!”的 TextView文本,getTop() 将返回非零值(在我的例子中是 38),因为另一个 TextView 的高度。

您想评估您触摸图像的位置吗?

关于android - 获取运动触摸事件android的Y坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38280153/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com