Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use native Textview (View) in Andengine Game Activity

I want to know is there any way to use native TextView or any other layout of android inside BaseAndEngine Activity.

My application is using Andengine for one of its whole screen. This screen is extended from BaseAndEngine and I need to use some native view like textview inside that screen. Because Andengine does not work fine for Arabic text and I need to show some Arabic text on gaming screen.

OR if possible how to show Arabic text in changeable text in Andengine. As changeable text write Arabic from left to right in reverse order.

like image 308
Rai Avatar asked Dec 01 '11 12:12

Rai


2 Answers

Of course you can.

Check this code out - basically you override onSetContentView, then you can set whatever you want.

@Override
protected void onSetContentView() {
    final FrameLayout frameLayout = new FrameLayout(this);
    final FrameLayout.LayoutParams frameLayoutLayoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT);

    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine);
    final FrameLayout.LayoutParams surfaceViewLayoutParams = new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());
    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);

    //Create any other views you want here, and add them to the frameLayout.

    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

(This method goes to your subclass of BaseGameActivity.)

You could also do it through xml, but I think this method is more clear.

like image 152
Jong Avatar answered Nov 02 '22 08:11

Jong


You can use Andengine for Arabic and Persian fonts too. But in a different manner. to do that you need to create a Sprite and add a bitmap to it. before that you draw your text on that bitmap.

the following code is an example that draw the Persian/Arabians text and attach it to a sprite. so we can attach the sprite to our scene. this is an example to show how we can do that, so you can adjust the bitmap and text size by yourself. if your device support Persian/Arabians, this code will work properly. if the text does not appear in your scene, change its position, it is out of screen

the example code function will print the "Persian Golf" in Persian/Arabians.

private void persianGolfPrinter(){
            BitmapTextureAtlas mBitmapTextureAtlas = new BitmapTextureAtlas(ResourceManager.getInstance().gameActivity.getTextureManager(), 400, 800, TextureOptions.BILINEAR);
            ITextureRegion mDecoratedBalloonTextureRegion;
            final IBitmapTextureAtlasSource baseTextureSource = new EmptyBitmapTextureAtlasSource(400, 800);
            final IBitmapTextureAtlasSource decoratedTextureAtlasSource = new BaseBitmapTextureAtlasSourceDecorator(baseTextureSource) {
                    @Override
                    protected void onDecorateBitmap(Canvas pCanvas) throws Exception {
                            this.mPaint.setColor(Color.BLACK);
                            this.mPaint.setStyle(Style.FILL);
                            this.mPaint.setTextSize(32f);
                            this.mPaint.setTextAlign(Align.CENTER);
                            pCanvas.drawText("خلیج فارس", 150, 150, this.mPaint);

                    }

                    @Override
                    public BaseBitmapTextureAtlasSourceDecorator deepCopy() {
                            throw new DeepCopyNotSupportedException();
                    }
            };

            mDecoratedBalloonTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromSource(mBitmapTextureAtlas, decoratedTextureAtlasSource, 0, 0);
            mBitmapTextureAtlas.load();

            Sprite test = new Sprite(0,0,mDecoratedBalloonTextureRegion,ResourceManager.getInstance().engine.getVertexBufferObjectManager());
            this.attachChild(test);
    }

don't use android textview... it makes your game ugly ....

like image 30
user3157047 Avatar answered Nov 02 '22 08:11

user3157047