文本视图中的镜像文本?

发布于 2024-12-19 07:39:16 字数 722 浏览 3 评论 0原文

我正在尝试做一个简单地将一堆文本输出到android屏幕的应用程序,问题是它必须被镜像(将被视为“hud”)。

令人惊讶的是,在 android 4.0 中,您可以通过简单地使用 textview.setScaleX(-1) 来使用 textview 来完成此操作...在 4.0 之前我找不到太多。 textview.setTextScaleX(-1) 不起作用(实际上它有点起作用,但只有一个字符出现,尽管它是镜像的)。 4.0 方法也适用于我的手机(nexus 运行 cm9)。

我偶然发现了一些建议,例如使用 AndroidCharacter.Mirror() 没有成功,我似乎只剩下 3 个选项:

1)编写自定义(镜像)字体 2)学习如何重写onDraw(按照Android TextView镜像(hud)?) 3)将其全部绘制到画布上。

第一个是合理的,我可能可以做到,但它限制了我只能使用一种语言(或很多工作)。第二个+第三个我很迷失,尽管我很确定我可以从我找到的几个例子中找出答案(例如:在画布上绘制镜像文本)。

在我尝试 2 或 3 之前,还有其他我可能没有考虑过的选择吗?

Im trying to do an application that simply outputs a bunch of text to the android screen, the problem is is that it has to be mirrored (Will be viewed as a "hud").

Surprisingly, in android 4.0, you can do this with a textview by simply going textview.setScaleX(-1)... prior to 4.0 I cant find much. textview.setTextScaleX(-1) doesnt work (actually it kinda works, but only one char comes up, though it is mirrored). The 4.0 approach also works on my phone (nexus s running cm9).

I've stumbled across a few suggestions, such as using AndroidCharacter.Mirror() with no success and it seems im left with 3 options:

1) Write a custom (mirrored) font
2) learn how to override onDraw (as per Android TextView mirroring (hud)?)
3) paint it all onto a canvas.

The first is plausible and i could probably do it, but it limits me to a single language (or a lot of work). The second + third Im quite lost with though Im pretty sure I can figure it out from a few examples i've found (this for example: Drawing mirror text on canvas).

Before I do attempt 2 or 3, is there any other options i've perhaps not considered?

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

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

发布评论

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

评论(1

恏ㄋ傷疤忘ㄋ疼 2024-12-26 07:39:16

我很确定 4.0 之前的 TextView 是不可能的。

镜像的自定义 TextView 并不难:

package your.pkg;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

public class MirroredTextView extends TextView {

    public MirroredTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(getWidth(), 0);
        canvas.scale(-1, 1);
        super.onDraw(canvas);
    }

}

并用作:

<your.pkg.MirroredTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World" />

Im pretty sure it is not possible with the pre-4.0 TextView.

A mirrored custom TextView is not that hard:

package your.pkg;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.widget.TextView;

public class MirroredTextView extends TextView {

    public MirroredTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.translate(getWidth(), 0);
        canvas.scale(-1, 1);
        super.onDraw(canvas);
    }

}

And use as:

<your.pkg.MirroredTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文