小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

PhoneGap優(yōu)化解決方案(續(xù))

 命運(yùn)之輪 2012-07-20

以前寫過一篇關(guān)于如何對(duì)PhoneGap進(jìn)行優(yōu)化的文章:PhoneGap優(yōu)化解決方案 。那篇文章主要是作為一份工作總結(jié),因此寫得很粗糙,主要是偏向理論,后來由于工作方向發(fā)生變化,也沒有再對(duì)PhoneGap深入研究下去了。

之后有許多網(wǎng)友向我要詳細(xì)的代碼,由于太忙我一直沒時(shí)間整理,今天終于有時(shí)間,也不多說廢話了,直接將代碼貼出來。

這份代碼是直接繼承WebView了,重載了其中的幾個(gè)方法,至于為什么這樣做時(shí)候,PhoneGap性能能得到比較明顯的提升,不懂的可以看我前面的那篇文章。

至于如何使用這份代碼,只要了解PhoneGap的同學(xué)想必都知道的,只需要使用一個(gè)WebViewPlus的實(shí)例來加載你自己的html地址即可。經(jīng)過測試發(fā)現(xiàn),其實(shí)這樣的改進(jìn)方案還是能一定程度上改善PhoneGap在Android上點(diǎn)擊響應(yīng)緩慢的問題的。

代碼如下:

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: 如果發(fā)現(xiàn)代碼有問題,或者更好的改進(jìn)方案,歡迎大家與我交流。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約