Mirrored text in textview?

允我心安 提交于 2019-12-17 18:44:34

问题


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?


回答1:


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" />


来源:https://stackoverflow.com/questions/8381329/mirrored-text-in-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!