【问题标题】:Andengine - remove Ads from the splashscreenAndengine - 从启动画面中删除广告
【发布时间】:2012-07-31 13:40:53
【问题描述】:

问题是 XML 视图(广告)出现在游戏数据加载时(大约 5-6 秒)。 如何从启动画面中删除广告?

向布局添加视图。 XML 视图 - 不可见

@Override
protected void onSetContentView() {
     relativeLayout = new RelativeLayout(this);
     final FrameLayout.LayoutParams relativeLayoutLayoutParams = 
          new FrameLayout.LayoutParams( FrameLayout.LayoutParams.MATCH_PARENT,
          FrameLayout.LayoutParams.MATCH_PARENT);

     this.mRenderSurfaceView = new RenderSurfaceView(this);
     this.mRenderSurfaceView.setRenderer( mEngine, this );

     final LayoutParams surfaceViewLayoutParams = new RelativeLayout.LayoutParams(
          super.createSurfaceViewLayoutParams());
     surfaceViewLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);

     // XML-view above AndEngine view
     LayoutInflater vi = (LayoutInflater) getSystemService(
          Context.LAYOUT_INFLATER_SERVICE);
     vv = vi.inflate(R.layout.main, null);
     vv.bringToFront();
     // set XML-view visible!
     vv.setVisibility( View.GONE );

    // add views to the layout
    // AndEngine view
    relativeLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);

    // XML view
    relativeLayout.addView(vv, createAdViewLayoutParams());
    setContentView(relativeLayout, relativeLayoutLayoutParams);
}

加载启动画面资源和场景

@Override
public void onCreateResources(OnCreateResourcesCallback pOnCreateResourcesCallback) throws Exception {

    splashBackgroundTextureAtlas = new BitmapTextureAtlas(getTextureManager(), 480, 800, TextureOptions.NEAREST);
    splashBackgroundTextureRegion = BitmapTextureAtlasTextureRegionFactory.createFromAsset(splashBackgroundTextureAtlas, this, "gfx/splash.png", 0, 0);
    mEngine.getTextureManager().loadTexture(splashBackgroundTextureAtlas);

    pOnCreateResourcesCallback.onCreateResourcesFinished();
}


@Override
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws Exception {
    splashScene = new Scene();
    splashScene.setBackgroundEnabled(false);
    splashScene.attachChild(new Sprite(0, 0, splashBackgroundTextureRegion, this.getVertexBufferObjectManager()));
    pOnCreateSceneCallback.onCreateSceneFinished(splashScene);
}

加载游戏资源和场景

@Override
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
    mEngine.registerUpdateHandler(new TimerHandler(0.01f, new ITimerCallback() {
            public void onTimePassed(final TimerHandler pTimerHandler) {
                    mEngine.unregisterUpdateHandler(pTimerHandler);
                    loadResources();
                    loadScene();

                    mEngine.setScene(scene);
                }
            }));

    pOnPopulateSceneCallback.onPopulateSceneFinished();

    // set XML-view visible!                
    runOnUiThread(new Runnable() {
        public void run() { 
            vv.setVisibility(View.VISIBLE);
        }
    });
}

public void loadResources() {
    // game resources
    try {
        ITexture backgroundTexture = new BitmapTexture(
                this.getTextureManager(), new IInputStreamOpener() {
                    @Override
                    public InputStream open() throws IOException {
                        return getAssets().open("gfx/bg01.jpg");
                    }
                });
        backgroundTexture.load();
        mBackgroundTextureRegion = TextureRegionFactory.extractFromTexture(backgroundTexture);

    } catch (IOException e) {
    }
}

public void loadScene() {
    scene = new Scene();
    scene.setTouchAreaBindingOnActionDownEnabled(true);

    // add background to scene
    Sprite backgroundSprite = new Sprite(0, 0, this.mBackgroundTextureRegion, getVertexBufferObjectManager());
    scene.attachChild(backgroundSprite);

    // add sprite to scene
    pandaSprite = new Panda( false, TextureControlManager.createTiledTextureRegionFromAsset(
            getTextureManager(), this, Panda.ANIMATION_FILES), 
            getVertexBufferObjectManager());
    scene.attachChild(pandaSprite);

    scene.attachChild(pandaSprite);
    scene.registerTouchArea(pandaSprite);       
}

编辑: 布局main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/footer_layout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="bottom"
    android:orientation="vertical" >

<com.google.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/adView"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"
    ads:adUnitId="@string/admob_id"
    ads:loadAdOnCreate="true" />

</LinearLayout>

堆栈跟踪:

