我想用多个大型位图优化视差滚动视图.在我的Nexus 5上,一切都很顺利,Traceview转储如下所示:
doFrame()方法使用~18 ms完成.
但是,在使用我的Nexus 7或Android 6 Emulator(Genymotion)时,Traceview转储如下所示:
运行完全相同的应用程序时,nSyncAndDrawFrame方法现在需要大约300毫秒.
有趣的代码部分位于视差视图的onDraw()中:
for (int i = 0; i < parallaxConfigManager.getNumberOfLayers(); i++) { Bitmap layer = parallaxConfigManager.getLayer(i); float dx = (offset * parallaxConfigManager.getScrollSpeedFactorForLayer(i) * imageScaleFactor); int offset = Math.round(-parallaxConfigManager.getBoardOffset(i) + dx); srcRect.offsetTo(offset,0); int realWidth = getRealWidth(srcRect,layer.getWidth()); float scaleFactor = destRect.width() / (float) srcRect.width(); if (realWidth < srcRect.width()) { destRect.left = (int) (scaleFactor * Math.max(0,-srcRect.left)); destRect.right = destRect.left + (int) (scaleFactor * realWidth); } destRect.bottom = Math.min(screenHeight,(int) (scaleFactor * layer.getHeight())); canvas.drawBitmap(layer,srcRect,destRect,paint); destRect.left = 0; destRect.right = screenWidth; }
但是,这段代码足够快.缓慢的部分是在Androids本机nSyncAndDrawFrame()中.