Android WebView使用setWideViewPort,禁用双击缩放但保持收缩缩放?
我正在使用这个代码,它的工作原理完全符合我的要求。但我必须在双击上实现另一个功能,并想禁用双击缩放(但保留收缩缩放功能) 尝试手动计算刻度,但没有运气(这感觉很复杂)。Android WebView使用setWideViewPort,禁用双击缩放但保持收缩缩放?,android,android-webview,Android,Android Webview,我正在使用这个代码,它的工作原理完全符合我的要求。但我必须在双击上实现另一个功能,并想禁用双击缩放(但保留收缩缩放功能) 尝试手动计算刻度,但没有运气(这感觉很复杂)。 是否有一种方法可以使用setUseWideViewPort和ZoomControl,但仅禁用或覆盖双击缩放?对不起,我没有时间测试,请尝试: GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener()
是否有一种方法可以使用setUseWideViewPort和ZoomControl,但仅禁用或覆盖双击缩放?对不起,我没有时间测试,请尝试:
GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
public boolean onDoubleTap(MotionEvent e) {
showToast("Double tap");
return true; //instead of false
}
}
找到了一个解决方案:
class MyWebView extends WebView {
public boolean onTouchEvent(MotionEvent event) {
gd.onTouchEvent(event);
// disable double tap zooming
if(doubleTap)
{
doubleTap = false;
return false;
}
return super.onTouchEvent(event);
}
GestureDetector.SimpleOnGestureListener sogl = new GestureDetector.SimpleOnGestureListener() {
public boolean onDoubleTap(MotionEvent e) {
showToast("Double tap");
doubleTap = true;
return false;
}
}
最好的解决方案是从MyWebView扩展您的WebView
public class HelpWebView extends WebView {
private GestureDetector gestureDetector;
private AtomicBoolean mPreventAction = new AtomicBoolean(false);
private AtomicLong mPreventActionTime = new AtomicLong(0);
public HelpWebView(Context context) {
super(context);
gestureDetector = new GestureDetector(context, new GestureListener());
}
public HelpWebView(Context context, AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetector(context, new GestureListener());
}
public HelpWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
gestureDetector = new GestureDetector(context, new GestureListener());
}
public HelpWebView(Context context, AttributeSet attrs, int defStyle, boolean privateBrowsing) {
super(context, attrs, defStyle, privateBrowsing);
gestureDetector = new GestureDetector(context, new GestureListener());
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int index = (event.getAction() & MotionEvent.ACTION_POINTER_INDEX_MASK) >> MotionEvent.ACTION_POINTER_INDEX_SHIFT;
int pointId = event.getPointerId(index);
// just use one(first) finger, prevent double tap with two and more fingers
if (pointId == 0){
gestureDetector.onTouchEvent(event);
if (mPreventAction.get()){
if (System.currentTimeMillis() - mPreventActionTime.get() > ViewConfiguration.getDoubleTapTimeout()){
mPreventAction.set(false);
} else {
return true;
}
}
return super.onTouchEvent(event);
} else {
return true;
}
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
mPreventAction.set(true);
mPreventActionTime.set(System.currentTimeMillis());
return true;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
mPreventAction.set(true);
mPreventActionTime.set(System.currentTimeMillis());
return true;
}
}
}
带有“一(第一)个手指”解决方案的手势检测器工作不可靠。在GalaxyS3 Android 4.0.4上,网络视图有时仍会被放大。在缩放视图时,可以使用类似这样的附加WebViewClient来恢复缩放比例:
public class NoZoomedWebViewClient extends WebViewClient {
private static final String LOG_TAG = "NoZoomedWebViewClient";
private static final long STABLE_SCALE_CALCULATION_DURATION = 2 * 1000;
private long stableScaleCalculationStart;
private String stableScale; // Avoid comparing floats
private long restoringScaleStart;
NoZoomedWebViewClient() {
stableScaleCalculationStart = System.currentTimeMillis();
}
@Override
public void onScaleChanged(final WebView view, float oldScale, float newScale) {
Log.d(LOG_TAG, "onScaleChanged: " + oldScale + " -> " + newScale);
long now = System.currentTimeMillis();
boolean calculating = (now - stableScaleCalculationStart) < STABLE_SCALE_CALCULATION_DURATION;
if (calculating) {
stableScale = "" + newScale;
} else if (!stableScale.equals("" + newScale)) {
boolean zooming = (now - restoringScaleStart) < STABLE_SCALE_CALCULATION_DURATION;
if (!zooming) {
Log.d(LOG_TAG, "Zoom out to stableScale: " + stableScale);
restoringScaleStart = now;
view.zoomOut();
// Just to make sure, do it one more time
view.postDelayed(new Runnable() {
@Override
public void run() {
view.zoomOut();
}
}, STABLE_SCALE_CALCULATION_DURATION);
}
}
}
}
公共类NoZoomedWebViewClient扩展了WebViewClient{
私有静态最终字符串日志\u TAG=“NoZoomedWebViewClient”;
私人静态最终长期稳定规模计算持续时间=2*1000;
私人长马厩CaleguationStart;
私有字符串stableScale;//避免比较浮点
私人长期启动;
NoZoomedWebViewClient(){
stableScaleCalculationStart=System.currentTimeMillis();
}
@凌驾
尺度上的公共无效已更改(最终WebView视图、浮动旧尺度、浮动新尺度){
Log.d(Log_标签,“onScaleChanged:“+oldScale+”->“+newScale”);
long now=System.currentTimeMillis();
布尔计算=(现在-stableScaleCalculationStart)
能否添加覆盖doubletap事件的代码?我正在使用MyWebView扩展WebView。public MyWebView(FriaBook context){super(context);gd=new GestureDetector(context,sogl)}GestureDetector.SimpleOnGestureListener sogl=new GestureDetector.SimpleOnGestureListener(){public boolean onDoubleTap(MotionEvent e){showToast(“双击”);返回false;}…有点像我是Android和Java的新手…onDoubleTap会启动,但不会覆盖双击缩放。感谢您的输入,但我已使用返回值过期,似乎没有任何区别。奇怪。
public class NoZoomedWebViewClient extends WebViewClient {
private static final String LOG_TAG = "NoZoomedWebViewClient";
private static final long STABLE_SCALE_CALCULATION_DURATION = 2 * 1000;
private long stableScaleCalculationStart;
private String stableScale; // Avoid comparing floats
private long restoringScaleStart;
NoZoomedWebViewClient() {
stableScaleCalculationStart = System.currentTimeMillis();
}
@Override
public void onScaleChanged(final WebView view, float oldScale, float newScale) {
Log.d(LOG_TAG, "onScaleChanged: " + oldScale + " -> " + newScale);
long now = System.currentTimeMillis();
boolean calculating = (now - stableScaleCalculationStart) < STABLE_SCALE_CALCULATION_DURATION;
if (calculating) {
stableScale = "" + newScale;
} else if (!stableScale.equals("" + newScale)) {
boolean zooming = (now - restoringScaleStart) < STABLE_SCALE_CALCULATION_DURATION;
if (!zooming) {
Log.d(LOG_TAG, "Zoom out to stableScale: " + stableScale);
restoringScaleStart = now;
view.zoomOut();
// Just to make sure, do it one more time
view.postDelayed(new Runnable() {
@Override
public void run() {
view.zoomOut();
}
}, STABLE_SCALE_CALCULATION_DURATION);
}
}
}
}