2010-04-01 74 views
27

如何在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> 

回答

27

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

Hrm當我最初問這個問題不可用時。我在1.6版本,你知道這是否被添加到更高版本? – Ravedave 2013-07-23 14:43:50

+0

只適用於API 11+ – AndroidDev 2013-07-30 15:41:04

+0

試試看我的應用程序崩潰,, – 2015-12-28 03:06:59

5

我還沒有嘗試過這樣做,但我認爲它應該工作。

覆蓋視圖的onDraw方法,調用它的超級傳遞,然後調用傳遞給它的畫布上的rotate方法,傳遞180或Math.PI,具體取決於它是以度或弧度運行。

+0

任何人有想法,爲什麼不工作? – Ravedave 2010-04-01 23:29:00

+4

您也可以在Y軸上按-1縮放:) – 2010-04-02 03:04:45

+0

謝謝我刪除了我的評論,要求提供進一步的幫助,因爲該代碼無效:)。謝謝你的回答,它指明瞭我正確的方向。 – Ravedave 2010-04-02 04:39:57

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(); 
    } 
} 
+0

完美!對於較舊版本的Android也能正常工作! – Nick 2014-11-18 08:36:00