我还有另一个Android问题。今天我正在尝试使用WebView来显示图像。图像的尺寸大于视图,使用户能够滚动。现在,我想要实现的是当用户触摸图像上的某个位置时(在webview中),将触发事件并且将存储触摸的坐标。
当我尝试在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中的通用点击/触摸。有没有其他方法可以实现我的目标?在此先感谢:)
答案 0 :(得分:1)
你需要的是onTouchEvent: Android guide
答案 1 :(得分:1)
您是否尝试过使用onTouch事件而不是onClick。 onTouch事件为您提供x和y坐标。检查here