在自定义视图onDraw中使用硬件层 [英] Using Hardware Layer in Custom View onDraw

查看:92
本文介绍了在自定义视图onDraw中使用硬件层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图了解如何在持续进行动画处理的自定义View中正确使用硬件加速(如果可用).这是我的onDraw():

So I'm trying to understand how I can properly use hardware acceleration (when available) in a custom View that is persistently animating. 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'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()中禁用它们,但是我试图了解我是否在实际使用它.本质上,调用drawBitmap()i/j循环永远不会改变;唯一更改的是Canvas翻译. Bitmap是作为场景后的纹理自动保存到GPU的,还是我需要手动执行一些操作?

I'm enabling hardware layers in onAttachedToWindow() and disabling them in onDetachedFromWindow(), but I'm trying to understand whether or not I'm actually using it. Essentially, the i/j loop that calls drawBitmap() never changes; the only thing that changes is the Canvas translation. Is the Bitmap automatically saved to the GPU as a texture behind the scenes, or is there something I need to do manually to do so?

推荐答案

您究竟在哪个视图上设置View.LAYER_TYPE_HARDWARE?如果要在包含上面显示的绘图代码的视图上设置硬件层,则会导致系统执行比必要更多的工作.由于您仅绘制位图,因此您无需在此处做任何事情.如果调用Canvas.drawBitmap(),则框架将代表您缓存生成的OpenGL纹理.

On what view(s) are you setting View.LAYER_TYPE_HARDWARE exactly? If you are setting a hardware layer on the view that contains the drawing code shown above, you are causing the system to do a lot more work than necessary. Since you are only drawing bitmaps you don't need to do anything here. If you call Canvas.drawBitmap() the framework will cache the resulting OpenGL texture on your behalf.

但是,您可以进一步优化代码.您可以使用子视图来代替调用drawBitmap().如果使用offset*()方法(或setX()/setY())移动这些子级,则框架将应用进一步的优化以避免再次调用draw()方法.

You could however optimize your code a little more. Instead of calling drawBitmap(), you could use child views. If you move these children using the offset*() methods (or setX()/setY()) the framework will apply further optimizations to avoid calling the draw() methods again.

通常,应在绘制成本高昂且内容不会经常更改的视图上设置硬件层(与您正在执行的操作几乎完全相反:)

In general, hardware layers should be set on views that are expensive to draw and whose content won't change often (so pretty much the opposite of what you're doing :)

这篇关于在自定义视图onDraw中使用硬件层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