¿Cómo puede mostrar el texto al revés con un TextView en Android?
Pregunta
¿Cómo puede mostrar el texto al revés con un TextView en Android?
En mi caso tengo un juego de 2 jugadores, donde juegan uno frente al otro. Quiero mostrar la prueba al segundo jugador orientado a ellos.
Esta fue la solución que implementé después del consejo de Aaronms
La clase que hace la anulación, bab.foo.upsididownText
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();
}
}
Y este es mi diseño 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>
Solución
En el archivo XML Agregar:
android:rotation = "180"
en el elemento respectivo para mostrar el texto al revés.
por ejemplo:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView"
android:rotation="180"/>
Otros consejos
No he intentado hacer esto yo mismo, pero creo que debería funcionar.
Anule el método de NDRAW de la vista, llame a su súper aprobación en el lienzo, luego después de llamar al método de rotación en el lienzo que se le pasó, pasando en 180 o Math.pi, dependiendo de si funciona en grados o radianos.
El siguiente código funciona perfectamente (gracias por cierto). Intenta agregar el constructor que falta. Si no funciona, el problema es la versión principal de Android es 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();
}
}