今天弄了下android webview下的幾個頁面。原先以為android 4+把 webview的viewport屬性忽略掉了。
但是今天弄了下。加了個 authorizationView.getSettings().setUseWideViewPort(true);
viewport 的幾個屬性重新起作用。(測試環境,4.0+的幾個版本)
但是又遇到幾個問題,就是html里有input的時候。獲取焦點的時候,android會重新縮放到原來模式,看源碼:
/**
* 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當要彈起鍵盤的時候,會判定當前的縮放比例與默認大小(我測試了下,我自己的版本的默認值是1.5),
當你網頁viewport設置initial-scale=0.5時,當input 獲取焦點的時候,android會放大到原來模式,不是我們想要的,網上查了下相關,
有個解決方案:
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();
}
}
}
});
但是作者碰到另外一個問題,引用自原話:
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
完了我自己測試了,發現此方案解決不了。但是引出了另外一問題,就是不用android版本下的webview實現是不一樣的,其實看代碼就能看出,
原先webview有mDefaultScale字段,但是后來應該挪到mZoomManager里去了,但是我發現我手機上webview 實現和作者遇到的問題一樣,只有mProvider成員,
沒有mZoomManager,所以只能尋求源碼,千辛萬苦,終于找到
http://androidxref.com/4.2_r1/xref/frameworks/base/core/java/android/webkit/WebViewClassic.java,
mProvider 其實類型就是WebViewClassic(自己看下源碼實現),簡要提下,WebProvider只是一個接口,作為WebView的一個成員,
創建時用了factory來,完了看下幾個工廠類,最后是webviewclassic實例)。
對于Jerry Bean 4.2這個版本(我一個手機就是自己刷的rom),webview的實現又換了個,所以要拿到默認縮放的成員,如下:
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();
}
}
}
雖然可以拿到,并且設置成功,但是在我的手機上還是不能解決input 獲取焦點后自動放大,
完了想了下,有個實現模式可以參考:當input 獲取焦點時,js調用java設置默認放縮率,設置前保存原有值,失去焦點后重新設置原來值,不然跳轉到其他頁面的時候,你會發現比例不對了。:)。
因為放大后雙擊還是還原回原來樣子。所以暫且不來糾結這個東西了。因為不同android版本的如果webview實現不一致的話,這代碼就不起作用了 :)
posted on 2012-12-18 20:09
小果子 閱讀(8379)
評論(1) 編輯 收藏 引用 所屬分類:
學習筆記 、
Android & Ios