NativeFeedsDemo1Activity.java
7.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
package cn.vlion.simple;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import java.util.List;
import cn.vlion.ad.core.Config;
import cn.vlion.ad.moudle.natives.NativeAdStatusChangeListener;
import cn.vlion.ad.moudle.natives.NativeListener;
import cn.vlion.ad.moudle.natives.NativeManager;
import cn.vlion.ad.moudle.natives.model.NativeAdStateData;
import cn.vlion.ad.moudle.natives.model.NativeFeeds;
import cn.vlion.ad.moudle.natives.model.NativeFeedsData;
import cn.vlion.ad.view.natives.NativeRelativeLayout;
import cn.vlion.simple.util.ScreenUtil;
/**
* Created by gujun on 2017/11/15.
*/
public class NativeFeedsDemo1Activity extends AppCompatActivity {
private static final String adId_Feeds = "441";
private static final String TAG = "NativeFeedsDemoActivity";
private ImageView icon, image;
private TextView title, desc, rating;
private Button click;
private NativeRelativeLayout adLayout;
private int downX = -999, downY = -999, upX = -999, upY = -999;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_native_feeds_demo1);
init();
loadFeeds();
}
private void init() {
icon = findViewById(R.id.icon);
image = findViewById(R.id.image);
title = findViewById(R.id.title);
desc = findViewById(R.id.desc);
rating = findViewById(R.id.rating);
click = findViewById(R.id.click);
adLayout = findViewById(R.id.adLayout);
}
public void loadFeeds() {
NativeManager.getInstance().requestFeeds(this, adId_Feeds, new NativeListener<NativeFeedsData>() {
@Override
public void onRequestSuccess(NativeFeedsData data) {
Log.e("DemoView", "数据获取成功 " + "adId:" + data.getTagid() + "," + new Gson().toJson(data));
addData(data);
}
@Override
public void onRequestFailed(String adId, int code, String message) {
Log.e("DemoView", "数据加载失败 : code = " + code + " , message = " + message + "," + "adId:" + adId);
}
});
}
private void addData(final NativeFeedsData data) {
if (data == null || data.getNativead() == null) return;
List<NativeFeeds.IconBean> icons = data.getNativead().getIcon();
String iconUrl = icons == null || icons.size() <= 0 ? "" : icons.get(0).getUrl();
if (!TextUtils.isEmpty(iconUrl))
Glide.with(this).load(iconUrl).into(icon);
List<NativeFeeds.ImgBean> images = data.getNativead().getImg();
String imageUrl = images == null || images.size() <= 0 ? "" : images.get(0).getUrl();
if (!TextUtils.isEmpty(imageUrl))
Glide.with(this).load(imageUrl).into(image);
int imageHeight = Integer.valueOf(images.get(0).getH());
int imageWidth = Integer.valueOf(images.get(0).getW());
int imageRealWidth = ScreenUtil.getScreenWidth(this) - dp2px(this, 20);
image.getLayoutParams().width = imageRealWidth;
image.getLayoutParams().height = imageRealWidth * imageHeight / imageWidth;
title.setText(data.getNativead().getTitle());
desc.setText(data.getNativead().getDesc());
rating.setText(data.getNativead().getRating() == null ? "0分" : data.getNativead().getRating() + "分");
click.setText(data.getNativead().getButton() == null ? "ad click" : data.getNativead().getButton());
//原生广告-点击事件
//1.用户可按照API接口文档自行处理
//2.用户交由SDK处理,实现方式如下
//已弃用方法 NativeManager -> onClick(Context context, BaseData baseData, NativeAdStatusChangeListener statusChangeListener)
//该方法无法上报监测宏字段 ,会减少开发者广告收益!
//方式1
//用户捕获视图触摸事件,将按下\抬起坐标传入MonitorEvent中
//构造方式设置
//MonitorEvent monitorEvent = new MonitorEvent(downX, downY, upX, upY);
//动态设置
//monitorEvent.onTouch(MotionEvent event)//用户触摸事件
//monitorEvent.setDownX();//用户手指按下时的横坐标
//monitorEvent.setDownY();//用户手指按下时的纵坐标
//monitorEvent.setUpX();//用户手指离开设备屏幕时的横坐标
//monitorEvent.setUpY();//用户手指离开设备屏幕时的横坐标
//SDK定义的布局容器 NativeFrameLayout / NativeGridLayout / NativeLinearLayout / NativeRelativeLayout
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//SDK布局容器会自动获取MonitorEvent相关信息
//SDL布局容器.getMonitorEvent()直接获取MonitorEvent无需自己处理自定义视图及事件传递
//调用NativeManager.onClick(...)通知SDK触发广告点击事件
NativeManager.getInstance().onClick(NativeFeedsDemo1Activity.this, data, adLayout.getMonitorEvent(), statusChangeListener);
}
});
//曝光的调用
NativeManager.getInstance().onExposure(data);
//方式2
//用户使用SDK定义的布局容器 NativeFrameLayout / NativeGridLayout / NativeLinearLayout / NativeRelativeLayout
//在展示广告物料数据时调用容器init()方法
//其余事情交由SDK内部处理
//nativeLayout.init(data, statusChangeListener);
}
public static int dp2px(Context context, float dpVal) {
return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
dpVal, context.getResources().getDisplayMetrics());
}
private NativeAdStatusChangeListener statusChangeListener = new NativeAdStatusChangeListener() {
@Override
public void onAdStatusChange(NativeAdStateData stateData) {
switch (stateData.getDownloadType()) {
case Config.NATIVE_AD_CLICK_TYPE_DOWNLOAD_PENDING:
Log.e(TAG, "onAdStatusChange: adId:" + stateData.getAdId() + ",等待下载" + stateData.getDownloadProgress());
break;
case Config.NATIVE_AD_CLICK_TYPE_DOWNLOAD_RUNNING:
Log.e(TAG, "onAdStatusChange: adId:" + stateData.getAdId() + ",正在下载" + stateData.getDownloadProgress());
break;
case Config.NATIVE_AD_CLICK_TYPE_DOWNLOAD_FAILED:
Log.e(TAG, "onAdStatusChange: adId:" + stateData.getAdId() + ",下载失败" + stateData.getDownloadProgress());
break;
case Config.NATIVE_AD_CLICK_TYPE_DOWNLOAD_SUCCESS:
Log.e(TAG, "onAdStatusChange: adId:" + stateData.getAdId() + ",成功" + stateData.getDownloadProgress());
break;
case Config.NATIVE_AD_CLICK_TYPE_DOWNLOAD_PAUSED:
Log.e(TAG, "onAdStatusChange: adId:" + stateData.getAdId() + ",暂停" + stateData.getDownloadProgress());
break;
}
}
};
}