Resize/show/hide 从另一个角度看广告
Resize/show/hide adview from another view
我正在开发一个简单的游戏,它有一些游戏状态(暂停、播放、玩家丢失等...)。我正在尝试显示这样的 adview(admob):
paused -> Show ad as banner.
playerlost -> show as fullscreen.
playing -> hide the ad.
主要活动:
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)的大小?
您可以使用可见性标志 show/hide 广告并启动全屏广告的插页式广告:
if(paused)
adview.setVisibility(View.VISIBLE);
else if(playerlost)
interstitialAd.show();
else if(playing)
adview.setVisibility(View.INVISIBLE);
更新:
要修改来自另一个 class 的广告,您可以使用外观:
定义一个接口,例如 MyAdListener.java:
public interface MyAdListener{
public void showAd();
public void hideAd();
public void lauchFullScreenAd();
}
在您游戏的主 activity 中,实现接口:
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();
}
);
}
}
然后终于在你的游戏中 class:
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();
}
我正在开发一个简单的游戏,它有一些游戏状态(暂停、播放、玩家丢失等...)。我正在尝试显示这样的 adview(admob):
paused -> Show ad as banner.
playerlost -> show as fullscreen.
playing -> hide the ad.
主要活动:
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)的大小?
您可以使用可见性标志 show/hide 广告并启动全屏广告的插页式广告:
if(paused)
adview.setVisibility(View.VISIBLE);
else if(playerlost)
interstitialAd.show();
else if(playing)
adview.setVisibility(View.INVISIBLE);
更新:
要修改来自另一个 class 的广告,您可以使用外观:
定义一个接口,例如 MyAdListener.java:
public interface MyAdListener{
public void showAd();
public void hideAd();
public void lauchFullScreenAd();
}
在您游戏的主 activity 中,实现接口:
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();
}
);
}
}
然后终于在你的游戏中 class:
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();
}