分享

PhoneGap优化解决方案(续)

 命運之輪 2012-07-20

以前写过一篇关于如何对PhoneGap进行优化的文章:PhoneGap优化解决方案 。那篇文章主要是作为一份工作总结,因此写得很粗糙,主要是偏向理论,后来由于工作方向发生变化,也没有再对PhoneGap深入研究下去了。

之后有许多网友向我要详细的代码,由于太忙我一直没时间整理,今天终于有时间,也不多说废话了,直接将代码贴出来。

这份代码是直接继承WebView了,重载了其中的几个方法,至于为什么这样做时候,PhoneGap性能能得到比较明显的提升,不懂的可以看我前面的那篇文章。

至于如何使用这份代码,只要了解PhoneGap的同学想必都知道的,只需要使用一个WebViewPlus的实例来加载你自己的html地址即可。经过测试发现,其实这样的改进方案还是能一定程度上改善PhoneGap在Android上点击响应缓慢的问题的。

代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
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);
	}
}

ps: 如果发现代码有问题,或者更好的改进方案,欢迎大家与我交流。

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约