我對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中的通用點擊/觸摸。有什麼替代方案可以實現我想要的嗎?感謝提前:)
我剛試過那個,它工作。但是我可能會在這裏做錯誤的事情,因爲我想要相對於圖像的座標。事件的座標是相對於視圖的權利? – kishidp 2012-07-27 06:33:27