我有一个通用活动,其中包含一个webview来显示不同的基于HTML的内容。
每个html内容都有这样的元标记。
<meta name="viewport" content="width=device-width,height=device-height, minimum-scale=0.4, maximum-scale=3,initial-scale=0.5">
WebView以编程方式配置为使用这些代码在屏幕上显示内容。
WebView webView = (WebView) findViewById(R.id.webview1);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setLightTouchEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setBackgroundColor(0);
不幸的是,元代码和webview配置都不适合屏幕中的内容;但双击适合内容优秀。所以我想我可以通过编程方式模拟webview上的双击吗?
答案 0 :(得分:2)
/**
*
*/
package com.y2cf.hoppr.utils.viewutils;
import android.content.Context;
import android.os.SystemClock;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.webkit.WebView;
/**
* @author rajesh
*
*/
public class MyWebView extends WebView {
private GestureDetector gestureDetector;
/**
* @param context
*/
public MyWebView(Context context) {
super(context);
gestureDetector = new GestureDetector(context, new GestureListener());
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
*/
public MyWebView(Context context, AttributeSet attrs) {
super(context, attrs);
gestureDetector = new GestureDetector(context, new GestureListener());
// TODO Auto-generated constructor stub
}
/**
* @param context
* @param attrs
* @param defStyle
*/
public MyWebView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
gestureDetector = new GestureDetector(context, new GestureListener());
// TODO Auto-generated constructor stub
}
public void simulateDoubleTap()
{
simulateDoubleTapEvent(0);
simulateDoubleTapEvent(2);
simulateDoubleTapEvent(2);
simulateDoubleTapEvent(1);
simulateDoubleTapEvent(0);
}
private void simulateDoubleTapEvent(int action)
{
long downTime = SystemClock.uptimeMillis();
long eventTime = SystemClock.uptimeMillis() + 100;
float x = 0.0f;
float y = 0.0f;
// List of meta states found here: developer.android.com/reference/android/view/KeyEvent.html#getMetaState()
int metaState = 0;
MotionEvent me = MotionEvent.obtain(
downTime,
eventTime,
action,
x,
y,
metaState
);
dispatchTouchEvent(me);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(event);
}
private class GestureListener extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDown(MotionEvent e) {
return true;
}
// event when double tap occurs
@Override
public boolean onDoubleTap(MotionEvent e) {
float x = e.getX();
float y = e.getY();
Log.d("Double Tap", "Tapped at: (" + x + "," + y + ")");
return true;
}
}
}
创建一个这样的类并调用simulateDoubleTap()方法。希望这会有所帮助,你的webview应该是这个类的一个实例。