广告view自适应
This commit is contained in:
parent
29c279ac60
commit
969ff542c4
|
|
@ -0,0 +1,176 @@
|
||||||
|
package com.tfq.ad.utils;
|
||||||
|
|
||||||
|
|
||||||
|
import android.app.Activity;
|
||||||
|
import android.view.View;
|
||||||
|
import android.view.ViewTreeObserver;
|
||||||
|
import android.widget.FrameLayout;
|
||||||
|
import android.widget.LinearLayout;
|
||||||
|
|
||||||
|
import com.tfq.ad.ad.UIUtils;
|
||||||
|
import com.tfq.ad.app.ADConstants;
|
||||||
|
|
||||||
|
public class AdView {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 设置AdView的高度,如果广告视图不能填充满,那么广告view就在最底部,否则就在主视图下面
|
||||||
|
*
|
||||||
|
* @param mActivity 上下文
|
||||||
|
* @param view rootView视图
|
||||||
|
* @param idRes 资源id
|
||||||
|
* @param idResLinearLayout LinearLayout资源id
|
||||||
|
* @param flContent 广告view
|
||||||
|
*/
|
||||||
|
public void adjustAdHeight(Activity mActivity, View view, int idRes, int idResLinearLayout, FrameLayout flContent) {
|
||||||
|
if (ADConstants.AD_NATIVE) {
|
||||||
|
// 设置动态高度监听
|
||||||
|
flContent.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
|
||||||
|
@Override
|
||||||
|
public void onGlobalLayout() {
|
||||||
|
int adHeight = flContent.getHeight();
|
||||||
|
int adWidth = flContent.getWidth();
|
||||||
|
if (adHeight > 0 && adWidth > 0) {
|
||||||
|
// 移除监听(避免重复调用)
|
||||||
|
flContent.getViewTreeObserver().removeOnGlobalLayoutListener(this);
|
||||||
|
|
||||||
|
// 调整广告容器和上方占位View的高度
|
||||||
|
adjustAdContainerHeight(mActivity, idRes, idResLinearLayout, adHeight, view, flContent);
|
||||||
|
}
|
||||||
|
LogAd.e("fl_content Height=" + adHeight + " Width=" + adWidth);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 调整广告容器和上方占位View的高度
|
||||||
|
*
|
||||||
|
* @param adHeight 广告容器高度
|
||||||
|
* @param rootView 根视图
|
||||||
|
* @param idRes 资源id
|
||||||
|
*/
|
||||||
|
private void adjustAdContainerHeight(Activity mActivity, int idRes, int idResLinearLayout, int adHeight, View rootView, FrameLayout flContent) {
|
||||||
|
if (rootView == null || mActivity == null) return;
|
||||||
|
|
||||||
|
// 获取屏幕高度和可用高度
|
||||||
|
int screenHeight = mActivity.getResources().getDisplayMetrics().heightPixels;
|
||||||
|
int availableHeight = getAvailableScreenHeight(mActivity);
|
||||||
|
|
||||||
|
// 获取主要View
|
||||||
|
LinearLayout mainLinearLayout = rootView.findViewById(idResLinearLayout);
|
||||||
|
View spacerView = rootView.findViewById(idRes);
|
||||||
|
|
||||||
|
if (mainLinearLayout != null && spacerView != null) {
|
||||||
|
// 测量主内容区域的高度(不包含广告和占位View)
|
||||||
|
mainLinearLayout.measure(
|
||||||
|
View.MeasureSpec.makeMeasureSpec(mActivity.getResources().getDisplayMetrics().widthPixels, View.MeasureSpec.EXACTLY),
|
||||||
|
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
|
||||||
|
);
|
||||||
|
int mainContentHeight = mainLinearLayout.getMeasuredHeight();
|
||||||
|
|
||||||
|
// 计算广告区域的完整高度(广告高度 + 上下间距)
|
||||||
|
int adAreaHeight = adHeight + UIUtils.dp2px(mActivity, 20); // 20dp是广告上下间距
|
||||||
|
|
||||||
|
// 计算总高度:主内容高度 + 广告区域高度
|
||||||
|
int totalHeight = mainContentHeight + adAreaHeight;
|
||||||
|
|
||||||
|
LogAd.e("=== 布局高度分析 ===");
|
||||||
|
LogAd.e("屏幕高度: " + screenHeight + "px, 可用高度: " + availableHeight + "px");
|
||||||
|
LogAd.e("主内容高度: " + mainContentHeight + "px, 广告高度: " + adHeight + "px");
|
||||||
|
LogAd.e("广告区域高度: " + adAreaHeight + "px, 总高度: " + totalHeight + "px");
|
||||||
|
LogAd.e("是否完全显示: " + (totalHeight <= availableHeight ? "是" : "否"));
|
||||||
|
|
||||||
|
// 设置广告容器的布局参数
|
||||||
|
LinearLayout.LayoutParams adParams = (LinearLayout.LayoutParams) flContent.getLayoutParams();
|
||||||
|
LinearLayout.LayoutParams spacerParams = (LinearLayout.LayoutParams) spacerView.getLayoutParams();
|
||||||
|
|
||||||
|
// 计算剩余空间(可以是负数)
|
||||||
|
int remainingHeight = availableHeight - totalHeight;
|
||||||
|
|
||||||
|
LogAd.e("=== 自动布局策略 ===");
|
||||||
|
LogAd.e("主内容高度: " + mainContentHeight + "px");
|
||||||
|
LogAd.e("广告高度: " + adHeight + "px");
|
||||||
|
LogAd.e("间距高度: " + UIUtils.dp2px(mActivity, 20) + "px");
|
||||||
|
LogAd.e("总高度: " + totalHeight + "px");
|
||||||
|
LogAd.e("可用高度: " + availableHeight + "px");
|
||||||
|
LogAd.e("剩余空间: " + remainingHeight + "px");
|
||||||
|
|
||||||
|
if (remainingHeight >= 0) {
|
||||||
|
// 情况1: 内容完全能显示,自动分配剩余空间
|
||||||
|
LogAd.e("【自动布局1】内容完全显示,占位View自动占用剩余空间: " + remainingHeight + "px");
|
||||||
|
|
||||||
|
// 设置广告为固定高度(保持广告原始尺寸)
|
||||||
|
adParams.height = adHeight;
|
||||||
|
adParams.width = FrameLayout.LayoutParams.WRAP_CONTENT;
|
||||||
|
flContent.setLayoutParams(adParams);
|
||||||
|
|
||||||
|
// 占位View自动占用剩余空间(动态计算)
|
||||||
|
spacerParams.height = remainingHeight;
|
||||||
|
spacerParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
|
||||||
|
spacerParams.weight = 0; // 明确不使用weight
|
||||||
|
spacerView.setLayoutParams(spacerParams);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
// 情况2: 内容超出屏幕,需要滚动,广告完全自适应
|
||||||
|
LogAd.e("【自动布局2】内容超出屏幕 " + Math.abs(remainingHeight) + "px,广告完全自适应");
|
||||||
|
|
||||||
|
// 设置广告为完全自适应(wrap_content)
|
||||||
|
adParams.height = FrameLayout.LayoutParams.WRAP_CONTENT;
|
||||||
|
adParams.width = FrameLayout.LayoutParams.WRAP_CONTENT;
|
||||||
|
flContent.setLayoutParams(adParams);
|
||||||
|
|
||||||
|
// 设置上方占位View高度为0(不占用空间)
|
||||||
|
spacerParams.height = 0;
|
||||||
|
spacerParams.width = LinearLayout.LayoutParams.MATCH_PARENT;
|
||||||
|
spacerParams.weight = 0;
|
||||||
|
spacerView.setLayoutParams(spacerParams);
|
||||||
|
}
|
||||||
|
|
||||||
|
LogAd.e("最终效果 - 占位View: " + spacerParams.height + "px, 广告: " + adParams.height);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取可用的屏幕高度(减去状态栏、导航栏等)
|
||||||
|
*/
|
||||||
|
private int getAvailableScreenHeight(Activity mActivity) {
|
||||||
|
int screenHeight = mActivity.getResources().getDisplayMetrics().heightPixels;
|
||||||
|
int statusBarHeight = getStatusBarHeight(mActivity);
|
||||||
|
int navigationBarHeight = getNavigationBarHeight(mActivity);
|
||||||
|
|
||||||
|
return screenHeight - statusBarHeight - navigationBarHeight;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取导航栏高度
|
||||||
|
*/
|
||||||
|
private int getNavigationBarHeight(Activity mActivity) {
|
||||||
|
int result = 0;
|
||||||
|
int resourceId = mActivity.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
|
||||||
|
if (resourceId > 0) {
|
||||||
|
result = mActivity.getResources().getDimensionPixelSize(resourceId);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 检查并调整布局
|
||||||
|
*/
|
||||||
|
public void checkAndAdjustLayout(Activity mActivity, int idRes, int idResLinearLayout, View rootView, FrameLayout flContent) {
|
||||||
|
if (flContent != null && flContent.getHeight() > 0) {
|
||||||
|
adjustAdContainerHeight(mActivity, idRes, idResLinearLayout, flContent.getHeight(), rootView, flContent);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取状态栏高度
|
||||||
|
*/
|
||||||
|
private int getStatusBarHeight(Activity mActivity) {
|
||||||
|
int result = 0;
|
||||||
|
int resourceId = mActivity.getResources().getIdentifier("status_bar_height", "dimen", "android");
|
||||||
|
if (resourceId > 0) {
|
||||||
|
result = mActivity.getResources().getDimensionPixelSize(resourceId);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
}
|
||||||
Binary file not shown.
|
|
@ -1 +1 @@
|
||||||
31ae7795386ce894b560e3e83ad09240
|
71fee82897f2daa3506d41ae26a2b847
|
||||||
|
|
@ -1 +1 @@
|
||||||
e13f8c834e5755f7dc3ed51a31e0589da862e6f0
|
221f138c032b70e738e6d01632e79f83a2fd32b9
|
||||||
|
|
@ -1 +1 @@
|
||||||
ec8f2d1ad62d8fc249ce5e0d93bdb9fdcfde5e7ffe586a11ecef36f9cf3ee474
|
312074a677c0e4c0c7eb5718a148dbfb1cc2d9c13e712f2ccfa3bad77b1b19ca
|
||||||
|
|
@ -1 +1 @@
|
||||||
cab6e2ab2ada9ddf2eabaac188e79a93707ffd61e1e19c2a44132076238cdf972b121ca220273fe8c7cddd380080be4d1fd34173930b0e2e1d07de431ef8d9c8
|
d4ac3f5a9299c260c5ad153f4e55bbdcd8b13b640d9168dce5b15b1275aaae2b00dccbde7ad89fb64175aa0a5f5f8bd3c0395d1a170231b364265f6728017755
|
||||||
|
|
@ -9,6 +9,6 @@
|
||||||
<version>1.0.0</version>
|
<version>1.0.0</version>
|
||||||
<version>1.0.1</version>
|
<version>1.0.1</version>
|
||||||
</versions>
|
</versions>
|
||||||
<lastUpdated>20251120060848</lastUpdated>
|
<lastUpdated>20251201091842</lastUpdated>
|
||||||
</versioning>
|
</versioning>
|
||||||
</metadata>
|
</metadata>
|
||||||
|
|
|
||||||
|
|
@ -1 +1 @@
|
||||||
650335f76d652ed2202d121b43cd474d
|
cc563969b719fae84e7f412f29bf8cb5
|
||||||
|
|
@ -1 +1 @@
|
||||||
0acbd9e4bbde3104ec5d7586b859227d87356a4e
|
29ff624b2703074cdce94e57b778d8057866f95e
|
||||||
|
|
@ -1 +1 @@
|
||||||
e446a896a442cce95dcfe4bb28dacfe039b0d7be62eef2cc87c16278ed22c98a
|
c1c1a937455330995daca44f95bd023c8f729db23eadef5d89a99aeecf21895e
|
||||||
|
|
@ -1 +1 @@
|
||||||
02d4a0cc0cb4d4930ad74529d5a9ee89e294b2524f74fd23b909e6033977ef86392eb6daf0cbd9fb9de5524930bb53187892a899f72ade61dfdaaddf5ed197ac
|
2bbc38bcf059ee48f12f0489bbf2f6c40f890501b180a74cfcc0806b4e269ff3a18e8ba0191f8371998585cd4a738bf46a5d6e4bb7634638e9221007b1e6d024
|
||||||
Loading…
Reference in New Issue