• PhoneGap优化解决方案(续)


    这份代码是直接继承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);
    	}
    }
    


  • 相关阅读:
    数据分析师入门——用 Pandas 进行数据预处理:数据清洗与可视化
    hdu 1532 Dinic模板(小白书)
    二分图的最大匹配、完美匹配和匈牙利算法(转)
    HDU 1532 (Dinic算法)
    HDU 1532 Drainage Ditches EK算法 flod算法
    Edmonds_Karp 算法入门详解(转)
    UVa 10801
    Codeforces Round #359 (Div. 2)C
    Codeforces Round #358 (Div. 2)B. Alyona and Mex
    int long long范围
  • 原文地址:https://www.cnblogs.com/liuruichao/p/4005837.html
Copyright © 2020-2023  润新知