在Surfaceview中显示\隐藏AdMob

时间:2012-05-22 01:47:54

标签: android interface callback admob surfaceview

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

如何从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());
}


}

Engine.class

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

AdMobInterface AdMob;

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


   AdMob = admob;

 }


 }

1 个答案:

答案 0 :(得分: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);

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