存在图像。我尝试按下图像的顶部。我得到的值来自运动事件:
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;
}
}
答案 0 :(得分:1)
也许this正是您要找的?
值之间的差异来自MotionEvent
&#39; s getY()
返回事件Y坐标,但TextView
&#39; s getTop()
方法返回相对于其父的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 with&#34; Click me!&#34;文本,getTop()
将返回非零值(在我的情况下为38),因为其他TextView的高度。
您想要重视触摸图像的位置吗?