你怎么能显示倒挂的文字与Android的一个TextView? [英] How can you display upside down text with a textview in Android?

查看:101
本文介绍了你怎么能显示倒挂的文字与Android的一个TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你怎么可以在Android的一个TextView?

显示倒挂文本

在我来说,我有一个2人游戏,他们跨越彼此发挥。我想显示测试的第二个玩家为导向,以他们。


这是后AaronMs意见,我实现的解决方案

这确实压倒一切的类,bab.foo.UpsideDownText

 包bab.foo;

进口android.content.Context;
进口android.graphics.Canvas;
进口android.util.AttributeSet;
进口android.widget.TextView;

公共类UpsideDownText扩展TextView的{

    //下面两个构造似乎需要
    公共UpsideDownText(上下文的背景下){
        超(上下文);
    }

    公共UpsideDownText(上下文的背景下,ATTRS的AttributeSet)
    {
        超(背景下,ATTRS);
    }

    @覆盖
    公共无效的OnDraw(帆布油画){
        //这节省关闭该画布适用于绘制矩阵,所以它可在以后被恢复。
        canvas.save();

        //现在我们改变矩阵
        //我们需要在我们的文本的中心旋转
        //否则,它绕原点,这是很糟糕。
        浮PY = this.getHeight()/ 2.0f;
        浮动PX = this.getWidth()/ 2.0f;
        canvas.rotate(180,PX,PY);

        //战平应用于矩阵中的文本。
        super.onDraw(画布);

        //恢复旧矩阵。
        canvas.restore();
    }
}
 

这是我的XML布局:

 < bab.foo.UpsideDownText
    机器人:文本=分数:0
    机器人:ID =@ + ID / tvScore
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文字颜色=#FFFFFF
    >
< /bab.foo.UpsideDownText>
 

解决方案

在XML文件中加入:

 机器人:旋转=180
 

在各自的元素倒置显示文本。

例如:

 <的TextView
       机器人:ID =@ + ID / textView1
       机器人:layout_width =match_parent
       机器人:layout_height =match_parent
       机器人:重力=中心
       机器人:文本=TextView的
       机器人:旋转=180/>
 

How can you display upside down text with a textview in Android?

In my case I have a 2 player game, where they play across from each other. I want to display test to the second player oriented to them.


This was the solution I implemented after AaronMs advice

The class that does the overriding, 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(); 
    }
}

And this is my 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>

解决方案

in the xml file add:

android:rotation = "180"

in the respective element to display text upside down.

for example:

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

这篇关于你怎么能显示倒挂的文字与Android的一个TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