如何获取自适应横幅 (Admob) API 30 的尺寸

问题描述 投票:0回答:5

由于

getDefaultDisplay();
中已弃用的方法
getMetrics()
API 30
,下面的代码无法正常工作。

private AdSize getAdSize() {
    Display display = getWindowManager().getDefaultDisplay();//deprecated
    DisplayMetrics outMetrics = new DisplayMetrics();
    display.getMetrics(outMetrics);//deprecated

    float density = outMetrics.density;
    float widthPixels = adContainerView.getWidth();

    if (widthPixels == 0) {
        widthPixels = outMetrics.widthPixels;
    }

    int adWidth = (int) (widthPixels / density);

    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
}

Google
需要使用WindowMetrics#getBounds()来获取应用程序窗口区域的尺寸,并使用Configuration#密度Dpi来获取当前密度。

我找到了这些方法

API 30
,但我不知道如何使用它们。

final WindowMetrics metrics = windowManager.getCurrentMetrics();
 // Gets all excluding insets
 final WindowInsets windowInsets = metrics.getWindowInsets();
 Insets insets = windowInsets.getInsets(WindowInsets.Type.navigationBars());
 final DisplayCutout cutout = windowInsets.getCutout();
 if (cutout != null) {
     final Insets cutoutSafeInsets = Insets.of(cutout.getSafeInsetsLeft(), ...);
     insets = insets.max(insets, cutoutSafeInsets);
 }

 int insetsWidth = insets.right + insets.left;
 int insetsHeight = insets.top + insets.bottom;

 // Legacy size that Display#getSize reports
 final Size legacySize = new Size(metrics.getWidth() - insetsWidth,
         metrics.getHeight() - insetsHeight);

有人遇到过这种情况吗?

android admob size screen
5个回答
2
投票

我用的是这个方法:

private AdSize getAdSize() {
    DisplayMetrics outMetrics = getResources().getDisplayMetrics();
    float widthPixels = outMetrics.widthPixels;
    float density = outMetrics.density;
    int adWidth = (int) (widthPixels / density);
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(requireContext(), adWidth);
}

对于 LinearLayout,查看自适应横幅:

    LinearLayout banner_view = findViewById(R.id.banner_view);
    AdView adView = new AdView(requireActivity().getApplicationContext());
    adView.setAdUnitId(getString(R.string.my_banner_id));
    banner_view.addView(adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    AdSize adSize = getAdSize(); // Code above
    adView.setAdSize(adSize);
    adView.loadAd(adRequest);

0
投票

谷歌开发https://developer.android.com/training/multiscreen/screendensities?hl=en

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        return context.getResources().getDisplayMetrics().densityDpi;

    } else {
        DisplayMetrics displayMetrics = new DisplayMetrics();

        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        int deviceWidth = displayMetrics.widthPixels;
        int deviceHeight = displayMetrics.heightPixels;

        activity.getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);

        DisplayMetrics metrics = new DisplayMetrics();
        ((WindowManager) context.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getMetrics(metrics);
        return metrics.densityDpi;
    }

0
投票

我找到了解决方案

  private static AdSize getAdSize(AppCompatActivity activity) {
    int windowWidth = activity.getResources().getConfiguration().screenWidthDp;
    return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(activity, windowWidth);
}

0
投票

我有同样的问题,来到这里发现仍然没有明确解决。 以下是我的解决方案。它是 C# Xamarin。所以 get* -> * 存在差异。 但这可能是暗示。我希望有人修改这个正确答案。

        Context     context = Android.App.Application.Context;
        IWindowManager windowManager = context.GetSystemService(Context.WindowService).JavaCast<IWindowManager>();
        float   adwidth, scale;
        if ((int)Android.OS.Build.VERSION.SdkInt >= 30) {
            WindowMetrics metrics = windowManager.CurrentWindowMetrics;
            Rect    bounds = metrics.Bounds;
            adwidth = (bounds.Width() < bounds.Height() ? bounds.Width() : bounds.Height());
            scale = context.Resources.Configuration.DensityDpi / 160f;
        }
        else {
            Display     display = windowManager.DefaultDisplay;
            DisplayMetrics  displayMetrics = new DisplayMetrics();
            display.GetMetrics(displayMetrics);
            adwidth = (displayMetrics.WidthPixels < displayMetrics.HeightPixels ? displayMetrics.WidthPixels : displayMetrics.HeightPixels);
            scale = displayMetrics.Density;
        }
        mAdView.AdSize = AdSize.GetCurrentOrientationAnchoredAdaptiveBannerAdSize(this, (int)(adwidth / scale));

0
投票
private AdSize getAdSize() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
        WindowMetrics windowMetrics = getWindowManager().getCurrentWindowMetrics();
        Rect bounds = windowMetrics.getBounds();

        float adWidthPixels = adContainerView.getWidth();

        // If the ad hasn't been laid out, default to the full screen width.
        if (adWidthPixels == 0f) {
            adWidthPixels = bounds.width();
        }

        float density = getResources().getDisplayMetrics().density;
        int adWidth = (int) (adWidthPixels / density);

        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
    } else {
        DisplayMetrics outMetrics = getResources().getDisplayMetrics();
        float widthPixels = outMetrics.widthPixels;
        float density = outMetrics.density;
        int adWidth = (int) (widthPixels / density);
        return AdSize.getCurrentOrientationAnchoredAdaptiveBannerAdSize(this, adWidth);
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.