So, I'm trying to figure out how I can correctly use hardware acceleration (when available) in a custom View that is constantly being animated. This is the basic premise of my 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 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 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);
I turned on the hardware levels in onAttachedToWindow() and disabled them in onDetachedFromWindow() , but I'm trying to figure out if I really use it. Essentially, the i/j loop that calls drawBitmap() never changes; the only thing that changes is the Canvas translation. Is Bitmap automatically saved on the GPU as a texture behind the scenes, or is there something I need to do manually to do this?
performance android android-view hardware-acceleration
kcoppock
source share