07-31 19:38:27.531: D/AndEngine(4960): MainActivity.onSurfaceCreated @(Thread: 'GLThread 27')
07-31 19:38:27.531: D/AndEngine(4960): MainActivity.onCreateGame @(Thread: 'GLThread 27')
07-31 19:38:27.531: D/AndEngine(4960): MainActivity.onCreateResources @(Thread: 'GLThread 27')
07-31 19:38:27.541: D/AndEngine(4960): MainActivity.onCreateScene @(Thread: 'GLThread 27')
07-31 19:38:27.551: D/AndEngine(4960): MainActivity.onPopulateScene @(Thread: 'GLThread 27')
07-31 19:38:27.551: D/AndEngine(4960): MainActivity.onGameCreated @(Thread: 'GLThread 27')
07-31 19:38:27.551: D/AndEngine(4960): MainActivity.onSurfaceChanged(Width=320,  Height=480) @(Thread: 'GLThread 27')
07-31 19:38:27.551: D/AndEngine(4960): MainActivity.onResumeGame @(Thread: 'main')
07-31 19:38:28.052: D/dalvikvm(4960): GC_FOR_MALLOC freed 127K, 54% free 3307K/7175K, external 20K/512K, paused 39ms
07-31 19:38:28.062: I/dalvikvm-heap(4960): Grow heap (frag case) to 6.978MB for 1536016-byte allocation
07-31 19:38:28.152: D/dalvikvm(4960): GC_FOR_MALLOC freed 7K, 45% free 4799K/8711K, external 20K/512K, paused 63ms
07-31 19:38:28.312: D/dalvikvm(4960): GC_CONCURRENT freed <1K, 45% free 4799K/8711K, external 20K/512K, paused 5ms+8ms
07-31 19:38:28.472: D/dalvikvm(4960): GC_EXPLICIT freed 1502K, 63% free 3299K/8711K, external 20K/512K, paused 54ms
07-31 19:38:29.173: D/dalvikvm(4960): GC_CONCURRENT freed 200K, 60% free 3526K/8711K, external 40K/512K, paused 4ms+5ms
07-31 19:38:29.403: D/dalvikvm(4960): GC_CONCURRENT freed 477K, 60% free 3519K/8711K, external 40K/512K, paused 4ms+31ms 

【问题讨论】:

  • 你能给我们看看 R.layout.main xml 文件吗?
  • 添加了 main.xml。我的问题是我不知道在什么时候设置 xml 视图可见性。

标签: android admob andengine


【解决方案1】:

最好的办法是在游戏开始之前不加载广告。

从您的 AdView XML 定义中删除 ads:loadAdOnCreate="true" 属性。这样,AdView 将不会在 onCreate() 中自动加载广告。

当你准备好时,然后在代码中加载带有 AdRequest 的 AdView:

AdView adView = (AdView) findViewById(R.id.adView);
adView.loadAd(new AdRequest());

【讨论】:

  • 但这也是一个事实,我不知道什么时候应该包括广告!我什么时候准备好? onPopulateScene 在下载完整场景之前调用
  • +1,因为我认为这是一个比我更好的答案 - 您可以在我建议您更改可见性的同一位置加载广告。
【解决方案2】:

我会在 XML 文件中将广告视图的可见性设置为“已消失”,然后在您使用代码 mEngine.setScene(scene); 更改 onTimePassed 中的场景后,将可见性更改为“可见”

这样

@Override 
public void onPopulateScene(Scene pScene, OnPopulateSceneCallback pOnPopulateSceneCallback) throws Exception {
mEngine.registerUpdateHandler(new TimerHandler(0.01f, new ITimerCallback() {
        public void onTimePassed(final TimerHandler pTimerHandler) {
                mEngine.unregisterUpdateHandler(pTimerHandler);
                loadResources();
                loadScene();

                mEngine.setScene(scene);

                // set XML-view visible here!                
                runOnUiThread(new Runnable() {
                    public void run() { 
                        vv.setVisibility(View.VISIBLE);
                    }
                });
        });

        pOnPopulateSceneCallback.onPopulateSceneFinished();

    });
}

【讨论】:

  • 我试过了。没有结果。广告视图在加载完成之前出现。我添加了堆栈跟踪。你可以看到它。
  • 我试过了。 vv.setVisibility(View.VISIBLE) 在闪屏显示的时候执行
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-06-16
  • 1970-01-01
  • 1970-01-01
  • 2012-02-14
  • 2021-06-02
  • 2021-09-07
  • 1970-01-01
相关资源
最近更新 更多