Android canvas setShadowLayer 大大降低性能

发布于 2024-12-25 06:46:34 字数 292 浏览 3 评论 0原文

我正在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

九公里浅绿 2025-01-01 06:46:34

在四处寻找加快大文本阴影速度的方法时,我偶然发现了这个问题和答案:

setShadowLayer Android API 差异

通过使用:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

我极大地加快了应用程序中所有文本阴影的速度。

以下是我如何使用它的示例:

/**
 * Set a backlight (shadow) on the passed TextView.
 * @param textView
 */
void setBacklight(TextView textView) {
    if (textView != null) {
        float textSize = textView.getTextSize();

        textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        textView.setShadowLayer(textSize,0,0,getColor(R.color.color_backlight));
    }
}

根据此文档:

https ://developer.android.com/guide/topics/graphics/hardware-accel.html

它表示您可以禁用视图的硬件加速。

我不知道为什么,但不知怎的,这神奇地加速了我的 TextView 阴影层。

我知道,我知道。 Canvas 或 Paint 类不存在该方法。因此,要回答具体问题(这样我就不会被每个人批评......),您可以将其设置在您打算绘制画布的视图上。像这样:

void inTheShadows(View view) {
    float left = 0f;
    float top = 0f;
    float right = 10f;
    float bottom = 10f;
    Canvas canvas = new Canvas();
    Paint paint = new Paint();

    paint.setShadowLayer(8.f, 0, 0, 0xff000000);

    canvas.drawRect(left, top, right, bottom, paint);

    view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

    view.onDrawForeground(canvas);
}

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:

myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

I drastically sped up all the text shadows in my app.

Here is a sample of how I use it:

/**
 * Set a backlight (shadow) on the passed TextView.
 * @param textView
 */
void setBacklight(TextView textView) {
    if (textView != null) {
        float textSize = textView.getTextSize();

        textView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

        textView.setShadowLayer(textSize,0,0,getColor(R.color.color_backlight));
    }
}

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:

void inTheShadows(View view) {
    float left = 0f;
    float top = 0f;
    float right = 10f;
    float bottom = 10f;
    Canvas canvas = new Canvas();
    Paint paint = new Paint();

    paint.setShadowLayer(8.f, 0, 0, 0xff000000);

    canvas.drawRect(left, top, right, bottom, paint);

    view.setLayerType(View.LAYER_TYPE_SOFTWARE,null);

    view.onDrawForeground(canvas);
}
飘过的浮云 2025-01-01 06:46:34

您可以使用以下代码获得几乎相同的结果:

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));

妥活 2025-01-01 06:46:34

使用图像图标而不是绘制它:)

是的,阴影的成本很高。

Use an image icon instead of drawing it :)

Yes shadowing is costly.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文