这份代码是直接继承WebView了,重载了其中的几个方法,至于为什么这样做时候,PhoneGap性能能得到比较明显的提升,不懂的可以看我前面的那篇文章。
至于如何使用这份代码,只要了解PhoneGap的同学想必都知道的,只需要使用一个WebViewPlus的实例来加载你自己的html地址即可。经过测试发现,其实这样的改进方案还是能一定程度上改善PhoneGap在Android上点击响应缓慢的问题的。
代码如下:
public class WebViewPlus extends WebView { public boolean optimizeShortPressEnabled = true; public boolean optimizeFlingEnabled = true; public boolean getMoving() { return (Boolean) this.getPrivateObject("mConfirmMove"); } public Handler getPrivateHandler() { return (Handler) this.getPrivateObject("mPrivateHandler"); } public void OptimizedShortPress() { Handler handler = this.getPrivateHandler(); if (handler.hasMessages(5)) handler.removeMessages(5); Method method = this.getPrivateMethod("doShortPress", new Class[]{}); this.invokeMethod(method); } public void OptimizedFling(MotionEvent ev) { Long lastTime = (Long) this.getPrivateObject("mLastTouchTime"); if (ev.getEventTime() - lastTime > 250L) { this.setPrivateObject("mTouchMode", Integer.valueOf(3)); } } public void Initialize() { optimizeShortPressEnabled = true; optimizeFlingEnabled = true; } public WebViewPlus(Context context) { super(context); this.Initialize(); } public WebViewPlus(Context context, AttributeSet attrs) { super(context, attrs); this.Initialize(); } public WebViewPlus(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); this.Initialize(); } @Override public boolean onTouchEvent(MotionEvent ev) { if (optimizeFlingEnabled && getMoving()) { this.OptimizedFling(ev); } boolean result = super.onTouchEvent(ev); if (optimizeShortPressEnabled && ev.getAction() == MotionEvent.ACTION_UP && !getMoving()) { this.OptimizedShortPress(); } return result; } public Method getPrivateMethod(String methodName, Class[] paramClasses) { return getPrivateMethod(this, methodName, paramClasses); } public static Method getPrivateMethod(Object objectClass, String methodName, Class[] paramClasses) { try { Method theMethod = objectClass.getClass().getSuperclass().getDeclaredMethod(methodName, paramClasses); return theMethod; } catch (Exception e) { e.printStackTrace(); return null; } } public Object invokeMethod(Method method, Object[] paramObjects) { return WebViewPlus.invokeMethod(this, method, paramObjects); } public Object invokeMethod(Method method) { return WebViewPlus.invokeMethod(this, method, new Object[] { }); } public Object invokeMethod(String methodName) { try { Method method = this.getPrivateMethod(methodName, new Class[] { }); method.setAccessible(true); return method.invoke(this, new Object[] { }); } catch (Exception e) { e.printStackTrace(); return null; } } public static Object invokeMethod(Object objectClass, Method method, Object[] paramObjects) { try { method.setAccessible(true); return method.invoke(objectClass, paramObjects); } catch (Exception e) { e.printStackTrace(); return null; } } public Object getPrivateObject(String feildName) { return WebViewPlus.getPrivateObject(this, feildName); } public static Object getPrivateObject(Object objectClass, String feildName) { try { Field theFeild = objectClass.getClass().getSuperclass().getDeclaredField(feildName); theFeild.setAccessible(true); return theFeild.get(objectClass); } catch (Exception e) { e.printStackTrace(); return null; } } public static void setPrivateObject(Object objectClass, String feildName, Object newValue) { try { Field theFeild = objectClass.getClass().getSuperclass().getDeclaredField(feildName); theFeild.setAccessible(true); theFeild.set(objectClass, newValue); } catch(Exception e) { e.printStackTrace(); } } public void setPrivateObject(String feildName, Object newValue) { WebViewPlus.setPrivateObject(this, feildName, newValue); } }