在 onDraw() 中保存画布;

发布于 2024-12-05 10:03:27 字数 800 浏览 0 评论 0原文

我试图将 Canvas 对象保存在 onDraw() 方法中。 这是因为我在 onDraw 方法中使用了 foreach 循环,结果是: canvas.DrawText (textitem, x,y, textpaint);
(我必须这样做,因为我在屏蔽区域周围绘制文本)

我现在尝试的是:

@Override
public void onDraw(Canvas canvas)
{
if (hasrun = false)
        {
            for(CustomTextViewDrawItem item : drawItemList)
            {
                canvas.drawText(item.Text, item.X, item.Y, textPaint);
            }

            if (eLabel.backgroundGradient != null)
            {
                canvas.drawPath(path, fillPaint);
            }
            canvas.save();
            savedCanvas = canvas ;
        }
        else 
        {
            canvas = savedCanvas; 
        }

        hasrun = true; 
        super.onDraw(canvas);
}

调试时我看到它看起来不错,但结果是空的。 让它发挥作用的最佳方法是什么?

I'm trying to save the Canvas object in a onDraw() method.
This is because i'm using a foreach loop in the onDraw method resulting in :
canvas.DrawText (textitem , x,y, textpaint);
(i have to do this because im drawing the text around a masked area)

what im trying now is this :

@Override
public void onDraw(Canvas canvas)
{
if (hasrun = false)
        {
            for(CustomTextViewDrawItem item : drawItemList)
            {
                canvas.drawText(item.Text, item.X, item.Y, textPaint);
            }

            if (eLabel.backgroundGradient != null)
            {
                canvas.drawPath(path, fillPaint);
            }
            canvas.save();
            savedCanvas = canvas ;
        }
        else 
        {
            canvas = savedCanvas; 
        }

        hasrun = true; 
        super.onDraw(canvas);
}

when debugging I see it looks ok, but comes out empty .
what would be the best way to get this working ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

我是男神闪亮亮 2024-12-12 10:03:27

也许是因为这个:

if (hasrun = false)

我猜你打算这样做:

if (hasrun == false)

Maybe that is because of this:

if (hasrun = false)

I guess you intend to do this instead:

if (hasrun == false)
独孤求败 2024-12-12 10:03:27

您可以尝试保存位图:(我认为最好在方法的开头调用 super.onDraw(canvas); ,因为与视图相关的绘图将位于顶部)


@Override
public void onDraw(Canvas canvas)
{
        super.onDraw(canvas);
        if (savedBitmap==null){
            savedBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
            Canvas canvasToSave = new Canvas(savedBitmap)
            for(CustomTextViewDrawItem item : drawItemList){
                canvasToSave.drawText(item.Text, item.X, item.Y, textPaint);
            }

            if (eLabel.backgroundGradient != null){
                canvasToSave.drawPath(path, fillPaint);
            }
        }
        canvas.drawBitmap(savedBitmap, 0, 0, new Paint());
}

You can try to save the bitmap: (I think it is better to call the super.onDraw(canvas); at the start on the method, because your view related drawing will be on top)


@Override
public void onDraw(Canvas canvas)
{
        super.onDraw(canvas);
        if (savedBitmap==null){
            savedBitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
            Canvas canvasToSave = new Canvas(savedBitmap)
            for(CustomTextViewDrawItem item : drawItemList){
                canvasToSave.drawText(item.Text, item.X, item.Y, textPaint);
            }

            if (eLabel.backgroundGradient != null){
                canvasToSave.drawPath(path, fillPaint);
            }
        }
        canvas.drawBitmap(savedBitmap, 0, 0, new Paint());
}

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