今天弄了下android webview下的幾個(gè)頁(yè)面。原先以為android 4+把 webview的viewport屬性忽略掉了。
但是今天弄了下。加了個(gè) authorizationView.getSettings().setUseWideViewPort(true);
viewport 的幾個(gè)屬性重新起作用。(測(cè)試環(huán)境,4.0+的幾個(gè)版本)
但是又遇到幾個(gè)問題,就是html里有input的時(shí)候。獲取焦點(diǎn)的時(shí)候,android會(huì)重新縮放到原來(lái)模式,看源碼:
/**
* Called in response to a message from webkit telling us that the soft
* keyboard should be launched.
*/
private void displaySoftKeyboard(boolean isTextView) {
InputMethodManager imm = (InputMethodManager)
getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
// bring it back to the default level scale so that user can enter text
boolean zoom = mZoomManager.getScale() < mZoomManager.getDefaultScale();
if (zoom) {
mZoomManager.setZoomCenter(mLastTouchX, mLastTouchY);
mZoomManager.setZoomScale(mZoomManager.getDefaultScale(), false);
}
if (isTextView) {
rebuildWebTextView();
if (inEditingMode()) {
imm.showSoftInput(mWebTextView, 0, mWebTextView.getResultReceiver());
if (zoom) {
didUpdateWebTextViewDimensions(INTERSECTS_SCREEN);
}
return;
}
}
// Used by plugins and contentEditable.
// Also used if the navigation cache is out of date, and
// does not recognize that a textfield is in focus. In that
// case, use WebView as the targeted view.
// see http://b/issue?id=2457459
imm.showSoftInput(this, 0);
}
從源碼可以看到,webview當(dāng)要彈起鍵盤的時(shí)候,會(huì)判定當(dāng)前的縮放比例與默認(rèn)大小(我測(cè)試了下,我自己的版本的默認(rèn)值是1.5),
當(dāng)你網(wǎng)頁(yè)viewport設(shè)置initial-scale=0.5時(shí),當(dāng)input 獲取焦點(diǎn)的時(shí)候,android會(huì)放大到原來(lái)模式,不是我們想要的,網(wǎng)上查了下相關(guān),
有個(gè)解決方案:
wv.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
try {
Field defaultScale = WebView.class
.getDeclaredField("mDefaultScale");
defaultScale.setAccessible(true);
float _s = defaultScale.getFloat(wv);
defaultScale.setFloat(wv, scale);
float x = wv.getScale();
int i = 0;
} catch (Exception e) {
e.printStackTrace();
try {
Field defaultZoom = WebView.class
.getDeclaredField("mZoomManager");
defaultZoom.setAccessible(true);
Field defaultScale = defaultZoom.getType()
.getDeclaredField("mDefaultScale");
defaultScale.setAccessible(true);
defaultScale.setFloat(defaultZoom.get(wv), scale);
} catch (Exception ee) {
ee.printStackTrace();
}
}
}
});
但是作者碰到另外一個(gè)問題,引用自原話:
as it showed, I using reflect to find the field 'mDefaultScale' to control the WebView.
But it doesn't work on Android 4.1.1 (Google Nexus), and I catch an exception —— java.lang.NoSuchFieldException: mDefaultScale.
Then I list the fileds and found the framework source seems being changed(I can only reach a field called 'mProvider').
So how can I fix the problem (I haven't got the source yet)? Thanks for reading my question with my poor English, Thx :)
PS: maybe a online framework source review website is helpful but I can't found one, if you can provide me one, it will be great. :P
完了我自己測(cè)試了,發(fā)現(xiàn)此方案解決不了。但是引出了另外一問題,就是不用android版本下的webview實(shí)現(xiàn)是不一樣的,其實(shí)看代碼就能看出,
原先webview有mDefaultScale字段,但是后來(lái)應(yīng)該挪到mZoomManager里去了,但是我發(fā)現(xiàn)我手機(jī)上webview 實(shí)現(xiàn)和作者遇到的問題一樣,只有mProvider成員,
沒有mZoomManager,所以只能尋求源碼,千辛萬(wàn)苦,終于找到
http://androidxref.com/4.2_r1/xref/frameworks/base/core/java/android/webkit/WebViewClassic.java,
mProvider 其實(shí)類型就是WebViewClassic(自己看下源碼實(shí)現(xiàn)),簡(jiǎn)要提下,WebProvider只是一個(gè)接口,作為WebView的一個(gè)成員,
創(chuàng)建時(shí)用了factory來(lái),完了看下幾個(gè)工廠類,最后是webviewclassic實(shí)例)。
對(duì)于Jerry Bean 4.2這個(gè)版本(我一個(gè)手機(jī)就是自己刷的rom),webview的實(shí)現(xiàn)又換了個(gè),所以要拿到默認(rèn)縮放的成員,如下:
try {
Field defaultScale = WebView.class
.getDeclaredField("mDefaultScale");
defaultScale.setAccessible(true);
float sv = defaultScale.getFloat(authorizationView);
defaultScale.setFloat(authorizationView, xxx);
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
try {
Field zoomManager;
zoomManager = WebView.class.getDeclaredField("mZoomManager");
zoomManager.setAccessible(true);
Object zoomValue = zoomManager.get(authorizationView);
Field defaultScale = zoomManager.getType().getDeclaredField("mDefaultScale");
defaultScale.setAccessible(true);
float sv = defaultScale.getFloat(zoomValue);
defaultScale.setFloat(zoomValue, xxx);
} catch (SecurityException e1) {
e1.printStackTrace();
} catch (IllegalArgumentException e1) {
e.printStackTrace();
} catch (IllegalAccessException e1) {
e.printStackTrace();
} catch (NoSuchFieldException e1) {
e1.printStackTrace();
try {
Field mProviderField = WebView.class.getDeclaredField("mProvider");
mProviderField.setAccessible(true);
//mProviderField.getClass()
Object webviewclassic = mProviderField.get(authorizationView);
Field zoomManager = webviewclassic.getClass().getDeclaredField("mZoomManager");
zoomManager.setAccessible(true);
Object zoomValue = zoomManager.get(webviewclassic);
Field defaultScale = zoomManager.getType().getDeclaredField("mDefaultScale");
defaultScale.setAccessible(true);
float sv = defaultScale.getFloat(zoomValue);
defaultScale.setFloat(zoomValue, xxx);
}catch(Exception e2)
{
e2.printStackTrace();
}
}
}
雖然可以拿到,并且設(shè)置成功,但是在我的手機(jī)上還是不能解決input 獲取焦點(diǎn)后自動(dòng)放大,
完了想了下,有個(gè)實(shí)現(xiàn)模式可以參考:當(dāng)input 獲取焦點(diǎn)時(shí),js調(diào)用java設(shè)置默認(rèn)放縮率,設(shè)置前保存原有值,失去焦點(diǎn)后重新設(shè)置原來(lái)值,不然跳轉(zhuǎn)到其他頁(yè)面的時(shí)候,你會(huì)發(fā)現(xiàn)比例不對(duì)了。:)。
因?yàn)榉糯蠛箅p擊還是還原回原來(lái)樣子。所以暫且不來(lái)糾結(jié)這個(gè)東西了。因?yàn)椴煌琣ndroid版本的如果webview實(shí)現(xiàn)不一致的話,這代碼就不起作用了 :)
posted on 2012-12-18 20:09
小果子 閱讀(8393)
評(píng)論(1) 編輯 收藏 引用 所屬分類:
學(xué)習(xí)筆記 、
Android & Ios