原生广告可以无缝融入应用界面,提供更好的用户体验和更高的点击率。
private SDMNative mSDMNative;
private SDMNativeAd mNativeAd;
// 创建原生广告实例
mSDMNative = new SDMNative(context, "PlacementId");
// 设置广告加载监听器
mSDMNative.setListener(new SDMNativeLoadListener() {
@Override
public void onAdLoaded(SDMNativeAd sdmNativeAd) {
Log.i(TAG, "onNativeAdLoaded");
if (mNativeAd != null) {
mNativeAd.destroy();
}
mNativeAd = sdmNativeAd;
}
@Override
public void onAdLoadFail(AdError adError) {
Log.i(TAG, "onNativeAdLoadFail, " + adError.getErrorInfo());
}
});
// 创建广告请求,设置广告尺寸
SDMAdRequest sdmAdRequest = new SDMAdRequest.Builder()
.setAdWidth(adViewWidth)
.setAdHeight(adViewHeight)
.build();
mSDMNative.load(sdmAdRequest);
广告加载完成后,将广告内容渲染到自定义布局中展示。原生广告支持模板广告和自定义渲染两种方式。
private void showAd() {
if (mNativeAd != null) {
// 设置广告事件监听器
mNativeAd.setListener(new SDMNativeAdListener() {
@Override
public void onAdShow() {
Log.i(TAG, "native ad onAdImpressed:
");
// 广告展示成功回调
}
@Override
public void onAdClosed() {
Log.i(TAG, "native ad onAdCloseButtonClick");
// 广告关闭回调
exitNativePanel();
}
@Override
public void onAdClick() {
Log.i(TAG, "native ad onAdClicked:
");
// 广告点击回调
}
@Override
public void onDeeplinkCallback(boolean b) {
// Deeplink回调
}
@Override
public void onShowFailed(AdError adError) {
// 广告展示失败回调
}
});
// 清空广告视图容器
mNativeAdView.removeAllViews();
List<View> clickViews = null;
try {
if (mNativeAd.isTemplateAd()) {
// 模板广告:使用SDK提供的预设模板
// 模板广告会自动处理渲染和点击事件
} else {
// 自定义渲染:开发者自定义广告视图
ViewGroup nativeAdContainer = mNativeAd.getCustomAdContainer();
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
mNativeAdView.addView(nativeAdContainer, params);
// 移除自定义视图的旧父容器(如果存在)
if (mSelfRenderView.getParent() != null) {
((ViewGroup)mSelfRenderView.getParent()).removeView(mSelfRenderView);
}
// 添加自定义视图到广告容器
nativeAdContainer.addView(mSelfRenderView);
// 绑定自定义渲染视图的点击事件
clickViews = SelfRenderViewUtil.bindSelfRenderView(this, mNativeAd, mSelfRenderView);
}
} catch (Exception e) {
e.printStackTrace();
}
// 注册广告视图,绑定点击事件
mNativeAd.registerAdView(mNativeAdView, clickViews, null, null, null);
// 显示广告视图
mNativeAdView.setVisibility(View.VISIBLE);
mPanel.setVisibility(View.VISIBLE);
} else {
// 广告未加载完成提示
Log.i(TAG, "this placement no cache!");
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff"
android:orientation="vertical">
<FrameLayout
android:id="@+id/native_ad_content_image_area"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout_height="200dp" />
<FrameLayout
android:id="@+id/native_ad_image"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_below="@id/native_ad_content_image_area"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:scaleType="fitCenter" />
<FrameLayout
android:id="@+id/native_ad_shake_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/native_ad_content_image_area"
android:layout_alignBottom="@id/native_ad_content_image_area"
android:layout_centerHorizontal="true"
android:visibility="gone" />
<FrameLayout
android:id="@+id/native_ad_slide_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/native_ad_content_image_area"
android:layout_alignBottom="@id/native_ad_content_image_area"
android:layout_centerHorizontal="true"
android:visibility="gone"
tools:visibility="visible" />
<FrameLayout
android:id="@+id/native_ad_rotate_view_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/native_ad_content_image_area"
android:layout_alignBottom="@id/native_ad_content_image_area"
android:layout_centerHorizontal="true"
android:visibility="gone"
tools:visibility="visible" />
<TextView
android:id="@+id/native_ad_install_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/native_ad_image"
android:layout_alignBottom="@id/native_ad_image"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dp"
android:background="#2095F1"
android:gravity="center"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:textColor="#ffffff" />
<TextView
android:id="@+id/native_ad_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/native_ad_image"
android:layout_toLeftOf="@id/native_ad_install_btn"
android:layout_toRightOf="@id/native_ad_image"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#000000"
android:textSize="15dp"
android:textStyle="bold" />
<TextView
android:id="@+id/native_ad_desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/native_ad_image"
android:layout_toLeftOf="@id/native_ad_install_btn"
android:layout_toRightOf="@id/native_ad_image"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#777777"
android:textSize="12dp" />
<com.test.ad.demo.view.NetworkImageView
android:id="@+id/native_ad_logo"
android:layout_width="40dp"
android:layout_height="20dp"
android:layout_above="@+id/native_ad_image"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:visibility="gone" />
<FrameLayout
android:id="@+id/native_ad_logo_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/native_ad_image"
android:layout_alignParentRight="true"
android:layout_marginRight="2dp"
android:layout_marginBottom="2dp"
android:visibility="gone" />
<ImageView
android:id="@+id/native_self_adlogo"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_marginLeft="2dp"
android:src="@drawable/ad_logo" />
<TextView
android:id="@+id/native_ad_from"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/native_self_adlogo"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_toRightOf="@id/native_self_adlogo"
android:background="#888888"
android:gravity="center"
android:paddingLeft="2dp"
android:paddingRight="2dp"
android:textColor="#ffffff"
android:textSize="6dp" />
<ImageView
android:id="@+id/native_ad_close"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginTop="2dp"
android:layout_marginRight="2dp"
android:padding="5dp"
android:src="@mipmap/ic_ad_close" />
<LinearLayout
android:id="@+id/six_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/native_ad_image"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="@+id/function_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="功能"
android:textSize="12dp" />
<TextView
android:id="@+id/privacy_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="隐私"
android:textSize="12dp" />
<TextView
android:id="@+id/permission_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="权限"
android:textSize="12dp" />
<TextView
android:id="@+id/developer_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="开发者"
android:textSize="12dp" />
<TextView
android:id="@+id/version_test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="版本"
android:textSize="12dp" />
</LinearLayout>
</RelativeLayout>
@Override
protected void onDestroy() {
super.onDestroy();
destroyAd();
if (mSDMNative != null) {
mSDMNative.setListener(null);
mSDMNative.destroy();
}
}
private void destroyAd() {
if (mNativeAd != null) {
mNativeAd.destroy();
}
}
public class SelfRenderViewUtil {
private static final String TAG = SelfRenderViewUtil.class.getSimpleName();
public static List<View> bindSelfRenderView(Context context, SDMNativeAd adMaterial, View selfRenderView) {
//log
int padding = dip2px(context, 5);
selfRenderView.setPadding(padding, padding, padding, padding);
TextView titleView = (TextView) selfRenderView.findViewById(R.id.native_ad_title);
TextView descView = (TextView) selfRenderView.findViewById(R.id.native_ad_desc);
TextView ctaView = (TextView) selfRenderView.findViewById(R.id.native_ad_install_btn);
TextView adFromView = (TextView) selfRenderView.findViewById(R.id.native_ad_from);
FrameLayout iconArea = (FrameLayout) selfRenderView.findViewById(R.id.native_ad_image);
FrameLayout contentArea = (FrameLayout) selfRenderView.findViewById(R.id.native_ad_content_image_area);
final NetworkImageView logoView = (NetworkImageView) selfRenderView.findViewById(R.id.native_ad_logo);
View closeView = selfRenderView.findViewById(R.id.native_ad_close);
FrameLayout shakeViewContainer = (FrameLayout) selfRenderView.findViewById(R.id.native_ad_shake_view_container);
FrameLayout slideViewContainer = (FrameLayout) selfRenderView.findViewById(R.id.native_ad_slide_view_container);
FrameLayout rotateViewContainer = (FrameLayout) selfRenderView.findViewById(R.id.native_ad_rotate_view_container);
FrameLayout adLogoContainer = selfRenderView.findViewById(R.id.native_ad_logo_container); //v6.1.52+
// bind view
List<View> clickViewList = new ArrayList<>();//click views
String title = adMaterial.getTitle();
// title
if (!TextUtils.isEmpty(title)) {
titleView.setText(title);
clickViewList.add(titleView);
titleView.setVisibility(View.VISIBLE);
} else {
titleView.setVisibility(View.GONE);
}
String descriptionText = adMaterial.getDescription();
if (!TextUtils.isEmpty(descriptionText)) {
// desc
descView.setText(descriptionText);
clickViewList.add(descView);
descView.setVisibility(View.VISIBLE);
} else {
descView.setVisibility(View.GONE);
}
// icon
String iconImageUrl = adMaterial.getIcon();
final NetworkImageView iconView = new NetworkImageView(context);
if (!TextUtils.isEmpty(iconImageUrl)) {
iconArea.addView(iconView);
iconView.setImageUrl(iconImageUrl);
clickViewList.add(iconView);
iconArea.setVisibility(View.VISIBLE);
}
// cta button
String callToActionText = adMaterial.getCallToAction();
if (!TextUtils.isEmpty(callToActionText)) {
ctaView.setText(callToActionText);
clickViewList.add(ctaView);
ctaView.setVisibility(View.VISIBLE);
} else {
ctaView.setVisibility(View.GONE);
}
// media view
View mediaView = adMaterial.getMediaView(context, new SDMNativeAdMediaViewListener() {
@Override
public void onVideoAdStartPlay(long l) {
}
@Override
public void onVideoAdComplete() {
}
@Override
public void onVideoError(String s, String s1) {
}
@Override
public void onProgressUpdate(long l, long l1) {
}
@Override
public void onClickCloseView() {
}
});
int mainImageHeight = adMaterial.getAdImageHeight();
int mainImageWidth = adMaterial.getAdImageWidth();
FrameLayout.LayoutParams mainImageParam = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT
, FrameLayout.LayoutParams.WRAP_CONTENT);
if (mediaView == null) {
ViewTreeObserver viewTreeObserver = selfRenderView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
// 移除监听器
selfRenderView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int realMainImageWidth = selfRenderView.getWidth() - dip2px(context,
10);
int realMainHeight = 0;
if (mainImageWidth > 0 && mainImageHeight > 0 && mainImageWidth > mainImageHeight) {
realMainHeight = realMainImageWidth * mainImageHeight / mainImageWidth;
mainImageParam.width = realMainImageWidth;
mainImageParam.height = realMainHeight;
} else {
mainImageParam.width = FrameLayout.LayoutParams.MATCH_PARENT;
mainImageParam.height = realMainImageWidth * 600 / 1024;
}
}
});
} else {
int realMainImageWidth = context.getResources()
.getDisplayMetrics().widthPixels - dip2px(context, 10);
if (context.getResources().getDisplayMetrics().widthPixels > context.getResources()
.getDisplayMetrics().heightPixels) {//Horizontal screen
realMainImageWidth = context.getResources()
.getDisplayMetrics().widthPixels - dip2px(context, 10) - dip2px(context,
330) - dip2px(context, 130);
}
if (mainImageWidth > 0 && mainImageHeight > 0 && mainImageWidth > mainImageHeight) {
mainImageParam.width = FrameLayout.LayoutParams.MATCH_PARENT;
mainImageParam.height = realMainImageWidth * mainImageHeight / mainImageWidth;
} else {
mainImageParam.width = FrameLayout.LayoutParams.MATCH_PARENT;
mainImageParam.height = realMainImageWidth * 600 / 1024;
}
}
contentArea.removeAllViews();
if (mediaView != null) {
if (mediaView.getParent() != null) {
((ViewGroup) mediaView.getParent()).removeView(mediaView);
}
mainImageParam.gravity = Gravity.CENTER;
mediaView.setLayoutParams(mainImageParam);
contentArea.addView(mediaView, mainImageParam);
//clickViewList.add(mediaView);
contentArea.setVisibility(View.VISIBLE);
} else {
contentArea.removeAllViews();
contentArea.setVisibility(View.GONE);
}
//Ad Logo
String adChoiceIconUrl = adMaterial.getAdChoiceIconUrl();
if (!TextUtils.isEmpty(adChoiceIconUrl)) {
logoView.setImageUrl(adChoiceIconUrl);
logoView.setVisibility(View.VISIBLE);
}else {
logoView.setImageBitmap(null);
logoView.setVisibility(View.GONE);
}
//⚠️注意:以下三种组件只渲染其中一种即可,若广告不支持则返回的View为null。
shakeViewContainer.removeAllViews();
slideViewContainer.removeAllViews();
rotateViewContainer.removeAllViews();
View sixInfoView = selfRenderView.findViewById(R.id.six_info);
if (!TextUtils.isEmpty(adMaterial.getAppName())) {
sixInfoView.setVisibility(View.VISIBLE);
TextView functionTextView = sixInfoView.findViewById(R.id.function_test);
TextView developerTextView = sixInfoView.findViewById(R.id.developer_test);
TextView versionTextView = sixInfoView.findViewById(R.id.version_test);
TextView privacyTextView = sixInfoView.findViewById(R.id.privacy_test);
TextView permissionTextView = sixInfoView.findViewById(R.id.permission_test);
developerTextView.setText(
TextUtils.isEmpty(adMaterial.getPublisher()) ? "" : adMaterial.getPublisher());
versionTextView.setText(
TextUtils.isEmpty(adMaterial.getAppVersion()) ? "" : adMaterial.getAppVersion());
if (!TextUtils.isEmpty(adMaterial.getFunctionUrl())) {
functionTextView.setVisibility(View.VISIBLE);
setOpenUrlClickListener(functionTextView, adMaterial.getFunctionUrl());
} else {
functionTextView.setOnClickListener(null);
functionTextView.setVisibility(View.GONE);
}
if (!TextUtils.isEmpty(adMaterial.getPrivacyUrl())) {
privacyTextView.setVisibility(View.VISIBLE);
setOpenUrlClickListener(privacyTextView, adMaterial.getPrivacyUrl());
} else {
privacyTextView.setVisibility(View.GONE);
privacyTextView.setOnClickListener(null);
}
if (!TextUtils.isEmpty(adMaterial.getPermissionUrl())) {
permissionTextView.setVisibility(View.VISIBLE);
setOpenUrlClickListener(permissionTextView, adMaterial.getPermissionUrl());
} else {
permissionTextView.setVisibility(View.GONE);
permissionTextView.setOnClickListener(null);
}
} else {
sixInfoView.setVisibility(View.GONE);
}
return clickViewList;
}
private static void setOpenUrlClickListener(View view, final String url) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK);
Context context = view.getContext();
if (context != null) {
context.startActivity(intent);
}
} catch (Throwable e2) {
e2.printStackTrace();
}
}
});
}
public static int dip2px(Context context, float dipValue) {
float scale = context.getResources().getDisplayMetrics().density;
return (int) (dipValue * scale + 0.5f);
}
}