【问题标题】:Change Text Size and color更改文本大小和颜色
【发布时间】:2014-03-06 13:15:22
【问题描述】:

我想更改文本的颜色和大小。我只想为此使用一种字体。有可能吗?

这是我的代码:定义字体和文本

// for red color font
redFont = FontFactory.createFromAsset(activity.getFontManager(),
                fontTexture, activity.getAssets(), "YIKES__.TTF", 30, true,
                Color.parseColor("#EF522A"));
        redFont.prepareLetters("Score:1234567890 Sec Cash / Level"
                .toCharArray());
        redFont.load();

// for white color font
    ITexture whiteFontTexture = new BitmapTextureAtlas(
                activity.getTextureManager(), 256, 256, TextureOptions.BILINEAR);

        whiteFont = FontFactory.createFromAsset(activity.getFontManager(),
                whiteFontTexture, activity.getAssets(), "YIKES__.TTF", 30, true,
                Color.WHITE);
        whiteFont.prepareLetters("Score:1234567890 Sec Cash / Level"
                .toCharArray());
        whiteFont.load();

然后初始化文本并更改大小,如:

gameOverText = new Text(0, 0,
            ResourcesManager.getInstance().whiteFont,
            "ABCDEFHIJKLMNOPQRSTUVWXYZ",
            ResourcesManager.getInstance().vbom);
    gameOverText.setPosition(45, 72);
//  gameOverText.setSize(490, 115);
    gameOverText.setText(gameOver);

然后改变颜色:

// define the color
pColor = new org.andengine.util.color.Color(255, 102, 51); 
gameOverText.setColor(pColor);

但我没有取得成功。如此接近低于一个。

【问题讨论】:

  • 我以前遇到过这种方式的问题,但不记得解决方案了。它与 alpha 通道有关 - 查看我的代码,要获得除白色以外的颜色,我还必须使用此 Color.argb(int alpha, int red, int green, int blue) 设置 alpha 通道 - 尝试那而不是你的 parseColor 调用

标签: android text fonts andengine


【解决方案1】:

这是旧帖子,我希望您已经找到答案,但对于那些仍然不知道如何做到这一点的人,您可以尝试一下。更改字体大小相当简单。您可以使用 andengine 的 setScale 方法对其进行缩放。

gameOverText.setScale(0.1f); //Scales both X and Y
gameOverText.setScaleX(0.5f); //Scales X only
gameOverText.setScaleY(1.0f); //Scales Y only

您可以根据需要使用其他缩放方法。比例因子的范围也从 0(min) 到 1(max) 值。

最后,我花了一整天才弄清楚的颜色。 Andengine 字体有 setColor 方法来设置颜色,但它只有在你从 android.graphics.Color.rgb(255,223,0) 类中设置颜色对象时才有效。如果使用其他颜色,它只会掩盖颜色并产生比您预期的奇怪结果。

gameOverText.setColor(android.graphics.Color.rgb(255,0,0)); //For Red

希望这对将来的某人有所帮助。

【讨论】:

    【解决方案2】:

    TextView 上使用setTextColor()setTextSize() 应该没问题。

    【讨论】:

    • 谢谢。我在AndEngine!!!不在安卓中。确实支持 TextView.@Mark
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-03-17
    • 2014-07-03
    • 2019-09-09
    • 2018-01-23
    • 1970-01-01
    • 2019-06-21
    相关资源
    最近更新 更多