在Android中,如何使用TextView显示上下颠倒的文本?

29

如何在Android的TextView中显示倒置的文本?

在我的情况下,我有一个双人游戏,他们彼此对弈。我想将文本显示为第二个玩家朝向他们。


这是我根据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(); 
    }
}

这是我的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>
3个回答

31

在 XML 文件中添加:

android:rotation = "180"
在相应元素中显示倒置文本。
例如:
<TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:text="TextView" 
       android:rotation="180"/>

当我最初提出这个问题时,这不可用。我使用的是1.6版本,您知道它是否已经添加到较新的版本中了吗? - Ravedave
仅适用于API 11+。 - Johann
尝试一下,我的应用程序崩溃了。 - Abdulla Sirajudeen

6

我自己没有尝试过,但我认为这应该可行。

重写视图的onDraw方法,调用它的super传递canvas,然后在传递给它的canvas上调用rotate方法,传递180或Math.PI,具体取决于它是以度数还是弧度工作。


6
你也可以在Y轴上按比例缩放-1 :) - Romain Guy
谢谢,我已经删除了我的评论,因为那段代码没有起作用。感谢您的答案,它指引了我正确的方向。 - Ravedave
@Romain Guy - 将缩放比例设置为0、-1、px、py会导致文本翻转,就像照镜子一样。 - Ravedave
如何在Android Canvas ShapeDrawable中使用RectShape或其他形状显示文本?是否有任何解决方案? - LOG_TAG

3
下面的代码完美运行(顺便说一句,谢谢)。尝试添加缺失的构造函数。如果它不起作用,则问题可能是Android版本主要是2.1。
package p.c;

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

public class TextViewUD extends TextView {

    public TextViewUD(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public TextViewUD(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public TextViewUD(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        // TODO Auto-generated constructor stub
    }

    @Override
    protected void onDraw(Canvas canvas) {
        canvas.save();

        float py = this.getHeight()/2.0f;
        float px = this.getWidth()/2.0f;
        Log.d("testUD", String.format("w: %d h: %d ", this.getWidth(), this.getHeight()));
        Log.d("testUD", String.format("w: %f h: %f ", py, px));
        canvas.rotate(180, px, py);

        super.onDraw(canvas);

        canvas.restore();
    }
}

完美!在旧版的Android上也能正常工作! - Nick

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接