我一直在尝试为我的动态壁纸添加半透明的颜色叠加层。无论我尝试什么,我的加载时间都非常慢。我尝试过drawARGB,在绘画中设置alpha,并逐像素地创建alpha位图。所有这些方法都会导致加载时间变慢。继承我的代码:
public void draw()
{
long borderLoadTime = 0;
long drawStart = 0;
long drawEnd = 0;
long whileLoopEnd = 0;
long overlayStart = 0;
long overlayEnd = 0;
long holderTime = 0;
long lockTime = 0;
long start = System.currentTimeMillis();
long drawCounter = 0;
Canvas c = new Canvas();
c = holder.lockCanvas();
long canvasLoadTime = System.currentTimeMillis();
if (c != null)
{
if (borderToggle)
c.drawRGB(InfinityService.BORDER_RED,
InfinityService.BORDER_GREEN,
InfinityService.BORDER_BLUE);
else
c.drawColor(Color.BLACK);
borderLoadTime = System.currentTimeMillis();
synchronized (grid)
{
lockTime = System.currentTimeMillis();
for (GridRow row : grid.getImageGrid().values())
{
for (Box box : row.getRow().values())
{
drawStart = System.currentTimeMillis();
if (box.getImgLeft() < getWidth()
&& box.getImgTop() < getHeight()
&& box.getImgRight() > 0
&& box.getImgBottom() > 0)
{
c.drawBitmap(serviceContext.getImages().get(box.getPhoto()),
Math.round(box.getImgLeft()),
Math.round(box.getImgTop()), p);
}
drawEnd = System.currentTimeMillis();
drawCounter += drawEnd - drawStart;
}
}
whileLoopEnd = System.currentTimeMillis();
}
overlayStart = System.currentTimeMillis();
if (overlayToggle)
c.drawARGB(InfinityService.OVERLAY_ALPHA,
InfinityService.OVERLAY_RED,
InfinityService.OVERLAY_GREEN,
InfinityService.OVERLAY_BLUE);
overlayEnd = System.currentTimeMillis();
holder.unlockCanvasAndPost(c);
holderTime = System.currentTimeMillis();
}
Log.d(InfinityService.TAG, "CANVAS LOAD: "
+ (canvasLoadTime - start) + " ms");
Log.d(InfinityService.TAG, "BORDER LOAD: "
+ (borderLoadTime - canvasLoadTime) + " ms");
Log.d(InfinityService.TAG, "BITMAP DRAW TIME: " + drawCounter
+ " ms");
Log.d(InfinityService.TAG, "LOOP TIME: "
+ ((whileLoopEnd - lockTime) - drawCounter) + " ms");
Log.d(InfinityService.TAG, "OVERLAY LOAD: "
+ (overlayEnd - overlayStart) + " ms");
Log.d(InfinityService.TAG, "HOLDER UNLOCK: "
+ (holderTime - overlayEnd) + " ms");
Log.d(InfinityService.TAG,
"LOCK TIME: "
+ ((overlayStart - borderLoadTime) - (whileLoopEnd - lockTime))
+ " ms");
Log.d(InfinityService.TAG,
"------------------------------------");
}
现在这里是绘制帧的一些日志条目。请注意drawARGB如何导致这么慢的时间。此外,画布锁定和解锁调用也需要很长时间才能完成。
11-10 16:09:34.803: D/InfinityService(10233): CANVAS LOAD: 0 ms
11-10 16:09:34.803: D/InfinityService(10233): BORDER LOAD: 1 ms
11-10 16:09:34.803: D/InfinityService(10233): BITMAP DRAW TIME: 4 ms
11-10 16:09:34.803: D/InfinityService(10233): LOOP TIME: 0 ms
11-10 16:09:34.803: D/InfinityService(10233): OVERLAY LOAD: 8 ms
11-10 16:09:34.803: D/InfinityService(10233): HOLDER UNLOCK: 1 ms
11-10 16:09:34.803: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:34.803: D/InfinityService(10233): ------------------------------------
11-10 16:09:34.873: D/InfinityService(10233): CANVAS LOAD: 1 ms
11-10 16:09:34.873: D/InfinityService(10233): BORDER LOAD: 0 ms
11-10 16:09:34.873: D/InfinityService(10233): BITMAP DRAW TIME: 4 ms
11-10 16:09:34.873: D/InfinityService(10233): LOOP TIME: 0 ms
11-10 16:09:34.873: D/InfinityService(10233): OVERLAY LOAD: 17 ms
11-10 16:09:34.873: D/InfinityService(10233): HOLDER UNLOCK: 3 ms
11-10 16:09:34.873: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:34.873: D/InfinityService(10233): ------------------------------------
11-10 16:09:34.953: D/InfinityService(10233): CANVAS LOAD: 1 ms
11-10 16:09:34.953: D/InfinityService(10233): BORDER LOAD: 2 ms
11-10 16:09:34.953: D/InfinityService(10233): BITMAP DRAW TIME: 3 ms
11-10 16:09:34.963: D/InfinityService(10233): LOOP TIME: 3 ms
11-10 16:09:34.963: D/InfinityService(10233): OVERLAY LOAD: 24 ms
11-10 16:09:34.963: D/InfinityService(10233): HOLDER UNLOCK: 3 ms
11-10 16:09:34.963: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:34.963: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.044: D/InfinityService(10233): CANVAS LOAD: 0 ms
11-10 16:09:35.044: D/InfinityService(10233): BORDER LOAD: 1 ms
11-10 16:09:35.044: D/InfinityService(10233): BITMAP DRAW TIME: 4 ms
11-10 16:09:35.044: D/InfinityService(10233): LOOP TIME: 2 ms
11-10 16:09:35.044: D/InfinityService(10233): OVERLAY LOAD: 24 ms
11-10 16:09:35.044: D/InfinityService(10233): HOLDER UNLOCK: 4 ms
11-10 16:09:35.054: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:35.054: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.164: D/InfinityService(10233): CANVAS LOAD: 1 ms
11-10 16:09:35.164: D/InfinityService(10233): BORDER LOAD: 1 ms
11-10 16:09:35.164: D/InfinityService(10233): BITMAP DRAW TIME: 5 ms
11-10 16:09:35.164: D/InfinityService(10233): LOOP TIME: 2 ms
11-10 16:09:35.164: D/InfinityService(10233): OVERLAY LOAD: 43 ms
11-10 16:09:35.164: D/InfinityService(10233): HOLDER UNLOCK: 15 ms
11-10 16:09:35.164: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:35.164: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.234: D/InfinityService(10233): CANVAS LOAD: 0 ms
11-10 16:09:35.234: D/InfinityService(10233): BORDER LOAD: 1 ms
11-10 16:09:35.234: D/InfinityService(10233): BITMAP DRAW TIME: 2 ms
11-10 16:09:35.234: D/InfinityService(10233): LOOP TIME: 1 ms
11-10 16:09:35.234: D/InfinityService(10233): OVERLAY LOAD: 18 ms
11-10 16:09:35.234: D/InfinityService(10233): HOLDER UNLOCK: 2 ms
11-10 16:09:35.234: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:35.234: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.304: D/InfinityService(10233): CANVAS LOAD: 0 ms
11-10 16:09:35.304: D/InfinityService(10233): BORDER LOAD: 1 ms
11-10 16:09:35.304: D/InfinityService(10233): BITMAP DRAW TIME: 2 ms
11-10 16:09:35.304: D/InfinityService(10233): LOOP TIME: 1 ms
11-10 16:09:35.304: D/InfinityService(10233): OVERLAY LOAD: 19 ms
11-10 16:09:35.304: D/InfinityService(10233): HOLDER UNLOCK: 1 ms
11-10 16:09:35.304: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:35.304: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.374: D/InfinityService(10233): CANVAS LOAD: 0 ms
11-10 16:09:35.374: D/InfinityService(10233): BORDER LOAD: 1 ms
11-10 16:09:35.374: D/InfinityService(10233): BITMAP DRAW TIME: 3 ms
11-10 16:09:35.374: D/InfinityService(10233): LOOP TIME: 0 ms
11-10 16:09:35.374: D/InfinityService(10233): OVERLAY LOAD: 7 ms
11-10 16:09:35.374: D/InfinityService(10233): HOLDER UNLOCK: 13 ms
11-10 16:09:35.374: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:35.374: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.444: D/InfinityService(10233): CANVAS LOAD: 1 ms
11-10 16:09:35.444: D/InfinityService(10233): BORDER LOAD: 0 ms
11-10 16:09:35.444: D/InfinityService(10233): BITMAP DRAW TIME: 3 ms
11-10 16:09:35.444: D/InfinityService(10233): LOOP TIME: 0 ms
11-10 16:09:35.444: D/InfinityService(10233): OVERLAY LOAD: 8 ms
11-10 16:09:35.444: D/InfinityService(10233): HOLDER UNLOCK: 11 ms
11-10 16:09:35.444: D/InfinityService(10233): LOCK TIME: 1 ms
11-10 16:09:35.444: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.614: D/InfinityService(10233): CANVAS LOAD: 1 ms
11-10 16:09:35.614: D/InfinityService(10233): BORDER LOAD: 0 ms
11-10 16:09:35.614: D/InfinityService(10233): BITMAP DRAW TIME: 4 ms
11-10 16:09:35.614: D/InfinityService(10233): LOOP TIME: 0 ms
11-10 16:09:35.614: D/InfinityService(10233): OVERLAY LOAD: 117 ms
11-10 16:09:35.614: D/InfinityService(10233): HOLDER UNLOCK: 9 ms
11-10 16:09:35.614: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:35.614: D/InfinityService(10233): ------------------------------------
11-10 16:09:35.844: D/InfinityService(10233): CANVAS LOAD: 0 ms
11-10 16:09:35.844: D/InfinityService(10233): BORDER LOAD: 1 ms
11-10 16:09:35.844: D/InfinityService(10233): BITMAP DRAW TIME: 3 ms
11-10 16:09:35.844: D/InfinityService(10233): LOOP TIME: 0 ms
11-10 16:09:35.844: D/InfinityService(10233): OVERLAY LOAD: 67 ms
11-10 16:09:35.844: D/InfinityService(10233): HOLDER UNLOCK: 108 ms
11-10 16:09:35.844: D/InfinityService(10233): LOCK TIME: 0 ms
11-10 16:09:35.844: D/InfinityService(10233): ------------------------------------
我注意到使用alpha颜色叠加时我的两台设备的性能都有相当大的下降。
作为2D android画布绘图的缺点,这是不可避免的吗? openGL会帮助解决这个问题吗?