Android WebView禁用PinchZoom但保留控件
我有一个带有网络视图的android应用程序(android SDK 10)。在该WebView上,我必须使用具有固定位置的元素。现在我知道,固定元素存在问题,但是HTML中的代码:Android WebView禁用PinchZoom但保留控件,android,webview,zooming,Android,Webview,Zooming,我有一个带有网络视图的android应用程序(android SDK 10)。在该WebView上,我必须使用具有固定位置的元素。现在我知道,固定元素存在问题,但是HTML中的代码: <meta name="viewport" content="width=100%; initial-scale=1; maximum-scale=1; minimum-scale=1; user-scalable=no;"> 使用ZoomControl时,我可以进行缩放。但是
<meta name="viewport"
content="width=100%;
initial-scale=1;
maximum-scale=1;
minimum-scale=1;
user-scalable=no;">
使用ZoomControl时,我可以进行缩放。但是,多点触摸和pinchzoom会扭曲页面
是否有可能禁用pich和多点触控变焦,但保持变焦控制器工作
根据Vikalp Patel的建议,我得出了以下解决方案:
CustomWebView mWebView = (CustomWebView) findViewById(R.id.webView1);
mWebView.loadUrl("path/to.html");
CustomWebView.java
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.webkit.WebView;
public class CustomWebView extends WebView {
/**
* Constructor
*/
public CustomWebView(Context context) {
super(context);
}
/**
* Constructor
*/
public CustomWebView(Context context, AttributeSet attrs) {
super(context, attrs);
}
/*
* (non-Javadoc)
*
* @see android.webkit.WebView#onTouchEvent(android.view.MotionEvent)
*/
@Override
public boolean onTouchEvent(MotionEvent event) {
if (event.getPointerCount() > 1) {
this.getSettings().setSupportZoom(false);
this.getSettings().setBuiltInZoomControls(false);
} else {
this.getSettings().setSupportZoom(true);
this.getSettings().setBuiltInZoomControls(true);
}
return super.onTouchEvent(event);
}
}
layout.xml中的实现
<package.path.CustomWebView
...
/>
希望,这对某些人有所帮助。我已经查看了WebView的源代码,并得出结论,没有优雅的方法可以完成您的要求 我最终做的是对WebView进行子类化,并重写OnTouchEvent 。在
OnTouchEvent
中,对于ACTION\u DOWN
,我使用MotionEvent.getPointerCount()
检查有多少指针。如果有多个指针,则调用setSupportZoom(false)
,否则调用setSupportZoom(true)
。然后我调用super.OnTouchEvent()
这将在滚动时有效地禁用缩放(从而禁用缩放控制),并在用户即将按下缩放时启用缩放。这不是一个很好的方法,但到目前为止,它对我很有效
请注意,
getPointerCount()
是在2.1中引入的,因此如果您支持1.6,您将不得不做一些额外的工作。您可以找到许多关于这些的解决方案,谢谢,这帮助了我。我已经用根据你的建议提出的解决方案编辑了我的帖子。
Try to the following code
WebView mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setSupportZoom(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.loadUrl("path/to.html");
Try to the following code
WebView mWebView = (WebView) findViewById(R.id.webView1);
mWebView.getSettings().setSupportZoom(true);
mWebView.setVerticalScrollBarEnabled(true);
mWebView.loadUrl("path/to.html");