Android canvas setShadowLayer 大大降低性能
我正在 Android 上编写一个画布应用程序,并且希望添加阴影,但是当我将它们添加到绘画对象时,我注意到速度明显变慢。我的代码很简单,如下所示:
...
Paint paint = new Paint();
paint.setShadowLayer(8.f, 0, 0, 0xff000000); // this line makes terribly slow drawing
canvas.drawRect(left, top, right, bottom, paint);
我怎样才能使它更快?
I'm writing a canvas app on android and I'm looking to add shadows, but I've noticed a great slow-down when I add them to my paint object. My code is simple it looks like this:
...
Paint paint = new Paint();
paint.setShadowLayer(8.f, 0, 0, 0xff000000); // this line makes terribly slow drawing
canvas.drawRect(left, top, right, bottom, paint);
How can I make this faster?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在四处寻找加快大文本阴影速度的方法时,我偶然发现了这个问题和答案:
setShadowLayer Android API 差异
通过使用:
我极大地加快了应用程序中所有文本阴影的速度。
以下是我如何使用它的示例:
根据此文档:
https ://developer.android.com/guide/topics/graphics/hardware-accel.html
它表示您可以禁用视图的硬件加速。
我不知道为什么,但不知怎的,这神奇地加速了我的 TextView 阴影层。
我知道,我知道。 Canvas 或 Paint 类不存在该方法。因此,要回答具体问题(这样我就不会被每个人批评......),您可以将其设置在您打算绘制画布的视图上。像这样:
While digging around to find a way to speed up my large text shadows, I stumbled on this question and answer:
setShadowLayer Android API differences
By using:
I drastically sped up all the text shadows in my app.
Here is a sample of how I use it:
According to this doc:
https://developer.android.com/guide/topics/graphics/hardware-accel.html
It says that you can disable hardware acceleration for the view.
I don't know why, but somehow this magically speeds up my TextView shadow layers.
I know, I know. That method doesn't exist for the Canvas or Paint classes. So to answer the specific question (so I don't get blasted by everyone...), you could set that on the View that you intend to draw the canvas. Like this:
您可以使用以下代码获得几乎相同的结果:
mPaint.setMaskFilter(new BlurMaskFilter(20, BlurMaskFilter.Blur.OUTER));
You can achieve almost the same result using this code instead:
mPaint.setMaskFilter(new BlurMaskFilter(20, BlurMaskFilter.Blur.OUTER));
使用图像图标而不是绘制它:)
是的,阴影的成本很高。
Use an image icon instead of drawing it :)
Yes shadowing is costly.