创建个字体类
package sample;
import com.badlogic.gdx.files.FileHandle;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import df.util.Util;
import df.util.engine.gdx.GdxFile;
/**
* Created by andrew on 14-6-23.
*/
public class FontUtil {
private static final String TAG = Util.toTAG(FontUtil.class);
public BitmapFont getFont() {
return font;
}
private BitmapFont font;
private static FontUtil instance;
private FontUtil() {
BitmapFont.BitmapFontData bitmapData = new BitmapFont.BitmapFontData(GdxFile.internal("font/minijianyihei.fnt"), false);
String[] fontArray = new String[]{
"font/minijianyihei1.png",
"font/minijianyihei2.png",
"font/minijianyihei3.png",
"font/minijianyihei4.png",
"font/minijianyihei5.png",
"font/minijianyihei6.png",
"font/minijianyihei7.png",
"font/minijianyihei8.png",
};
TextureRegion[] regions = new TextureRegion[fontArray.length];
for (int i = 0; i < fontArray.length; i++) {
FileHandle picHandle = GdxFile.internal(fontArray[i]);
regions[i] = new TextureRegion(new Texture(picHandle));
}
bitmapData.imagePaths = fontArray;
font = new BitmapFont(bitmapData, regions, true);
font.setColor(Color.BLUE);
}
public static FontUtil getInstance() {
if (null == instance) {
instance = new FontUtil();
}
return instance;
}
public BitmapFont.TextBounds draw(SpriteBatch spriteBatch, CharSequence str, float x, float y) {
/* compiled code */
return font.draw(spriteBatch, str, x, y);
}
public BitmapFont.TextBounds draw(SpriteBatch spriteBatch, CharSequence str, float x, float y, int start, int end) {
/* compiled code */
return font.draw(spriteBatch, str, x, y, start, end);
}
public BitmapFont.TextBounds drawMultiLine(SpriteBatch spriteBatch, CharSequence str, float x, float y) {
/* compiled code */
return font.drawMultiLine(spriteBatch, str, x, y);
}
public BitmapFont.TextBounds drawMultiLine(com.badlogic.gdx.graphics.g2d.SpriteBatch spriteBatch, java.lang.CharSequence str, float x, float y, float alignmentWidth, com.badlogic.gdx.graphics.g2d.BitmapFont.HAlignment alignment) {
/* compiled code */
return font.drawMultiLine(spriteBatch, str, x, y, alignmentWidth, alignment);
}
public BitmapFont.TextBounds drawWrapped(com.badlogic.gdx.graphics.g2d.SpriteBatch spriteBatch, java.lang.CharSequence str, float x, float y, float wrapWidth) {
/* compiled code */
return font.drawWrapped(spriteBatch, str, x, y, wrapWidth);
}
}
测试缩放
package sample;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import df.util.Util;
import df.util.android.LogUtil;
import df.util.engine.gdx.GdxFile;
import dy.android.cike.Define;
import dy.android.cike.main.task.MainUpgradeStoneGroup;
/**
* Created by andrew on 14-6-20.
*/
public class MyScreen implements Screen {
private static final String TAG = Util.toTAG(MyScreen.class);
Stage stage;
@Override
public void dispose() {
// TODO Auto-generated method stub
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void render(float i) {
Gdx.gl.glClearColor(0, 1, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
stage.act();
stage.draw();
}
@Override
public void resize(int arg0, int arg1) {
// TODO Auto-generated method stub
// stage.getViewport().update(width, height, true);
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void hide() {
}
@Override
public void show() {
stage = new Stage(Define.BASE_SCREEN_WIDTH, Define.BASE_SCREEN_HEIGHT);
Gdx.input.setInputProcessor(stage);
MyGroup parentGroup = new MyGroup(MainUpgradeStoneGroup.cmParent());
stage.addActor(parentGroup);
LogUtil.v(TAG, "getLineHeight ", FontUtil.getInstance().getFont().getLineHeight());
LogUtil.v(TAG, "getSpaceWidth ", FontUtil.getInstance().getFont().getSpaceWidth());
LogUtil.v(TAG, "getXHeight ", FontUtil.getInstance().getFont().getXHeight());
LogUtil.v(TAG, "getCapHeight ", FontUtil.getInstance().getFont().getCapHeight());
LogUtil.v(TAG, "getAscent ", FontUtil.getInstance().getFont().getAscent());
LogUtil.v(TAG, "getDescent ", FontUtil.getInstance().getFont().getDescent());
FontUtil.getInstance().getFont().setScale(0.5F);
LogUtil.v(TAG, "scale getLineHeight ", FontUtil.getInstance().getFont().getLineHeight());
LogUtil.v(TAG, "scale getSpaceWidth ", FontUtil.getInstance().getFont().getSpaceWidth());
LogUtil.v(TAG, "scale getXHeight ", FontUtil.getInstance().getFont().getXHeight());
LogUtil.v(TAG, "scale getCapHeight ", FontUtil.getInstance().getFont().getCapHeight());
LogUtil.v(TAG, "scale getAscent ", FontUtil.getInstance().getFont().getAscent());
LogUtil.v(TAG, "scale getDescent ", FontUtil.getInstance().getFont().getDescent());
LogUtil.v(TAG, "\n\n");
TextureRegion region = new TextureRegion(new Texture(GdxFile.internal("ui/main/main_actor.png")));
Image first = new Image(region);
stage.addActor(first);
first.setPosition(100, 40);
Image img = new Image(region);
stage.addActor(img);
img.setPosition(20, 40);
LogUtil.v(TAG, "img ", img.getX(), img.getY(), img.getWidth(), img.getHeight());
img.setScale(0.5f);
LogUtil.v(TAG, "img ", img.getX(), img.getY(), img.getWidth(), img.getHeight(), img.getScaleX(), img.getScaleY());
}
}
测试结果:
sample.MyGroup[183101] : addActor actor = dy.android.cike.main.task.MainUpgradeStoneGroup
dfutil.MyScreen[183101] : getLineHeight 57.0
dfutil.MyScreen[183101] : getSpaceWidth 13.0
dfutil.MyScreen[183101] : getXHeight 27.0
dfutil.MyScreen[183101] : getCapHeight 36.0
dfutil.MyScreen[183101] : getAscent 10.0
dfutil.MyScreen[183101] : getDescent -12.0
dfutil.MyScreen[183101] : scale getLineHeight 28.5
dfutil.MyScreen[183101] : scale getSpaceWidth 6.5
dfutil.MyScreen[183101] : scale getXHeight 13.5
dfutil.MyScreen[183101] : scale getCapHeight 18.0
dfutil.MyScreen[183101] : scale getAscent 5.0
dfutil.MyScreen[183101] : scale getDescent -6.0
dfutil.MyScreen[183101] :
dfutil.MyScreen[183101] : img 20.0 40.0 365.0 635.0
dfutil.MyScreen[183101] : img 20.0 40.0 365.0 635.0 0.5 0.5
字体缩放后,调用getXHeight( ) 字体大小就会改变。
Image代表的Actor类缩放后,仍然从左下角开始绘图。
img.getWidth( ) , img.getHeight( )没有改变,绘制的时候会乘以getScale( )