How can you display upside down text with a textview in Android?
如何在Android中使用textview显示颠倒文本?
In my case I have a 2 player game, where they play across from each other. I want to display test to the second player oriented to them.
在我的情况下,我有一个2人游戏,他们互相玩耍。我想向面向他们的第二个玩家展示测试。
This was the solution I implemented after AaronMs advice
这是我在AaronMs建议之后实施的解决方案
The class that does the overriding, bab.foo.UpsideDownText
执行覆盖的类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();
}
}
And this is my XML layout:
这是我的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 个解决方案
#1
28
in the xml file add:
在xml文件中添加:
android:rotation = "180"
in the respective element to display text upside down.
在相应的元素中显示文本颠倒。
for example:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView"
android:rotation="180"/>
#2
5
I haven't tried doing this myself, but I think it should work.
我自己没有试过这样做,但我认为它应该有效。
Override the view's onDraw method, call it's super passing in the canvas, then after call the rotate method on the canvas passed to it, passing in either 180 or Math.PI, depending on whether it works in degrees or radians.
覆盖视图的onDraw方法,调用它在画布中的超级传递,然后调用传递给它的画布上的rotate方法,传入180或Math.PI,具体取决于它是以度数还是弧度工作。
#3
3
the below code is working perfectly (thanks by the way). try to add the missing constructor. if it is not working my the problem is android version main is 2.1
下面的代码工作正常(感谢顺便说一下)。尝试添加缺少的构造函数。如果它不工作我的问题是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();
}
}
#1
28
in the xml file add:
在xml文件中添加:
android:rotation = "180"
in the respective element to display text upside down.
在相应的元素中显示文本颠倒。
for example:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView"
android:rotation="180"/>
#2
5
I haven't tried doing this myself, but I think it should work.
我自己没有试过这样做,但我认为它应该有效。
Override the view's onDraw method, call it's super passing in the canvas, then after call the rotate method on the canvas passed to it, passing in either 180 or Math.PI, depending on whether it works in degrees or radians.
覆盖视图的onDraw方法,调用它在画布中的超级传递,然后调用传递给它的画布上的rotate方法,传入180或Math.PI,具体取决于它是以度数还是弧度工作。
#3
3
the below code is working perfectly (thanks by the way). try to add the missing constructor. if it is not working my the problem is android version main is 2.1
下面的代码工作正常(感谢顺便说一下)。尝试添加缺少的构造函数。如果它不工作我的问题是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();
}
}