WallpaperManager的问题二,竖屏壁纸两侧会有黑边。这个问题就是具体的壁纸绘制的问题了,简单来讲就是壁纸本身没有被拉伸到桌面所需要的宽度,在ImageWallpaper中进行绘制时,会根据Wallpaper的需要宽度、壁纸的宽度来指定壁纸在什么位置来进行绘制,黑边就是由于画布太宽,留出黑边绘制壁纸,造成这个问题。
void drawFrameLocked() {
if (!mVisible) {
if (DEBUG) {
Log.d(TAG, "Suppressed drawFrame since wallpaper is not visible.");
}
return;
}
if (!mRedrawNeeded && !mOffsetsChanged) {
if (DEBUG) {
Log.d(TAG, "Suppressed drawFrame since redraw is not needed "
+ "and offsets have not changed.");
}
return;
}
if (mBackgroundWidth < 0 || mBackgroundHeight < 0) {
// If we don't yet know the size of the wallpaper bitmap,
// we need to get it now.
updateWallpaperLocked();
}
SurfaceHolder sh = getSurfaceHolder();
final Rect frame = sh.getSurfaceFrame();
final int dw = frame.width();
final int dh = frame.height();
final int availw = dw - mBackgroundWidth;
final int availh = dh - mBackgroundHeight;
int xPixels = availw < 0 ? (int)(availw * mXOffset + .5f) : (availw / 2);
int yPixels = availh < 0 ? (int)(availh * mYOffset + .5f) : (availh / 2);
mOffsetsChanged = false;
if (!mRedrawNeeded && xPixels == mLastXTranslation && yPixels == mLastYTranslation) {
if (DEBUG) {
Log.d(TAG, "Suppressed drawFrame since the image has not "
+ "actually moved an integral number of pixels.");
}
return;
}
mRedrawNeeded = false;
mLastXTranslation = xPixels;
mLastYTranslation = yPixels;
if (mBackground == null) {
// If we somehow got to this point after we have last flushed
// the wallpaper, well we really need it to draw again. So
// seems like we need to reload it. Ouch.
updateWallpaperLocked();
}
if (mIsHwAccelerated) {
if (!drawWallpaperWithOpenGL(sh, availw, availh, xPixels, yPixels)) {
drawWallpaperWithCanvas(sh, availw, availh, xPixels, yPixels);
}
} else {
drawWallpaperWithCanvas(sh, availw, availh, xPixels, yPixels);
}
if (FIXED_SIZED_SURFACE) {
// If the surface is fixed-size, we should only need to
// draw it once and then we'll let the window manager
// position it appropriately. As such, we no longer needed
// the loaded bitmap. Yay!
mBackground = null;
mWallpaperManager.forgetLoadedWallpaper();
}
}
最后一个思考就是为什么有黑边后,重启后就没有了那?
这个就需要把整个壁纸管理、绘制的方式综合来看了。
1.首先,设置一张壁纸后,/data/system/wallpaper_info.xml保存的是该壁纸的真是宽高。
2.开机后,load壁纸信息时,发现宽度比高度小,所以对壁纸的宽高进行修改,返回的宽度与高度相等。举例同为800,返回给ImageWallpaper,ImageWallpaper据此拉伸壁纸图片为800x800。
3.在标准桌面中,指定宽度为480*2=960,要求ImageWallpaper进行拉伸绘制,ImageWallpaper通过计算,留出足够黑边后绘制壁纸,此时出现黑边。同时WallpaperManager保存宽度到wallpaper_info.xml中,也就是wallpaper_info中的宽度变为960。
4.再次启动后,返回的壁纸宽度变为960,在ImageWallpaper中按照960进行拉伸图片,所以再次开机后,已经没有黑边了,可以全屏显示。
更多Android相关信息见Android 专题页面 http://www.linuxidc.com/topicnews.aspx?tid=11
