adjust ad

This commit is contained in:
mwang 2019-06-10 22:40:38 +08:00
parent bdd6eff8c1
commit 4ff9cc763a
11 changed files with 131 additions and 84 deletions

View File

@ -32,6 +32,7 @@ import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTBannerAd;
import com.bytedance.sdk.openadsdk.TTFeedAd;
import com.bytedance.sdk.openadsdk.TTImage;
import com.bytedance.sdk.openadsdk.TTInteractionAd;
import com.bytedance.sdk.openadsdk.TTNativeAd;
@ -358,7 +359,7 @@ public abstract class Activity_base extends AppCompatActivity {
}
//----toutiao ad ----
boolean mShowAd =false;
// boolean mShowAd =false;
public void loadBannerAd(FrameLayout bannerContainer, String codeId, int width, int height) {
//step4:创建广告请求参数AdSlot,具体参数含义参考文档
if(!Constants.SHOWAD || getTTAdNative()==null ){return;}
@ -624,7 +625,7 @@ public abstract class Activity_base extends AppCompatActivity {
i++;
}
if (nativeAd == null || !mShowAd) {
if (nativeAd == null ) {
return;
}
adCache.remove(i);
@ -893,4 +894,41 @@ public abstract class Activity_base extends AppCompatActivity {
}
});
}
/**
* 加载 toutiao feed广告
*/
public void loadListAd(BookListAdapter adapter, int adCnt, boolean addFooter) {
//feed广告请求类型参数
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(BuildConfig.AD_SLOT_TOUTIAO_FEEDAD_ID)
.setSupportDeepLink(true)
.setImageAcceptedSize(228, 150)
.setAdCount(adCnt)
.build();
//调用feed广告异步请求接口
getAdNative().loadFeedAd(adSlot, new TTAdNative.FeedAdListener() {
@Override
public void onError(int code, String message) {
Log.e(TAG, String.format("loadListAd onError: code %s,msg %s",code,message) );
}
@Override
public void onFeedAdLoad(List<TTFeedAd> ads) {
// myAdapter.changeMoreStatus(2);
if (ads == null || ads.isEmpty()) {
Log.e(TAG, "on FeedAdLoaded: ad is null!" );
return;
}
if(addFooter) {
adapter.AddFooterItem(ads);
}else{
adapter.AddHeaderItem(ads);
}
}
});
}
}

View File

