2011-12-01 22 views
5

我想知道是否有任何方法使用原生TextView或BaseAndEngine Activity內的任何其他Android版式。如何在Andengine遊戲活動中使用原生文字視圖(視圖)

我的應用程序使用Andengine作爲其整個屏幕之一。此屏幕從BaseAndEngine擴展而來,我需要在該屏幕內使用一些本地視圖,如textview。由於Andengine對阿拉伯文本無法正常工作,因此我需要在遊戲屏幕上顯示一些阿拉伯文字。

或者如果可能的話如何在Andengine的可變文本中顯示阿拉伯文字。由於可變文本以相反順序從左至右寫阿拉伯文。

+0

你好,沒有ü解決表示在andengine多變文本阿拉伯語的問題呢?我現在面臨同樣的問題。你能幫我嗎 –

回答

4

當然可以。

檢查此代碼 - 基本上你覆蓋onSetContentView,那麼你可以設置任何你想要的。

@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); 
} 

(此方法去你的BaseGameActivity子類。)

你也可以做到這一點通過XML,但我認爲這種方法更清晰。

+0

謝謝你的回答。我認爲這看起來可能和直接。我會嘗試這個,並會讓你知道。 – Rai

+0

這就是我用來爲遊戲添加「AdView」的代碼。 – Jong

+0

這個答案不顯示如何使用TextView IN Andengine,以至於如何顯示一個OVER Andengine。 @Rai可以澄清他們不需要將TextView與Andengine集成。 –

-2

你不能直接在AndEngine中使用它們,因爲Andengine中的對象是OpenGL對象。但是您可以使用android操作系統繪製任何標準視圖的位圖,然後從該視圖創建紋理和精靈。對於像阿拉伯文字這樣的情況,這不是最佳選擇,但是它會起作用。在創建視圖的位圖時要小心內存泄漏。

3

您也可以使用Andengine進行阿拉伯語和波斯語字體的設置。但以不同的方式。要做到這一點,你需要創建一個Sprite併爲其添加一個位圖。在此之前,您在該位圖上繪製文本。

以下代碼是一個繪製波斯/阿拉伯文字並將其附加到精靈的示例。所以我們可以將精靈附加到我們的場景中。 這是一個例子來說明我們如何做到這一點,所以你可以自己調整位圖和文字大小。 如果您的設備支持波斯/阿拉伯人,此代碼將正常工作。如果文字沒有出現在你的場景中,改變它的位置,它就會出現在屏幕外

示例代碼函數將打印波斯/阿拉伯人的「波斯高爾夫球」。

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); 
    } 

不要使用Android的TextView ...它使你的遊戲醜陋....