从另一个视图调整/显示/隐藏广告视图

时间:2015-03-03 22:10:05

标签: android layout resize admob

我开发了一个简单的游戏,它有一些游戏状态(暂停,播放,播放器丢失等等)。我试着像这样展示一个广告(admob):

paused -> Show ad as banner.
playerlost -> show as fullscreen.
playing -> hide the ad.

MainActivity:

public class MainActivity extends Activity{
    private tGame game;
    //(...)
    layout.addView(game);
    layout.addView(admob, adParams);
    setContentView(layout);
    //(...)
}

另一种观点(tGame):

public class tGame extends View implements Runnable{
    //(...)
    int gameState = 0; // <- PLAYING, PAUSED, PLAYERLOST, etc.
    //(...)
}

如何从其他视图(游戏)调整广告(admob)的大小?

1 个答案:

答案 0 :(得分:0)

您可以使用公开标记来显示/隐藏广告并为全屏广告启动插页式广告:

if(paused)
  adview.setVisibility(View.VISIBLE);
else if(playerlost)
 interstitialAd.show();
else if(playing)
  adview.setVisibility(View.INVISIBLE);

<强>更新

要修改其他类的广告,您可以使用外观:

定义一个接口,例如MyAdListener.java:

public interface MyAdListener{
public void showAd();
public void hideAd();
public void lauchFullScreenAd();
}

在游戏的主要活动中,实现界面:

public class MainActivity extends Activity implements MyAdListener{
    private tGame game;
    //(...)
    layout.addView(game);
    layout.addView(admob, adParams);
    setContentView(layout);
//(...)

@Override
public void showAd(){
runOnUiThread(new Runnable() {
   @Override
   public void run(){
      adview.setVisibility(View.VISIBLE);
   }
  );
 }

@Override
public void hideAd(){
runOnUiThread(new Runnable() {
   @Override
   public void run(){
      adview.setVisibility(View.INVISIBLE);
   }
  );

}



 @Override
    public void launchFullScreenAd(){
    runOnUiThread(new Runnable() {
       @Override
       public void run(){
          if (interstitial != null) interstitial.show();    
       }
      );
     }


}

然后最后在你的游戏课程中:

public class tGame extends View implements Runnable{
    MyAdListener myAdListener;
    public tGame(Context context){
    myAdListener = (MyAdListener)context;
    }
    //(...)
    int gameState = 0; // <- PLAYING, PAUSED, PLAYERLOST, etc.
    //(...)

   if(paused)
      myAdListener.showAd();
    else if(playerlost)
      myAdListener.launchFullScreenAd();
    else if(playing)
      myAdListener.hideAd();

}