// Determine the screen width (less decorations) to use for the ad width.
// If the ad hasn't been laid out, default to the full screen width.
private val adSize: AdSize
get() {
val display = windowManager.defaultDisplay
val outMetrics = DisplayMetrics()
display.getMetrics(outMetrics)
val density = outMetrics.density
var adWidthPixels = ad_view_container.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
val adWidth = (adWidthPixels / density).toInt()
return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth)
}
var adWidthPixels = ad_view_container.width.toFloat()
if (adWidthPixels == 0f) {
adWidthPixels = outMetrics.widthPixels.toFloat()
}
通常の利用するタイミングを考えるとほぼ常に「0f」 でしょうか。
他の公式サンプルを見てみます。
// Since we're loading the banner based on the adContainerView size, we need to wait until this
// view is laid out before we can get the width.
ad_view_container.viewTreeObserver.addOnGlobalLayoutListener {
if (!initialLayoutComplete) {
initialLayoutComplete = true
loadBanner()
}
}