Привет, я хочу интегрировать Admob в мою игру LibGdx, но проблема в том, что если я сделаю это, мой экран игры растягивает Gameview. Я не хочу этого! я просто хочу, чтобы перекрывать объявление над Gameview без растягивания GameviewAdmob Libgdx Gameview stretching
Вот мой код:
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);
}
}
У вас есть вопрос, который содержит только соответствующую часть (-ы) вашего кода вместо всего этого? –