您好我想在我的LibGdx游戏中集成Admob,但问题是如果我这样做我的游戏画面正在拉伸Gameview我不想要这个!我只是想在不延伸游戏视图的情况下重叠Gameview上的广告
这是我的代码:
protected AdView adView;
protected View gameView;
private InterstitialAd interstitialAd;
private final int SHOW_ADS = 1;
private final int HIDE_ADS = 0;
protected Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case SHOW_ADS: {
adView.setVisibility(View.VISIBLE);
break;
}
case HIDE_ADS: {
adView.setVisibility(View.GONE);
break;
}
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = false;
cfg.useCompass = false;
cfg.useImmersiveMode = true;
cfg.hideStatusBar = true;
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(
WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
AdView admobView = createAdView();
layout.addView(admobView);
View gameView = createGameView(cfg);
layout.addView(gameView);
setContentView(layout);
startAdvertising(admobView);
interstitialAd = new InterstitialAd(this);
interstitialAd.setAdUnitId(AD_UNIT_ID_INTERSTITIAL);
interstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
// Toast.makeText(getApplicationContext(),
// "Finished Loading Interstitial", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed() {
// Toast.makeText(getApplicationContext(),
// "Closed Interstitial", Toast.LENGTH_SHORT).show();
}
});
}
private AdView createAdView() {
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
adView.setAdUnitId(AD_UNIT_ID_BANNER);
adView.setId(12345); // this is an arbitrary id, allows for relative
// positioning in createGameView()
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg) {
gameView = initializeForView(new Game(this), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.BELOW, adView.getId());
gameView.setLayoutParams(params);
return gameView;
}
private void startAdvertising(AdView adView) {
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adView.setVisibility(View.GONE);
}
@Override
public void showOrLoadInterstital() {
try {
runOnUiThread(new Runnable() {
public void run() {
if (interstitialAd.isLoaded()) {
interstitialAd.show();
// Toast.makeText(getApplicationContext(),
// "Showing Interstitial", Toast.LENGTH_SHORT).show();
} else {
AdRequest interstitialRequest = new AdRequest.Builder()
.build();
interstitialAd.loadAd(interstitialRequest);
// Toast.makeText(getApplicationContext(),
// "Loading Interstitial", Toast.LENGTH_SHORT).show();
}
}
});
} catch (Exception e) {
}
}
@Override
public void onResume() {
super.onResume();
if (adView != null)
adView.resume();
}
@Override
public void onPause() {
if (adView != null)
adView.pause();
super.onPause();
}
@Override
public void onDestroy() {
if (adView != null)
adView.destroy();
super.onDestroy();
}
@Override
public void showAds(boolean show) {
handler.sendEmptyMessage(show ? SHOW_ADS : HIDE_ADS);
}
}
答案 0 :(得分:1)
我最近遇到了同样的问题,它来自用于显示AdView和View的布局。当广告显示时,ReLativeLayout将拉伸GameView。为避免这种情况,请使用FrameLayout而不是RelativeLayout。它非常相似。
在你的onCreate()
中,你需要这个:
FrameLayout fLayout = new FrameLayout(this);
FrameLayout.LayoutParams fParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
admobView = createAdView();
View gameView = createGameView(config);
fLayout.addView(gameView);
fLayout.addView(admobView);
setContentView(fLayout);
startAdvertising(admobView);
添加admobView
后,请务必将frameLayout
添加到gameView
,以便admobView
与gameView
重叠。