Mirrored text in textview?

2020-02-05 01:12发布

问题:

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