Kumpulan Informasi Loker dan Kuliah di Indonesia

Thursday, April 2, 2015

Cara Membuat Text didalam Android

2:18:00 PM Posted by Abdul Rohman No comments

Text adalah komponen yang diturunkan dari entity sehingga bisa dimasukan dalam scene. Dan tulisan akan menampilkan tulisan yang bentuknya sesuai font yang ditentukan.

Terdapat 2 elemen dalam teks yaitu:
  1. Changeable text
  2. Ticker text
Contohnya:
1. Buat Project

     Project Name  : TestText
     Buitl Target  : Android 2.2
     Application nama  : text
     Package name  : com.latihan.text
     Activity  : text
     Min SDK  : 8
2.  Tuliskan Script dibawah ini:

import org.anddev.andengine.engine.Engine;
import org.anddev.andengine.engine.camera.Camera;
import org.anddev.andengine.engine.options.EngineOptions;
import org.anddev.andengine.engine.options.EngineOptions.ScreenOrientation;
import org.anddev.andengine.engine.options.resolutionpolicy.RatioResolutionPolicy;
import org.anddev.andengine.entity.scene.Scene;
import org.anddev.andengine.entity.scene.background.ColorBackground;
import org.anddev.andengine.entity.text.Text;
import org.anddev.andengine.opengl.font.Font;
import org.anddev.andengine.opengl.texture.TextureOptions;
import org.anddev.andengine.opengl.texture.atlas.bitmap.BitmapTextureAtlas;
import org.anddev.andengine.ui.activity.BaseGameActivity;
import org.anddev.andengine.util.HorizontalAlign;
import android.graphics.Color;
import android.graphics.Typeface;
 
public class text extends BaseGameActivity {
private int CAMERA_WIDTH = 480;
private int CAMERA_HEIGHT = 320;
private Scene scene;
private Camera camera;
private BitmapTextureAtlas fontTexture;
    private Font font;
public Engine onLoadEngine() {
final Camera camera = new Camera(
0, 0, CAMERA_WIDTH, CAMERA_HEIGHT);
return new Engine(new EngineOptions(
true, ScreenOrientation.LANDSCAPE,
new RatioResolutionPolicy(CAMERA_WIDTH, CAMERA_HEIGHT),
camera));
}
public void onLoadResources() {
fontTexture = new BitmapTextureAtlas(256, 256,
TextureOptions.BILINEAR_PREMULTIPLYALPHA);
font = new Font(
fontTexture,
Typeface.create(Typeface.DEFAULT, Typeface.BOLD),
22, true, Color.WHITE);
        mEngine.getTextureManager().loadTexture(this.fontTexture);
        mEngine.getFontManager().loadFont(font);
}

public Scene onLoadScene() {
scene = new Scene();
scene.setBackground(new ColorBackground(0.2f, 0.2f, 0.2f));
Text textCenter = new Text(
0, 30, this.font,
"Educa Studio\nYANG INI ALIGN CENTER!",
HorizontalAlign.CENTER);
        Text textLeft = new Text(
        0, 140, this.font,
        "Educa Studio!\nYANG INI ALIGN LEFT!",
        HorizontalAlign.LEFT);
        Text textRight = new Text(
        0, 240, this.font,
        "Educa Studio!\nYANG INI ALIGN RIGHT!",
        HorizontalAlign.RIGHT);
        scene.attachChild(textCenter);
        scene.attachChild(textLeft);
        scene.attachChild(textRight);
        return scene;
}
public void onLoadComplete() {
}
}



0 komentar:

Post a Comment