2012-07-27 81 views
2

我對Android有另一個問題。今天我試圖使用WebView來顯示圖像。圖像的尺寸大於使用戶能夠滾動的視圖。現在,我想要實現的是當用戶觸摸圖像上的某個地方(在web視圖中)時,將觸發事件並存儲觸摸的座標。單擊WebView上的事件座標

當我嘗試在xml佈局中使用onClick來完成此操作時,我所獲取的所有參數都是觸發點擊事件的視圖對象。這裏是我的xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<WebView 
    android:id="@+id/upperview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" 
    android:onClick="imageClicked" /> 
<WebView 
    android:id="@+id/lowerview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1" /> 

</LinearLayout> 

現在,這裏是我的活動:

public class MyActivity extends Activity { 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    WebView upperview = (WebView) findViewById(R.id.upperview); 
    upperview.getSettings().setBuiltInZoomControls(true); 
    upperview.loadUrl("file:///android_asset/texture.png"); 

    WebView lowerview = (WebView) findViewById(R.id.lowerview); 
    String htmlstring = "<h1>Header</h1><p>This is HTML text<br /><i>Formatted in italics</i></p>"; 
    lowerview.loadData(htmlstring, "text/html", "utf-8"); 
} 

public void imageClicked(View view) { 

} 
} 

現在,還有的imageClicked方法中沒有的功能,因爲我不知道在哪裏可以找到座標。我做錯了嗎?我在想也許xml佈局中的onClick屬性只對應於webview中的通用點擊/觸摸。有什麼替代方案可以實現我想要的嗎?感謝提前:)

回答

1

你有沒有試着用onTouch事件,而不是的onClick。 onTouch事件爲您提供x和y座標。檢查here

+0

我剛試過那個,它工作。但是我可能會在這裏做錯誤的事情,因爲我想要相對於圖像的座標。事件的座標是相對於視圖的權利? – kishidp 2012-07-27 06:33:27

1

你需要什麼的onTouchEvent: Android guide

+0

好的。謝謝。只是一個問題,是相對於webview的座標?因爲我想要實現的是觸摸相對於圖像的座標嗎?這甚至有可能嗎? – kishidp 2012-07-27 06:30:51

+0

對不起,我不太確定。嘗試在座標上書寫x和y,當你觸摸屏幕時登錄到logcat,然後你可能會發現:) – 2012-07-27 06:44:38

+0

我有點懶,所以我用'Toast'代替顯示座標和yup,它們是相對於webview。我在這裏做錯了事。厄運。 – kishidp 2012-07-27 06:50:48