2

我对 Android 有另一个问题。今天我正在尝试使用 WebView 来显示图像。图像的尺寸大于视图,使用户能够滚动。现在,我想要实现的是当用户触摸图像上的某个位置(在 web 视图中)时,将触发一个事件并存储触摸的坐标。

当我尝试在 xml 布局中使用 onClick 执行此操作时,我得到的所有参数都是触发 on click 事件的视图对象。这是我的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中的通用点击/触摸。有没有实现我想要的替代方案?提前致谢 :)

4

2 回答 2

1

您需要的是 onTouchEvent: Android 指南

于 2012-07-27T06:26:36.327 回答
1

您是否尝试过使用 onTouch 事件而不是 onClick。onTouch 事件为您提供 x 和 y 坐标。在这里检查

于 2012-07-27T06:30:45.360 回答