如何在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();
}
}
这是我的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>
Hrm当我最初问这个问题不可用时。我在1.6版本,你知道这是否被添加到更高版本? – Ravedave 2013-07-23 14:43:50
只适用于API 11+ – AndroidDev 2013-07-30 15:41:04
试试看我的应用程序崩溃,, – 2015-12-28 03:06:59