原生插屏
介绍
- 一般原生插屏使用默认的内置布局,sdk内部配置了横竖全屏半屏4套布局
- V13.7.0.1新增API支持开发者传入自定义布局
- 需要在展示广告前调用该API,以下以华为下载类广告为例
示例Demo:Interstitial Demo
请求广告
- 华为下载按钮需要额外设置setCustomParams,建议在创建广告位后立马设置
TPInterstitial tpInterstitial = new TPInterstitial(activity, "在TP平台创建的插屏广告位ID");
HashMap<String, Object> mLocalExtras = new HashMap<>();
// 使用华为下载按钮需要额外设置
mLocalExtras.put("huawei_autoinstall", 1);
tpInterstitial.setCustomParams(mLocalExtras);
tpInterstitial.setAdListener(new InterstitialAdListener() {
@Override
public void onAdLoaded(TPAdInfo tpAdInfo) {
Log.i(TAG, "onAdLoaded: ");
// 收到广告加载成功后,调用show进行广告展示
}
});
// 请求广告
tpInterstitial.loadAd();
展示广告
- 应用如果没有固定方向,建议写横竖两套layout布局,调用setCustomNativeAdRender前判断下屏幕方向,在传入具体的布局
if (tpInterstitial != null) {
tpInterstitial.setCustomNativeAdRender(new TPNativeAdRender() {
@Override
public ViewGroup createAdLayoutView() {
LayoutInflater inflater = (LayoutInflater) NewTestActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// R.layout.tp_custom_layout为开发者自定义的布局样式
return (ViewGroup) inflater.inflate(R.layout.tp_custom_layout, null);
}
@Override
public ViewGroup renderAdView(TPNativeAdView tpNativeAdView) {
ViewGroup viewGroup = createAdLayoutView();
// 大图 tp_custom_main_image 为开发者自定义布局中的大图id
ImageView imageView = viewGroup.findViewById(R.id.tp_custom_main_image);
if (imageView != null) {
if (tpNativeAdView.getMediaView() != null) {
ViewGroup.LayoutParams params = imageView.getLayoutParams();
ViewParent viewParent = imageView.getParent();
if (viewParent != null) {
((ViewGroup) viewParent).removeView(imageView);
((ViewGroup) viewParent).addView(tpNativeAdView.getMediaView(), params);
getClickViews().add(tpNativeAdView.getMediaView());
}
} else if (tpNativeAdView.getMainImage() != null) {
imageView.setImageDrawable(tpNativeAdView.getMainImage());
} else if (tpNativeAdView.getMainImageUrl() != null) {
TPImageLoader.getInstance().loadImage(imageView, tpNativeAdView.getMainImageUrl());
}
}
// icon tp_custom_icon_image 为开发者自定义布局中的icon id
ImageView iconView = viewGroup.findViewById(R.id.tp_custom_icon_image);
if (iconView != null) {
if (tpNativeAdView.getIconImage() != null) {
iconView.setImageDrawable(tpNativeAdView.getIconImage());
} else if (tpNativeAdView.getIconImageUrl() != null) {
TPImageLoader.getInstance().loadImage(iconView, tpNativeAdView.getIconImageUrl());
} else if (tpNativeAdView.getIconView() != null) {
ViewGroup.LayoutParams params = iconView.getLayoutParams();
ViewParent viewParent = iconView.getParent();
iconView = (ImageView) tpNativeAdView.getIconView();
if (viewParent != null) {
int index = ((ViewGroup) viewParent).indexOfChild(iconView);
((ViewGroup) viewParent).removeView(iconView);
((ViewGroup) viewParent).addView(iconView, index, params);
}
}
}
// 主标题 tp_custom_title 为开发者自定义布局中的title id
TextView titleView = viewGroup.findViewById(R.id.tp_custom_title);
if (titleView != null && tpNativeAdView.getTitle() != null) {
titleView.setText(tpNativeAdView.getTitle());
}
// 副标题 tp_custom_subTitle 为开发者自定义布局中的subTitle id
TextView subTitleView = viewGroup.findViewById(R.id.tp_custom_subTitle);
if (subTitleView != null && tpNativeAdView.getSubTitle() != null) {
subTitleView.setText(tpNativeAdView.getSubTitle());
}
// 点击按钮 tp_custom_cta 为开发者自定义布局中的cta id
TextView callToActionView = viewGroup.findViewById(R.id.tp_custom_cta);
if (callToActionView != null && tpNativeAdView.getCallToAction() != null) {
callToActionView.setText(tpNativeAdView.getCallToAction());
}
// 华为下载按钮
AppDownloadButton appDownloadButton = (AppDownloadButton) tpNativeAdView.getAppDownloadButton();
if (appDownloadButton != null) {
// 设置下载按钮上文字的大小,单位:px。
appDownloadButton.setTextSize(DeviceUtils.dip2px(activity, 16));
// 设置按钮大小 px
appDownloadButton.setPadding(DeviceUtils.dip2px(activity, 100), DeviceUtils.dip2px(activity, 15), DeviceUtils.dip2px(activity, 100), DeviceUtils.dip2px(activity, 15));
// 设置应用下载按钮样式 开发者可以自定义样式,示例代码仅举例
// appDownloadButton.setAppDownloadButtonStyle();
// 隐藏点击按钮
callToActionView.setVisibility(View.INVISIBLE);
// 设置下载按钮位置
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(DeviceUtils.dip2px(activity, 320), DeviceUtils.dip2px(activity, 50));
layoutParams.gravity = CENTER;
layoutParams.bottomMargin = DeviceUtils.dip2px(Activity, 15);
ViewGroup lastView = (ViewGroup) viewGroup.getChildAt(viewGroup.getChildCount() - 1);
lastView.addView(appDownloadButton, layoutParams);
} else {
setCallToActionView(callToActionView, true);
}
// adchoice
FrameLayout adChoiceView = viewGroup.findViewById(R.id.tp_ad_choices_container);
// 把主要的元素设置给三方广告平台,第二个参数是是否可以点击
setImageView(imageView, true);
setIconView(iconView, true);
setTitleView(titleView, true);
setSubTitleView(subTitleView, true);
setImageView(imageView,true);
setAdChoicesContainer(adChoiceView, true);
return viewGroup;
}
});
tpInterstitial.showAd(activity, "");
}
}