I am developing a game and it's coming along quite nicely. I do have a bit of a problem about the AdMob ad refreshing though. Every time the ad is refreshed or it draws a different aspect of the ad, my frame rate plummets and almost makes the game unplayable. Here is what I have for the loading of the ad...
ad = new AdView(this, AdSize.BANNER, "...");
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice("...");
adRequest.addTestDevice("...");
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
ad.setLayoutParams(lp);
RelativeLayout layout = new RelativeLayout(this);
layout.addView(renderView);
layout.addView(ad);
ad.loadAd(new AdRequest());
setContentView(layout);
My solution for rendering the ad on top of the SurfaceView
was to just add it to a RelativeLayout
and add both the SurfaceView
and AdView
to it. This all works fine and dandy, but every time the ad refreshes (UI or new Ad request), it bogs down the UI thread, which in turn slows down my render thread.
Is there a way that I can make all of this work together nicely to have all work done by the AdView done separately from the main thread? I am not too sure about dynamically updating the current layout from another thread.
Thanks for the help.
I had this problem too. I found it was caused by Google ads which animate when they change, rather than static Admob banner ads which don't animate at all. There's a setting in your admob app settings for controlling whether Google ads are used... try turning it off to see if it makes a difference.
Apologies for resurrecting this question, but I have managed to come across the same issue, and found out that turning LAYER_TYPE_SOFTWARE
on the AdView
's internal WebView
made it smoother.
I have confirmed this working on my side with version 8.4.0 running on Android 5.1, although it is possible that this workaround will benefit more devices with stronger CPUs.
In my activity's onResume I have
/*
* The AdView attaches it's internal WebView only once it
* has been loaded, so we need to wait until that happens.
*/
adView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
runOnWebView(mAdView, new WebViewAction() {
@Override
public void run(WebView view) {
// the most important part is here
view.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}
});
}
});
with the runOnWebView method and WebViewAction interface defined as
/**
* Recursively searches for a WebView instance located in
* the hierarchy of view and invokes the callback method on
* action with the found instance.
*/
private void runOnWebView(View view, WebViewAction action) {
if (view instanceof WebView) {
action.run((WebView) view);
return;
}
if (view instanceof ViewGroup) {
final ViewGroup parent = (ViewGroup) view;
for (int i = 0; i < parent.getChildCount(); i++) {
runOnWebView(parent.getChildAt(i), action);
}
}
}
private interface WebViewAction {
void run(WebView view);
}