[英]event.getX and screen coordinates
好吧,我的問題是這樣的:我有一個圖像,並且我想在單擊地圖的特定點時顯示其他圖像。 我正在使用此代碼
private void setImageClickListener() {
ImageView map_image=(ImageView)findViewById(R.id.map_icon);
map_image.setOnTouchListener(new ImageView.OnTouchListener() {
//OnTouchListener listener = new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(!(event.getAction() == MotionEvent.ACTION_DOWN))
return false; //If the touch event was not putting the finger down on the screen, return false(Actions may be move, up, and so on)
final float x = event.getX();
final float y = event.getY();
System.out.println("Coordinates of button pressed are: X is %d"+x+" and Y is %d"+ y);
if(x>260 && x<390 && y>270 && y< 290)
DoFirst();
//... and so on...
//In the end, you must return a boolean saying whether you "consumed" the event - if you handled the event or not.
return true;
}
});
而我的xml文件是:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
>
<ImageView
android:id="@+id/map_icon"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:scaleType="fitXY"
android:src="@drawable/home_map" />
問題是,當我在模擬器上運行時,坐標與在不同手機上運行時的坐標不同。 如何在多個屏幕上運行? 我認為使用event.getX的bu的坐標是指圖像坐標,而不是屏幕坐標。 所以我要的是范圍要獨立於屏幕。
有什么幫助嗎?
我建議您使用自定義視圖,以完全控制ImageView。 然后在onSizeChange()中,無論移動設備如何,您都會知道確切的大小...
這是一個示例: 使位圖的某些區域在觸摸時透明
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.