0

我在我的游戏中实现了 admob,但我的游戏使用表面视图来显示图形。

如何从 Surface 视图中“访问” AdMob 视图?

编辑2:

试图实现回调:

MainActivity.class

interface AdMobInterface {
public void HideAd();
public void ShowAd();
}

public class MainActivity extends Activity implements AdListener {

 @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    layout = new RelativeLayout(this);
    engine = new Engine(this);
    layout.addView(engine);

    if(engine.IsDemoVersion) {
        SetupAdMob();
    }

    setContentView(layout);

}


public void ShowAd() {
         ///Execute this from Engine.class
}

public void HideAd() {
          ///Execute this from Engine.class 
}

private void SetupAdMob() {
    String AdMobPublisherID = "XXXXXXXXXX";
    adView = new AdView(this, AdSize.BANNER, AdMobPublisherID);
    RelativeLayout.LayoutParams params = new    RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    layout.addView(adView, params);
    layout.bringChildToFront(layout.getChildAt(1));
    adView.loadAd(new AdRequest());
}


}

引擎类

  public class Engine extends SurfaceView implements
    SurfaceHolder.Callback, SensorEventListener, AdMobInterface {

AdMobInterface AdMob;

  public Engine(Context context, AdMobInterface admob) {
    super(context);


   AdMob = admob;

 }


 }
4

1 回答 1

0

使用FrameLayout并显示表面视图顶部的添加。像这样的东西:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/root"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
    <SurfaceView android:layout_width="fill_parent"
                 android:layout_height="fill_parent" />
    <AdView id="@+id/ad" 
            android:layout_width="wrap_content" 
            android:layout_height="fill_parent" />
</FrameLayout>

然后您需要做的就是通过 ID 找到 AdView 并将其从父布局中删除。

View adView = findViewById(R.id.ad);
FrameLayout root = (FrameLayout)findViewById(R.id.root);
root.removeView(adView);

最好从活动内部执行此操作,您的“引擎”无需了解广告或布局。

于 2012-05-22T07:08:25.240 回答