Comment pouvez-vous afficher le texte à l'envers avec un TextView dans Android?

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

  •  23-09-2019
  •  | 
  •  

Question

Comment pouvez-vous afficher le texte à l'envers avec un TextView dans Android?

Dans mon cas, j'ai un jeu de 2 joueurs, où ils jouent les uns des autres. Je veux afficher le test au deuxième joueur orienté vers eux.


C'était la solution que j'ai mise en œuvre après les conseils d'Aaronms

La classe qui fait le remplacement, 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(); 
    }
}

Et c'est ma disposition 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>
Était-ce utile?

La solution

Dans le fichier XML, ajoutez:

android:rotation = "180"

dans l'élément respectif pour afficher le texte à l'envers.

par exemple:

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

Autres conseils

Je n'ai pas essayé de le faire moi-même, mais je pense que cela devrait fonctionner.

Remplacez la méthode Onedraw de la vue, appelez son super transmission dans la toile, puis après avoir appelé la méthode rotative sur la toile qui lui est transmise, passant dans 180 ou math.pi, selon qu'elle fonctionne en degrés ou en radians.

Le code ci-dessous fonctionne parfaitement (merci au fait). Essayez d'ajouter le constructeur manquant. Si cela ne fonctionne pas mon problème est la version Android Main est 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();
    }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top