【问题标题】:How can you display upside down text with a textview in Android?如何在 Android 中使用 textview 显示颠倒的文本?
【发布时间】:2010-04-01 05:14:03
【问题描述】:

如何在 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(); 
    }
}

这是我的 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>

【问题讨论】:

    标签: android textview


    【解决方案1】:

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

    【讨论】:

    • Hrm 当我最初问这个不可用的问题时。我在 1.6,你知道这是否被添加到以后的版本中?
    • 仅在 API 11+ 中可用
    • 试试我的应用程序崩溃了,,
    【解决方案2】:

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

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

    【讨论】:

    【解决方案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!
    猜你喜欢
    • 1970-01-01
    • 2012-10-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-03
    • 2014-05-15
    • 1970-01-01
    相关资源
    最近更新 更多