package com.pangleadcontroller.Ads;
import android.app.Activity;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.Toast;
import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import java.util.List;
/**
* 模板渲染banner
* @author JZ
* @time 07/04/2021
*/
public class PangleBannerAd {
private static PangleBannerAd pangleBannerAd = null;
private static final String TAG = "PangleBannerAd";
private TTAdNative ttAdNative;
private TTNativeExpressAd mTTAd;
private FrameLayout mExpressContainer;
private FrameLayout.LayoutParams frameLayout;
private boolean isInitView = true;
public static PangleBannerAd getInstance(){
if (null == pangleBannerAd){
synchronized (PangleBannerAd.class){
if (null == pangleBannerAd){
pangleBannerAd = new PangleBannerAd();
}
}
}
return pangleBannerAd;
}
/**
* banner加载
* @param activity
*/
public void loadPangleBannerAd(Activity activity){
setBannerView(activity,0);//实例化view
ttAdNative = TTAdSdk.getAdManager().createAdNative(activity);
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(PangleId.bannerAd_CodeId) //广告位id
.setSupportDeepLink(true)
.setAdCount(1) //请求广告数量为1到3条
.setExpressViewAcceptedSize(300,45) //期望模板广告view的size,单位dp
.build();
ttAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
//请求失败回调
@Override
public void onError(int code, String message) {
Log.e(TAG,"banner加载失败------code = " + code + " message = " + message);
}
//请求成功回调
@Override
public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
Log.e(TAG,"banner加载成功------");
if (ads == null || ads.size() == 0) {
return;
}
mTTAd = ads.get(0);
bindAdListener(activity,mTTAd);
}
});
}
/**
* banner展示
* @param activity
*/
public void showPangleBannerAd(Activity activity){
if (mTTAd != null) {
mTTAd.render();
} else {
Toast.makeText(activity,"广告重新加载中...",Toast.LENGTH_LONG).show();
loadPangleBannerAd(activity);
}
}
/**
* banner显示位置
* @param activity
* @param orientation
*/
private void setBannerView(Activity activity,int orientation){
if (isInitView == true){
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
mExpressContainer = new FrameLayout(activity.getApplicationContext());
frameLayout = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
/**
* 设置banner展示的位置
* 0 底部居中
* 1 顶部居中
* 默认为 0
*/
if (orientation == 0){
frameLayout.gravity = Gravity.BOTTOM | Gravity.CENTER;
}else if (orientation == 1){
frameLayout.gravity = Gravity.TOP | Gravity.CENTER;
}else {
frameLayout.gravity = Gravity.BOTTOM | Gravity.CENTER;
}
activity.addContentView(mExpressContainer,frameLayout);//显示布局管理器
isInitView = false;
}
});
}
}
private void bindAdListener(Activity activity,TTNativeExpressAd ad) {
ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
@Override
public void onAdClicked(View view, int type) {
Log.e(TAG,"banner被点击------");
}
@Override
public void onAdShow(View view, int type) {
Log.e(TAG,"banner展示------");
}
@Override
public void onRenderFail(View view, String msg, int code) {
Log.e(TAG,"banner渲染失败------");
}
@Override
public void onRenderSuccess(View view, float width, float height) {
Log.e(TAG,"banner渲染成功------");
//返回view的宽高 单位 dp
mExpressContainer.removeAllViews();
mExpressContainer.addView(view);
}
});
//dislike设置
bindDislike(activity,ad);
if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD) {
return;
}
}
/**
* 设置广告的不喜欢, 注意:强烈建议设置该逻辑,如果不设置dislike处理逻辑,
* 则模板广告中的 dislike区域不响应dislike事件。
* @param activity
* @param ad
*/
private void bindDislike(Activity activity, TTNativeExpressAd ad) {
//使用默认模板中默认dislike弹出样式
ad.setDislikeCallback(activity, new TTAdDislike.DislikeInteractionCallback() {
@Override
public void onShow() {
Log.e(TAG,"banner onShow------");
}
@Override
public void onSelected(int position, String value) {
Log.e(TAG,"banner onSelected------");
//用户选择不喜欢原因后,移除广告展示
mExpressContainer.removeAllViews();
mTTAd = null;
loadPangleBannerAd(activity);
}
@Override
public void onCancel() {
Log.e(TAG,"banner onCancel------");
}
@Override
public void onRefuse() {
Log.e(TAG,"banner onRefuse------");
}
});
}
//在Activity的onDestroy方法中清空广告对象
public void onDestroy() {
if (mTTAd != null) {
mTTAd.destroy();
}
}
}
评论0