Подтвердить что ты не робот

Как вы можете отображать перевернутый текст с текстовым просмотром в Android?

Как вы можете отображать перевернутый текст с текстовым просмотром в Android?

В моем случае у меня есть игра с двумя игроками, где они играют друг с другом. Я хочу показать тест второму игроку, ориентированному на них.


Это было решение, которое я реализовал после консультации 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 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>
4b9b3361

Ответ 1

в файле 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"/>

Ответ 2

Я не пробовал делать это сам, но я думаю, что он должен работать.

Переопределите метод viewDraw, назовите его супер передачей в холсте, а затем вызовите метод rotate на холсте, переданный ему, передав его в 180 или Math.PI, в зависимости от того, работает ли он в градусах или радиан.

Ответ 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();
    }
}