在自定义活动中绘制 TextView?

发布于 2024-11-08 11:39:55 字数 727 浏览 0 评论 0原文

我正在实现一个自定义View,我需要在其中绘制一些文本。文本必须适合一个盒子(所以我必须将其分解并使其适合)。因此,我认为我可以使用 TextView 并将其绘制在我的自定义 View 中。这是我尝试过的:

canvas.drawRoundRect(rect, eventRadius, eventRadius, eventBg);

canvas.save();
canvas.clipRect(rect);
TextView tv = new TextView(getContext());
tv.setText(e.getSummary());
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
tv.layout(0, 0, (int) (rect.right - rect.left), (int) (rect.bottom - rect.top));
tv.draw(canvas);
canvas.restore();

但是,什么也没有出现。我知道 rect 没问题,因为第一个 drawRoundRect 工作正常。我缺少什么?有更好的办法吗?也许我应该扩展 ViewGroup 来代替?我不确定那会如何运作。

I'm implementing a custom View, and I need to draw some text in it. The text has to fit in a box (so I have to break it up and make it fit). Because of this, I thought I could use a TextView and draw it inside my custom View. Here's what I've tried:

canvas.drawRoundRect(rect, eventRadius, eventRadius, eventBg);

canvas.save();
canvas.clipRect(rect);
TextView tv = new TextView(getContext());
tv.setText(e.getSummary());
tv.setTextColor(Color.BLACK);
tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
tv.layout(0, 0, (int) (rect.right - rect.left), (int) (rect.bottom - rect.top));
tv.draw(canvas);
canvas.restore();

However, nothing is showing up. I know rect is OK because the first drawRoundRect works fine. What am I missing? Is there a better way? Maybe I should extend ViewGroup instead? I'm not sure how that would work.

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

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

发布评论

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

评论(2

德意的啸 2024-11-15 11:39:55

要在不包装的情况下执行此操作:

canvas.drawText(yourText, xCoord,YCoord, YourPaint);

通过包装

protected void onDraw(Canvas canvas) {

        TextPaint tp=new TextPaint();
        tp.setARGB(255, 255, 0, 0);
        tp.setTextSize(12);
        StaticLayout sl=new StaticLayout("THIS IS SOME LONGER TEXT",tp,60,Layout.Alignment.ALIGN_NORMAL,1f,0f,true);
        sl.draw(canvas);
        }

http://developer.android 来执行此操作。 com/reference/android/text/StaticLayout.html

To do it without wrapping:

canvas.drawText(yourText, xCoord,YCoord, YourPaint);

to do it with wrapping

protected void onDraw(Canvas canvas) {

        TextPaint tp=new TextPaint();
        tp.setARGB(255, 255, 0, 0);
        tp.setTextSize(12);
        StaticLayout sl=new StaticLayout("THIS IS SOME LONGER TEXT",tp,60,Layout.Alignment.ALIGN_NORMAL,1f,0f,true);
        sl.draw(canvas);
        }

http://developer.android.com/reference/android/text/StaticLayout.html

明明#如月 2024-11-15 11:39:55

我当前的解决方案是这样的:

TextView textView = new TextView(getContext());
int width = (int) (rect.right - rect.left);
int height = (int) (rect.bottom - rect.top);
textView.layout(0, 0, width, height);
textView.setText(e.getSummary());
Bitmap bitmapText = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvasText = new Canvas(bitmapText);
textView.draw(canvasText);

canvas.drawBitmap(bitmapText, rect.left, rect.top, null);

感觉很脏(而且有些不理想),但它有效。如果有人没有提出更好的解决方案,我将在几天内将其标记为已接受。

My current solution is this:

TextView textView = new TextView(getContext());
int width = (int) (rect.right - rect.left);
int height = (int) (rect.bottom - rect.top);
textView.layout(0, 0, width, height);
textView.setText(e.getSummary());
Bitmap bitmapText = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvasText = new Canvas(bitmapText);
textView.draw(canvasText);

canvas.drawBitmap(bitmapText, rect.left, rect.top, null);

It feels dirty (and somewhat un-optimal), but it works. If someone doesn't come up with a better solution I'm going to mark this as accepted in a couple of days.

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