Wie können Sie auf dem Kopf mit einem Textview in Android auf den Kopf stellen?

StackOverflow https://stackoverflow.com/questions/2558257

  •  23-09-2019
  •  | 
  •  

Frage

Wie können Sie auf dem Kopf mit einem Textview in Android auf den Kopf stellen?

In meinem Fall habe ich ein 2 -Spieler -Spiel, bei dem sie sich gegenseitig gegenüberspielen. Ich möchte Test auf den zweiten Spieler anzeigen, der an ihnen orientiert ist.


Dies war die Lösung, die ich nach Aaronms Rat implementiert habe

Die Klasse, die den übergeordneten, bab.foo.upsidedowntext macht

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

Und das ist mein XML -Layout:

<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>
War es hilfreich?

Lösung

In der XML -Datei fügen Sie hinzu:

android:rotation = "180"

im jeweiligen Element, um Text auf den Kopf zu stellen.

zum Beispiel:

<TextView
       android:id="@+id/textView1"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:gravity="center"
       android:text="TextView" 
       android:rotation="180"/>

Andere Tipps

Ich habe nicht versucht, das selbst zu tun, aber ich denke, es sollte funktionieren.

Überschreiben Sie die ONDRAW -Methode der Ansicht, rufen Sie sie in der Leinwand auf, und nach dem Aufruf der Rotate -Methode auf der Leinwand, die an sie entweder 180 oder in Math.Pi übergeht, je nachdem, ob sie in Grad oder Radians funktioniert.

Der folgende Code funktioniert perfekt (danke übrigens). Versuchen Sie, den fehlenden Konstruktor hinzuzufügen. Wenn es nicht funktioniert, ist mein Problem die Android -Version, Main ist 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();
    }
}
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top