因此,我试图了解如何在View
持续动画的自定义中正确使用硬件加速(如果可用)。这是我的基本前提onDraw()
:
canvas.drawColor(mBackgroundColor);
for (Layer layer : mLayers) {
canvas.save();
canvas.translate(layer.x, layer.y);
//Draw that number of images in a grid, offset by -1
for (int i = -1; i < layer.xCount - 1; i++) {
for (int j = -1; j < layer.yCount - 1; j++) {
canvas.drawBitmap(layer.bitmap, layer.w * i, layer.h * j, null);
}
}
//If the layer's x has moved past its width, reset back to a seamless position
layer.x += ((difference * layer.xSpeed) / 1000f);
float xOverlap = layer.x % layer.w;
if (xOverlap > 0) {
layer.x = xOverlap;
}
//If the layer's y has moved past its height, reset back to a seamless position
layer.y += ((difference * layer.ySpeed) / 1000f);
float yOverlap = layer.y % layer.h;
if (yOverlap > 0) {
layer.y = yOverlap;
}
canvas.restore();
}
//Redraw the view
ViewCompat.postInvalidateOnAnimation(this);
我正在启用硬件层并在 中onAttachedToWindow()
禁用它们onDetachedFromWindow()
,但我试图了解我是否真的在使用它。本质上,i/j
调用的循环drawBitmap()
永远不会改变;唯一改变的是Canvas
翻译。是在Bitmap
幕后作为纹理自动保存到 GPU,还是我需要手动执行一些操作?