Android4.1和4.2中webkit设置setDefaultZoom无效的解决方法
2013 年 4 月 23 日 Android4.1和4.2中webkit设置setDefaultZoom无效的解决方法有1条评论
表现为:设置默认缩放后,回到页面会自动缩放,但是当刷新后缩放效果消失,而锁屏后过一会再解锁则又会缩放,同理刷新又不缩放。出现这个问题是因为framework层webkit的zoom在对像素密度的处理上出现漏洞导致。在设置完成zoom后,即时的调用了更改,但在刷新后又通过setviewport方法重新设置了zoom,在这个设置中出现异常,mtk平台中直接通过获取屏幕像素密度来设置缩放,没有考虑到webview的自身配置而重新加以判断并发送更新zoom消息。具体代码如下所示:
// adjust the default scale to match the densityDpi
float adjust = 1.0f;
if (mViewportDensityDpi == -1) {
adjust = getFixedDisplayDensity(mContext);
} else if (mViewportDensityDpi > 0) {
adjust = (float) mContext.getResources().getDisplayMetrics().densityDpi
/ mViewportDensityDpi;
adjust = ((int) (adjust * 100)) / 100.0f;
}
// Remove any update density messages in flight.
// If the density is indeed different from WebView's default scale,
// a new message will be queued.
mWebViewClassic.mPrivateHandler.removeMessages(
WebViewClassic.UPDATE_ZOOM_DENSITY);
if (adjust != mWebViewClassic.getDefaultZoomScale()) {
Message.obtain(mWebViewClassic.mPrivateHandler,
WebViewClassic.UPDATE_ZOOM_DENSITY, adjust).sendToTarget();
}
float adjust = 1.0f;
if (mViewportDensityDpi == -1) {
adjust = getFixedDisplayDensity(mContext);
} else if (mViewportDensityDpi > 0) {
adjust = (float) mContext.getResources().getDisplayMetrics().densityDpi
/ mViewportDensityDpi;
adjust = ((int) (adjust * 100)) / 100.0f;
}
// Remove any update density messages in flight.
// If the density is indeed different from WebView's default scale,
// a new message will be queued.
mWebViewClassic.mPrivateHandler.removeMessages(
WebViewClassic.UPDATE_ZOOM_DENSITY);
if (adjust != mWebViewClassic.getDefaultZoomScale()) {
Message.obtain(mWebViewClassic.mPrivateHandler,
WebViewClassic.UPDATE_ZOOM_DENSITY, adjust).sendToTarget();
}
其中的getFixedDisplayDensity()方法内容如下:
static float getFixedDisplayDensity(Context context) {
// We make bad assumptions about multiplying and dividing density by 100,
// force them to be true with this hack
float density = context.getResources().getDisplayMetrics().density;
return ((int) (density * 100)) / 100.0f;
}
// We make bad assumptions about multiplying and dividing density by 100,
// force them to be true with this hack
float density = context.getResources().getDisplayMetrics().density;
return ((int) (density * 100)) / 100.0f;
}
修正方法如下所示,重新编译framework即可修复该问题。
// Modified by xdtianyu, [BEGIN][2013-04-23]
/*if (mViewportDensityDpi == -1) {
adjust = getFixedDisplayDensity(mContext);*/
if (mViewportDensityDpi == -1) {
if (mWebViewClassic != null && (int)(mWebViewClassic.getDefaultzoomScale() * 100) != 100) {
adjust = mWebViewClassic.getDefaultZoomScale();
}
// Modified by xdtianyu, [END][2013-04-23]
} else if (mViewportDensityDpi > 0) {
adjust = (float) mContext.getResources().getDisplayMetrics().densityDpi
/ mViewportDensityDpi;
adjust = ((int) (adjust * 100)) / 100.0f;
}
/*if (mViewportDensityDpi == -1) {
adjust = getFixedDisplayDensity(mContext);*/
if (mViewportDensityDpi == -1) {
if (mWebViewClassic != null && (int)(mWebViewClassic.getDefaultzoomScale() * 100) != 100) {
adjust = mWebViewClassic.getDefaultZoomScale();
}
// Modified by xdtianyu, [END][2013-04-23]
} else if (mViewportDensityDpi > 0) {
adjust = (float) mContext.getResources().getDisplayMetrics().densityDpi
/ mViewportDensityDpi;
adjust = ((int) (adjust * 100)) / 100.0f;
}
评论:0