如何在Android中使用textview显示颠倒文本?

Rav*_*ave 29 android textview

如何在Android中使用textview显示颠倒文本?

在我的情况下,我有一个2人游戏,他们互相玩耍.我想向面向他们的第二个玩家展示测试.


这是我在AaronMs建议之后实施的解决方案

执行覆盖的类bab.foo.UpsideDownText

package bab.foo;

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

public class UpsideDownText extends TextView {

    //The below two constructors appear to be required
    public UpsideDownText(Context context) {
        super(context);
    }

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

    @Override
    public void onDraw(Canvas canvas) {
        //This saves off the matrix that the canvas applies to draws, so it can be restored later. 
        canvas.save(); 

        //now we change the matrix
        //We need to rotate around the center of our text
        //Otherwise it rotates around the origin, and that's bad. 
        float py = this.getHeight()/2.0f;
        float px = this.getWidth()/2.0f;
        canvas.rotate(180, px, py); 

        //draw the text with the matrix applied. 
        super.onDraw(canvas); 

        //restore the old matrix. 
        canvas.restore(); 
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我的XML布局:

<bab.foo.UpsideDownText 
    android:text="Score: 0" 
    android:id="@+id/tvScore" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:textColor="#FFFFFF" 
    >
</bab.foo.UpsideDownText>
Run Code Online (Sandbox Code Playgroud)

Nar*_*han 29

在xml文件中添加:

android:rotation = "180"
Run Code Online (Sandbox Code Playgroud)

在相应的元素中显示文本颠倒.

例如:

<TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:text="TextView" 
       android:rotation="180"/>
Run Code Online (Sandbox Code Playgroud)


Aar*_*onM 5

我自己没有尝试这样做,但我认为它应该有效.

覆盖视图的onDraw方法,调用它在画布中的超级传递,然后调用传递给它的画布上的rotate方法,传入180或Math.PI,具体取决于它是以度数还是弧度工作.

  • 您也可以在Y轴上按-1进行缩放:) (6认同)