AndEngineでTrueTypeフォントで文字を表示します。
【準備】
1.「AndEngineをワークスペースにインポートする方法」の手順で、AndEngineをワークスペースにインポートしておきます。
【手順1】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの作成手順で、「AndEngineSample042-TextTtfFont」といプロジェクトを作成。
※プロジェクトの設定は以下の通り。
※他の項目は任意。
| 項目名 | 項目に設定する値 |
| アプリケーション名(Application Name) | com.example.andenginesample042 |
| プロジェクト名(Project Name) | AndEngineSample042-TextTtfFont |
| パッケージ名(Package Name) | com.example.andenginesample042 |
| Build SDK | API 8 |
| Minimum Required SDK | API 8 |
2.「AndEngineをライブラリ設定する方法」の手順でライブラリを設定。
【手順2】
1.「AndroidManifest.xml」は以下の通り。
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andenginesample042"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.andenginesample042.MainActivity"
android:label="@string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順3】
1.「src/com/example/andenginesample042/MainActivity.java」は以下の通り。
package com.example.andenginesample042;
import org.andengine.engine.camera.Camera;
import org.andengine.engine.options.EngineOptions;
import org.andengine.engine.options.ScreenOrientation;
import org.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.andengine.entity.scene.Scene;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.text.Text;
import org.andengine.entity.text.TextOptions;
import org.andengine.opengl.font.Font;
import org.andengine.opengl.font.FontFactory;
import org.andengine.opengl.texture.ITexture;
import org.andengine.opengl.texture.TextureOptions;
import org.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.andengine.ui.activity.SimpleBaseGameActivity;
import org.andengine.util.HorizontalAlign;
import android.graphics.Color;
public class MainActivity extends SimpleBaseGameActivity {
private static final float BG_R = 25 / 255.0f;
private static final float BG_G = 160 / 255.0f;
private static final float BG_B = 224 / 255.0f;
private static final int CAMERA_WIDTH = 720;
private static final int CAMERA_HEIGHT = 480;
private Font droidFont;
@Override
public EngineOptions onCreateEngineOptions() {
Camera camera = new Camera(0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
RatioResolutionPolicy policy = new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT);
return new EngineOptions(true, ScreenOrientation.LANDSCAPE_FIXED, policy, camera);
}
@Override
public void onCreateResources() {
ITexture droidFontTexture = new BitmapTextureAtlas(this.getTextureManager(), 256, 256, TextureOptions.BILINEAR);
FontFactory.setAssetBasePath("");
this.droidFont = FontFactory.createFromAsset(this.getFontManager(), droidFontTexture, this.getAssets(), "Droid.ttf", 24, true, Color.BLACK);
this.droidFont.load();
}
@Override
public Scene onCreateScene() {
Scene scene = new Scene();
scene.setBackground(new Background(BG_R, BG_G, BG_B));
drawText(scene);
return scene;
}
private void drawText(Scene scene) {
Text centerText = new Text(100, 40, this.droidFont, "Hello AndEngine! AndEngine!\n CENTER Align", new TextOptions(HorizontalAlign.CENTER), this.getVertexBufferObjectManager());
Text leftText = new Text(100, 170, this.droidFont, "Hello AndEngine! AndEngine!\nLEFT Align ", new TextOptions(HorizontalAlign.LEFT), this.getVertexBufferObjectManager());
Text rightText = new Text(100, 300, this.droidFont, "Hello AndEngine! AndEngine!\nRIGHT Align", new TextOptions(HorizontalAlign.RIGHT), this.getVertexBufferObjectManager());
scene.attachChild(centerText);
scene.attachChild(leftText);
scene.attachChild(rightText);
}
}
2.「Ctrl+Shift+F」を押し、コードをフォーマッティング。3.「Ctrl+S」を押し、ファイルを保存。
【手順4】
1.「Droid.ttf」のサウンドファイルを「assets」以下に同名で保存。
【手順5】
1.「Androidプロジェクトの作成・実行方法(バージョン別一覧)」のAndroidプロジェクトの実行手順で、実行。
【手順6】
1.以下の様に表示されれば成功です。
以上です。

0 件のコメント:
コメントを投稿
注: コメントを投稿できるのは、このブログのメンバーだけです。