@ -24,6 +24,7 @@ import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
@ -128,6 +129,8 @@ public class BookActivity extends Activity_base {
@BindView(R.id.imageViewHead)
ImageView imageViewHead;
@BindView(R.id.ll_others)
LinearLayout llAuthorMore;
@BindView(R.id.tvAuthorMore)
TextView tvAuthorMore;
@BindView(R.id.tvTonglei)
@ -158,9 +161,9 @@ public class BookActivity extends Activity_base {
rvBooklistRelated.setVisibility(View.GONE);
rvBooklistAuthor.setVisibility(View.GONE);
tvTonglei.setVisibility(View.GONE);
llAuthorMore. setVisibility(View.GONE);
tvAuthorMore.setText( "");
initiDownloadReceiver();
mShowAd =Constants.SHOWAD;
if(Constants.SHOWAD){
loadNativeBannerAd(mBannerContainer,BuildConfig.AD_SLOT_TOUTIAO_BANNER_NATIVE_ID);
mBannerContainer.setVisibility(View.VISIBLE);
@ -237,7 +240,7 @@ public class BookActivity extends Activity_base {
if(mDataAuthor!=null && mDataAuthor.size() >0){
tvAuthorMore.setText(mNovel.getAuthor()+ " 还写过");
llAuthorMore .setVisibility(View.VISIBLE);
} else{
return;
}
@ -248,6 +251,7 @@ public class BookActivity extends Activity_base {
rvBooklistAuthor.setLayoutManager(new LinearLayoutManager(this));
rvBooklistAuthor.setAdapter(mAdapterAuthor);
loadListAd(mAdapterAuthor, 1 ,false);
}
private void setNovelsRelated(){
@ -265,6 +269,9 @@ public class BookActivity extends Activity_base {
mAdapterRelated = getBookListAdapter(mDataRelated,R.layout.recycle_list_item_horizon);
rvBooklistRelated.setLayoutManager(new LinearLayoutManager(this));
rvBooklistRelated.setAdapter(mAdapterRelated);
loadListAd(mAdapterRelated, 1 ,false);
}
/**
* 设置服务器传来的详细信息

View File

@ -45,6 +45,7 @@ import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.novelbook.android.AD.toutiao.TToast;
import com.novelbook.android.Activity_base;
import com.novelbook.android.Activity_base_ViewBinding;
import com.novelbook.android.Activity_paihangbang;
import com.novelbook.android.Activity_shudan;
import com.novelbook.android.BookActivity;
@ -97,7 +98,7 @@ public abstract class BasicFragment extends Fragment {
protected Gson gson = new Gson();
// protected MyImageLoader imgloader = new MyImageLoader();
// Main2Activity activity;
FragmentActivity activity;
Activity_base activity;
@Nullable
@BindView(R.id.swipeLayout)
SwipeRefreshLayout mSwipeRefresh;
@ -118,7 +119,7 @@ public abstract class BasicFragment extends Fragment {
setFTag();
// Log.d(TAG,"fragment onCreate ");
// activity =(Main2Activity)getActivity();
activity = getActivity();
activity = (Activity_base)getActivity();
processArguments();
@ -141,7 +142,7 @@ public abstract class BasicFragment extends Fragment {
initData();
initViews();
initSwipeRefreshLayout();
showBanner(mBannerContainer,(int) getResources().getDimension(R.dimen.topBannerHeight) );
return view;
// Inflate the layout for this fragment
@ -427,6 +428,7 @@ void initTabs(){
public void onResume() {
super.onResume();
error_try=0;
showBanner(mBannerContainer,(int) getResources().getDimension(R.dimen.topBannerHeight) );
// MobclickAgent.onPageStart(TAG);
}
@Override
@ -449,16 +451,17 @@ void initTabs(){
}
//---------toutiao ad------
public void showBanner(FrameLayout bannerContainer , int adHeight ) {
public void showBanner( FrameLayout bannerContainer , int adHeight ) {
if(bannerContainer==null){
return;
}
bannerContainer.setVisibility(View.GONE);
// bannerContainer.setVisibility(View.GONE);
if (!Constants.SHOWAD) {
return;
}
((Main2Activity) activity).loadBannerAd(bannerContainer, BuildConfig.AD_SLOT_TOUTIAO_BANNER_ID,600, adHeight);
activity .loadBannerAd(bannerContainer, BuildConfig.AD_SLOT_TOUTIAO_BANNER_ID,(int)(adHeight*5.9), adHeight);
if( mBannerContainer.getVisibility() !=View.VISIBLE)
mBannerContainer.setVisibility(View.VISIBLE);
}
@ -466,36 +469,6 @@ void initTabs(){
* 加载 toutiao feed广告
*/
void loadListAd( BookListAdapter adapter, int adCnt, boolean addFooter) {
//feed广告请求类型参数
AdSlot adSlot = new AdSlot.Builder()
.setCodeId(BuildConfig.AD_SLOT_TOUTIAO_FEEDAD_ID)
.setSupportDeepLink(true)
.setImageAcceptedSize(228, 150)
.setAdCount(adCnt)
.build();
//调用feed广告异步请求接口
((Activity_base)getActivity()).getAdNative().loadFeedAd(adSlot, new TTAdNative.FeedAdListener() {
@Override
public void onError(int code, String message) {
Log.e(TAG, String.format("loadListAd onError: code %s,msg %s",code,message) );
}
@Override
public void onFeedAdLoad(List<TTFeedAd> ads) {
// myAdapter.changeMoreStatus(2);
if (ads == null || ads.isEmpty()) {
Log.e(TAG, "on FeedAdLoaded: ad is null!" );
return;
}
if(addFooter) {
adapter.AddFooterItem(ads);
}else{
adapter.AddHeaderItem(ads);
}
}
});
activity.loadListAd(adapter, adCnt ,addFooter);
}
}

View File

@ -137,7 +137,7 @@ public class Fragment_booklist extends BasicFragment {
keyWord =bundle.getString(EXTR_SEARCH);
history =bundle.getString(EXTR_HISTORY);
if(!TextUtils.isEmpty(history)){
listItem = listItem =R.layout.recycle_list_item_history;
listItem =R.layout.recycle_list_item_history;
}
}
@ -376,6 +376,9 @@ public class Fragment_booklist extends BasicFragment {
mMoreData = Novel.getNovelsHistory();
pageCount=1;
handler.sendEmptyMessage(1);
if(mMoreData!=null && mMoreData.size()>2) {
loadListAd(mAdapter, 2, mData.size() > 0);
}
}
}.start();
}

View File

@ -787,7 +787,7 @@ public class ReadActivity extends Activity_base implements AdInterface {
Intent intent = new Intent(context, ReadActivity.class);
intent.putExtra(EXTRA_BOOK, book);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
context.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
//context.overridePendingTransition(R.anim.in_from_right, R.anim.out_to_left);
context.startActivity(intent);
return true;
}
@ -884,8 +884,8 @@ public class ReadActivity extends Activity_base implements AdInterface {
rl_bottom.setVisibility(View.VISIBLE);
appbar.setVisibility(View.VISIBLE);
tvUrl.setVisibility(View.VISIBLE);
tvUrl.setText("abcd+" + pageFactory.getChapter().getChapterUrl());
/* tvUrl.setVisibility(View.VISIBLE);
tvUrl.setText("abcd+" + pageFactory.getChapter().getChapterUrl());*/
if(pageFactory.getChapter()!=null && !TextUtils.isEmpty(pageFactory.getChapter().getChapterUrl())) {
tvUrl.setVisibility(View.VISIBLE);
@ -1129,7 +1129,7 @@ public class ReadActivity extends Activity_base implements AdInterface {
return;
}
Log.d(TAG, String.format("loadBannerAd:showNativeBannerInLines width %s, adY %s ",Constants.SCREEN_WIDTH_PIX-50, adY/Constants.ONE_DP_SIZE ));
if( adY >400*Constants.ONE_DP_SIZE){
if( adY >390*Constants.ONE_DP_SIZE){
return;
}
Log.d(TAG, String.format("loadBannerAd:showNativeBannerInLines width %s, height %s ,adY %s ",Constants.SCREEN_WIDTH_PIX-50,height,adY));
@ -1169,10 +1169,10 @@ public class ReadActivity extends Activity_base implements AdInterface {
final int contentAdHight=80;
@Override
public void showStatusAd(boolean showAd ,int adHeight,int adY) {
public void showStatusAd(int adHeight,int adY) {
// hideSystemUI();
mShowAd =showAd;
// mShowAd =showAd;
// mBannerContainer.setVisibility(View.GONE);
@ -1181,10 +1181,6 @@ public class ReadActivity extends Activity_base implements AdInterface {
return;
}
if(!showAd){
return;
}
// Log.d(TAG, String.format("loadBannerAd:width %s, height %s ,adY %s,showAd %s",Constants.SCREEN_WIDTH_PIX-50,adHeight/Constants.ONE_DP_SIZE,adY/Constants.ONE_DP_SIZE, showAd));
@ -1196,8 +1192,8 @@ public class ReadActivity extends Activity_base implements AdInterface {
// Log.d(TAG, String.format("loadBannerAd:width %s, height %s ,adY %s",Constants.SCREEN_WIDTH_PIX-50,adHeight,adY));
if(adHeight>150*Constants.ONE_DP_SIZE && adY <400*Constants.ONE_DP_SIZE){
Log.d(TAG, String.format("loadBannerAd:width %s, height %s ,adY %s,showAd %s",Constants.SCREEN_WIDTH_PIX-50,adHeight/Constants.ONE_DP_SIZE,adY/Constants.ONE_DP_SIZE, showAd));
// hideAdSlot(mNative_banner_container);
Log.d(TAG, String.format("loadBannerAd:width %s, height %s ,adY %s ",Constants.SCREEN_WIDTH_PIX-50,adHeight/Constants.ONE_DP_SIZE,adY/Constants.ONE_DP_SIZE ));
hideAdSlot(mNative_banner_container_in_lines);
// hideAdSlot(mTopBannerContainer);
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mNative_status_banner_container.getLayoutParams();

View File

@ -6,7 +6,7 @@ public interface AdInterface {
* @param height
* @param adY
*/
public void showStatusAd(boolean showAd,int height,int adY);
public void showStatusAd(int height,int adY);
public void showTopBanner(int height,int adY);
public void showNativeBannerInLines(int height,int adY);
public void hideSystemUI();

View File

@ -47,4 +47,5 @@ public class Constants {
public static int version =0;
public static int serverVersion =0;
public static String updateUrl="http://xiaoshuofenxiang.com/version.xml";;
public static final String BAD_CHAR =" ";
}

View File

@ -58,7 +58,8 @@ import java.util.concurrent.ConcurrentHashMap;
public class PageFactory implements ChangeSource{
private static final String TAG = "PageFactory";
private static PageFactory pageFactory;
private float mBannerNaviHeight;
private float mBannerTopHeight;
private Context mContext;
private Config config;
//当前的书本
@ -506,28 +507,28 @@ public class PageFactory implements ChangeSource{
TRPage trPage = new TRPage();
totalPageCnt++;
float bannerH = 0;
// Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT = 4;
if (Constants.SHOWAD && pageNo > 1) {
// Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT = 1;
if (Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT > 0 && totalPageCnt % Constants.AD_CHAPT_NATIVE_BANNER_PER_PAGE_COUNT == 0) {
bannerH = mContext.getResources().getDimension(R.dimen.nativeBannerHeight) + 4 * Constants.ONE_DP_SIZE;
calculateLineCount();
int adLines = (int) (bannerH / ((m_fontSize + lineSpace)));
int adLines = (int) (mBannerNaviHeight / ((m_fontSize + lineSpace)));
int adLine = new Random().nextInt(mLineCount - adLines - 1);
adLine = adLine < mLineCount ? adLine : mLineCount - 2;
if (adLine > 0) {
trPage.setNativeBannerStartLine(adLine);
//trPage.setNativeBannerHeight(Constants.AD_CHAPT_NATIVE_BANNER_HEIGHT);
trPage.setNativeBannerHeight((int) bannerH);
trPage.setNativeBannerHeight((int) mBannerNaviHeight);
}
Log.d(TAG, String.format("getNextChapterPage: pageNo %s, topBannerHeight %s, adLines %s,adLine %s, nativeBannerHeight %s",
pageNo, trPage.getTopBannerHeight(), adLines, adLine, trPage.getNativeBannerHeight()));
} else if (Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT > 0 && totalPageCnt % Constants.AD_CHAPT_TOP_BANNER_PER_PAGE_COUNT == 0) {
bannerH = mContext.getResources().getDimension(R.dimen.topBannerHeight) + 0 * Constants.ONE_DP_SIZE;
trPage.setTopBannerHeight((int) bannerH);
trPage.setTopBannerHeight((int) mBannerTopHeight);
}
}
trPage.setPageNo(pageNo);
@ -688,7 +689,8 @@ private void hideSysUI(){
sdf = new SimpleDateFormat("HH:mm");//HH:mm为24小时制,hh:mm为12小时制
date = sdf.format(new java.util.Date());
df = new DecimalFormat("#0.0");
mBannerNaviHeight = mContext.getResources().getDimension(R.dimen.nativeBannerHeight) + 4 * Constants.ONE_DP_SIZE;
mBannerTopHeight = mContext.getResources().getDimension(R.dimen.topBannerHeight) + 0 * Constants.ONE_DP_SIZE;
marginWidth = mContext.getResources().getDimension(R.dimen.readingMarginWidth);
marginHeight = mContext.getResources().getDimension(R.dimen.readingMarginHeight);
// Log.d(TAG, "getNavigationBarSize: orignal marginHeight +" +marginHeight);
@ -1046,19 +1048,27 @@ private void hideSysUI(){
float space =m_fontSize + lineSpace;
paragraphSpace = prate * lineSpace;
if(showAd && mAd!=null){
// if(showAd && mAd!=null){
mAdHeight=trPage.getTopBannerHeight();
mAdY=(int)marginHeight;
mAdType =AdTpye.TOPBANNER;
// mAd.showTopBanner(trPage.getTopBannerHeight(),(int)(marginHeight));
// mAd.showNativeBannerInLines(0,0);
}
// }
/* if(trPage.getPageNo()>1 && m_lines.size() <= mLineCount/2+1){
if(trPage.getTopBannerHeight() + trPage.getNativeBannerHeight() ==0){
mAdHeight=(int)mBannerNaviHeight;
mAdY=(int)marginHeight;
mAdType =AdTpye.NATIVEINLINES;
}
}*/
if (m_lines.size() > 0) {
float y = marginHeight +(trPage.getTopBannerHeight() >0 ? trPage.getTopBannerHeight() :0);
float y = marginHeight +mAdHeight;
String lastLine ="";
int lineNo =0;
for (String strLine : m_lines) {
@ -1068,15 +1078,16 @@ private void hideSysUI(){
// Log.d(TAG, strLine);
// Log.d(TAG,"最后字符 。。。" +strLine.charAt(strLine.length()-1) + "");
lastLine =strLine;
strLine = strLine.replace("<EFBFBD>","");
if(lineNo==trPage.getNativeBannerStartLine()){
if(showAd && mAd!=null){
mAdHeight=trPage.getNativeBannerHeight();
mAdY=(int)y;
mAdY=(int)y +(int)( +lineSpace*0.5);
mAdType =AdTpye.NATIVEINLINES;
// mAd.showNativeBannerInLines(trPage.getNativeBannerHeight(),(int)y);
}
y+=trPage.getNativeBannerHeight() +m_fontSize*1.1;
y+=trPage.getNativeBannerHeight() +m_fontSize +lineSpace ;
Log.d(TAG, String.format("onDraw showNativeBannerInLines : lineno %s,strline %s ",lineNo,strLine));
}else {
@ -1101,11 +1112,13 @@ private void hideSysUI(){
}
// Log.d(TAG,String.format("getNavigationBarSize mHeight is %s ,last line height %s, power %s, %s",
// Log.d(TAG,String.format("getNavigationBarSize mHeight is %s ,last line height %s, power %s, %s", //
// mHeight,y,mHeight - CommonUtil.convertDpToPixel(mContext,10) + mBorderWidth - statusMarginBottom, lastLine));
// lastLine =lastLine.trim().replace(Constants.BAD_CHAR,"");
Log.d(TAG, String.format("onDraw: last line lenth %s,%s, ",lastLine.length() ,lastLine));
float adHeight = mHeight -y - space -marginHeight-statusMarginBottom;
float adY =y +space;
float adY =y ;//+space;
adY= lastLine.length()==0 ?adY-m_fontSize - lineSpace :adY;
if(mAdHeight ==0 && showAd ) {
mAdY=(int)adY;
mAdHeight =(int) adHeight;
@ -1201,6 +1214,9 @@ private void hideSysUI(){
}
public void showAd(){
if(mStatus == Status.LASTPAGE){
return;
}
Log.d(TAG, String.format("loadBannerAd showAd: mAdType %s",mAdType));
if(mAdType ==AdTpye.TOPBANNER){
mAd.showTopBanner(mAdHeight,mAdY);
@ -1216,8 +1232,8 @@ private void hideSysUI(){
boolean showAd = showingStatusAd || getCurrentPage()==null || getCurrentPage().getPageNo() > 1;
// boolean showAd =true;
if (mAd != null) {
mAd.showStatusAd(showAd,adHeight, adY);
if (showAd && mAd != null) {
mAd.showStatusAd(adHeight, adY);
}
@ -1610,6 +1626,9 @@ private void hideSysUI(){
while (mBookUtil.next(true,chaptId) != -1){
char word = (char) mBookUtil.next(false,chaptId);
// Log.d(TAG, String.format(" loadchapt getNextLines() chaptId %s, word '%s'", chaptId,word ));
if((word+"").equals(Constants.BAD_CHAR)){
continue;
}
//判断是否换行
if ((word + "" ).equals("\n") ){// if ((word + "" ).equals("\r") && (((char) mBookUtil.next(true)) + "").equals("\n")){
// mBookUtil.next(false);
@ -1635,9 +1654,15 @@ private void hideSysUI(){
}
}else {
float widthChar = mPaint.measureText(word + "");
if(widthChar==0){
continue;
}
width += widthChar;
// Log.d(TAG, String.format(" loadchapt getNextLines(),widthChar %s ,width %s,mVisibleWidth %s",widthChar, width ,mVisibleWidth ));
if (width > mVisibleWidth) {
width = widthChar;
lines.add(line);

View File

@ -19,13 +19,14 @@
app:search_baground="@drawable/search_baground_shap"
/>
<FrameLayout
android:layout_weight="0"
android:gravity="bottom"
style="@style/frmBannerContainer"
android:layout_height="400dp"
android:id="@+id/banner_container"/>
<LinearLayout style="@style/llGraySplit" />
<FrameLayout
android:gravity="bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/banner_container"/>
</LinearLayout>

View File

@ -208,10 +208,12 @@
</LinearLayout>
</LinearLayout>
<FrameLayout
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
style="@style/frmBannerContainer"
android:layout_height="310dp"
android:layout_height="@dimen/nativeBannerHeight"
android:id="@+id/banner_container"/>
<LinearLayout style="@style/llGraySplit" />
<!--
<LinearLayout
@ -267,10 +269,11 @@
-->
<LinearLayout
android:id="@+id/ll_others"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout style="@style/llGraySplit" />
<TextView
android:id="@+id/tvAuthorMore"
style="@style/NovelBlockTitle"

View File

@ -456,7 +456,7 @@
<item name="android:layout_centerInParent">true</item>
<!--<item name="android:background">@color/mintcream</item>-->
<item name="android:background">@color/transparent</item>
<item name="android:visibility">gone</item>
<item name="android:visibility">visible</item>
</style>