一款不错的广告页控件,App欢迎页

现在大多数App都有启动页和广告页面,不得不说,这两个页面很受产品的青睐,特别是广告页,对运营人员来说,确实是一种不错的选择。

在实际开发中,对于广告页设计,要注意一下用户体验。

根据网络情况做不同处理:

  • 在联网的情况下载,加载广告信息,更人性点,在wifi数据才加载广告,非移动数据下,建议不要去请求广告数据。

  • 在用户没有联网的时候, 直接略过广告请求。

我曾使用过一款应用,有次遇到网络不稳定时,大半天进不了首页,此处省略500个字。

  回归正题,今天要实现的效果图如下:

640?wx_fmt=gif&wxfrom=5&wx_lazy=1


展示 广告图时间可以设置,可以控制播放广告的秒数,可以控制广告的内容(图片)和广告详情页面的链接。


为了不重复造轮子,把它封装成控件。


  1public class SplashView extends FrameLayout {
 2/**
 3 * 倒计时
 4 */

 5private int countdown;
 6/**
 7 * 默认图
 8 */

 9private Integer defaultSplash;
10/**
11 * 跳转链接
12 */

13private String link;
14/**
15 * 广告图片地址
16 */

17private String imageUrl;
18/**
19 * 跳转字体颜色
20 */

21private int textColor;
22/**
23 * 字体背景颜色
24 */

25private int textBackgroundColor;
26/**
27 * 字体大小
28 */

29private int textSize;
30/**
31 * 背景字体大小
32 */

33private int textBackgroundSize;
34/**
35 * 间距
36 */

37private int textMargin;
38/**
39 * 动画消失
40 */

41private Animation dismissAnimation;
42/**
43 * 广告默认停留时间
44 */

45private TextView tvCountDown;
46/**
47 * 显示广告区域
48 */

49private ImageView ivSplash;
50/**
51 * 点击事件接口
52 */

53private OnClickSplashListener onClickSplashListener;
54private Activity context;
55public SplashView(@NonNull Activity context) {
56    super(context);
57    initView(context);
58}
59public SplashView(@NonNull Activity context, @Nullable AttributeSet attrs) {
60    super(context, attrs);
61    initView(context);
62}
63public SplashView(@NonNull Activity context, @Nullable AttributeSet attrs, int defStyleAttr) {
64    super(context, attrs, defStyleAttr);
65    initView(context);
66}
67public void setDefaultSplash(Integer defaultSplash) {
68    this.defaultSplash = defaultSplash;
69}
70public void setLink(String link) {
71    this.link = link;
72}
73public void setImageUrl(String imageUrl) {
74    this.imageUrl = imageUrl;
75}
76public void setCountdown(int countdown) {
77    this.countdown = countdown;
78}
79public void setTextColor(int textColor) {
80    this.textColor = textColor;
81}
82public void setTextBackgroundColor(int textBackgroundColor) {
83    this.textBackgroundColor = textBackgroundColor;
84}
85public void setTextSize(int textSize) {
86    this.textSize = textSize;
87}
88public void setTextBackgroundSize(int textBackgroundSize) {
89    this.textBackgroundSize = textBackgroundSize;
90}
91public void setTextMargin(int textMargin) {
92    this.textMargin = textMargin;
93}
94public void setOnClickSplashListener(OnClickSplashListener onClickSplashListener) {
95    this.onClickSplashListener = onClickSplashListener;
96}
97private void initView(@NonNull Activity context) {
98    this.context = context;
99    ivSplash = new ImageView(context);
100    ivSplash.setScaleType(ImageView.ScaleType.FIT_XY);
101    LayoutParams ivLayoutParam = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
102    this.addView(ivSplash, ivLayoutParam);
103    tvCountDown = new TextView(context);
104}
105public void show() {
106    ViewGroup viewGroup = context.getWindow().getDecorView().findViewById(android.R.id.content);
107    if (viewGroup == null || viewGroup.getChildCount() == 0) {
108        return;
109    }
110    this.addView(tvCountDown, setTextViewData(context));
111    context.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
112    setImageLoad();
113    LayoutParams thisLayoutParam = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
114    viewGroup.addView(this, thisLayoutParam);
115    this.post(runnable);
116}
117private LayoutParams setTextViewData(@NonNull Activity context) {
118    int countDownMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textMargin, context.getResources().getDisplayMetrics());
119    int countDownLayoutSize = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, textBackgroundSize, context.getResources().getDisplayMetrics());
120    tvCountDown.setTextSize(TypedValue.COMPLEX_UNIT_DIP, textSize);
121    tvCountDown.setTextColor(textColor);
122    tvCountDown.setGravity(Gravity.CENTER);
123    LayoutParams tvLayoutParam = new LayoutParams(countDownLayoutSize, countDownLayoutSize);
124    tvLayoutParam.gravity = Gravity.TOP | Gravity.RIGHT;
125    tvLayoutParam.setMargins(0, countDownMargin, countDownMargin, 0);
126    GradientDrawable countDownBg = new GradientDrawable();
127    countDownBg.setShape(GradientDrawable.OVAL);
128    countDownBg.setColor(textBackgroundColor);
129    tvCountDown.setBackgroundDrawable(countDownBg);
130    tvCountDown.setOnClickListener(new OnClickListener() {
131        @Override
132        public void onClick(View view)
{
133            dismiss();
134        }
135    });
136    ivSplash.setOnClickListener(new OnClickListener() {
137        @Override
138        public void onClick(View view)
{
139            if (!TextUtils.isEmpty(link) && onClickSplashListener != null) {
140                onClickSplashListener.clickAdv(link);
141            }
142        }
143    });
144    return tvLayoutParam;
145}
146private void setImageLoad() {
147    Glide.with(context).load(imageUrl).into(ivSplash);
148}
149private Runnable runnable = new Runnable() {
150    @SuppressLint("DefaultLocale")
151    @Override
152    public void run()
{
153        String result = "跳过\n%d s";
154        if (countdown == 0) {
155            tvCountDown.setText(String.format(result, 0));
156            dismiss();
157        } else {
158            tvCountDown.setText(String.format(result, countdown--));
159            postDelayed(this, 1000);
160        }
161    }
162};
163public void dismiss() {
164    if (onClickSplashListener != null) {
165        onClickSplashListener.jumpOver();
166    }
167    removeCallbacks(runnable);
168    ViewGroup viewGroup = (ViewGroup) this.getParent();
169    if (dismissAnimation == null) {
170        dismissAnimation = new AlphaAnimation(1.0f, 0f);
171        dismissAnimation.setDuration(1000);
172    }
173    setAnimation(dismissAnimation);
174    if (viewGroup != null) {
175        viewGroup.removeView(this);
176    }
177    SplashHelper.clear();
178}
179public void forceDismiss() {
180    removeCallbacks(runnable);
181    ViewGroup viewGroup = (ViewGroup) this.getParent();
182    if (viewGroup != null) {
183        viewGroup.removeView(this);
184    }
185}
186public void setDismissAnimation(Animation animation) {
187    this.dismissAnimation = animation;
188}
189public interface OnClickSplashListener {
190    /**
191     * 跳过广告
192     */

193    void jumpOver();
194    /**
195     * 点击广告链接
196     *
197     * @param link
198     */

199    void clickAdv(String link);
200  }
201}

抽离出一个帮助类

  1public class SplashHelper {
 2private static SplashView splashView;
 3private SplashHelper() {
 4}
 5public static Builder getBuilder(Activity context) {
 6    return new Builder(context);
 7}
 8public static void destroy() {
 9    if (splashView == null) {
10        return;
11    }
12    splashView.forceDismiss();
13    clear();
14}
15public static void clear() {
16    splashView = null;
17}
18public static class Builder {
19    private String link;
20    private String imageUrl;
21    private Integer defaultImage;
22    private int countDown = 3;
23    private int textColor = Color.parseColor("#ffffff");
24    private int textBackgroundColor = Color.parseColor("#888888");
25    private int textSize = 10;
26    private int textBackgroundSize = 35;
27    private int textMargin = 12;
28    private Animation dismissAnimation;
29    private SplashView.OnClickSplashListener onClickSplashListener;
30    private Activity context;
31    public Builder(@NonNull Activity context) {
32        this.context = context;
33    }
34    public Builder link(String link) {
35        this.link = link;
36        return this;
37    }
38    public Builder imageUrl(String imageUrl) {
39        this.imageUrl = imageUrl;
40        return this;
41    }
42    public Builder defaultImage(Integer defaultSplash) {
43        this.defaultImage = defaultSplash;
44        return this;
45    }
46    public Builder countDown(int countDown) {
47        this.countDown = countDown;
48        return this;
49    }
50    public Builder textColor(int textColor) {
51        this.textColor = textColor;
52        return this;
53    }
54    public Builder textBackgroundColor(int textBackgroundColor) {
55        this.textBackgroundColor = textBackgroundColor;
56        return this;
57    }
58    public Builder textSize(int textSize) {
59        this.textSize = textSize;
60        return this;
61    }
62    public Builder textBackgroundSize(int textBackgroundSize) {
63        this.textBackgroundSize = textBackgroundSize;
64        return this;
65    }
66    public Builder textMargin(int textMargin) {
67        this.textMargin = textMargin;
68        return this;
69    }
70    public Builder listener(SplashView.OnClickSplashListener onClickSplashListener) {
71        this.onClickSplashListener = onClickSplashListener;
72        return this;
73    }
74    public Builder dismissAnimation(Animation dismissAnimation) {
75        this.dismissAnimation = dismissAnimation;
76        return this;
77    }
78    public SplashView build() {
79        if (splashView == null) {
80            splashView = new SplashView(context);
81        }
82        splashView.setDefaultSplash(defaultImage);
83        splashView.setLink(link);
84        splashView.setImageUrl(imageUrl);
85        splashView.setCountdown(countDown);
86        splashView.setTextColor(textColor);
87        splashView.setTextBackgroundColor(textBackgroundColor);
88        splashView.setTextSize(textSize);
89        splashView.setTextBackgroundSize(textBackgroundSize);
90        splashView.setTextMargin(textMargin);
91        if (dismissAnimation != null) {
92            splashView.setDismissAnimation(dismissAnimation);
93        }
94        if (onClickSplashListener != null) {
95            splashView.setOnClickSplashListener(onClickSplashListener);
96        }
97        return splashView;
98    }
99 }
100}

广告详情页

 1public class WebActivity extends AppCompatActivity {
2@Override
3protected void onCreate(@Nullable Bundle savedInstanceState) {
4    super.onCreate(savedInstanceState);
5    setContentView(R.layout.activity_web);
6    initWebView();
7}
8private void initWebView() {
9    WebView webView = findViewById(R.id.webView);
10    webView.setWebViewClient(new WebViewClient() {
11        @Override
12        public boolean shouldOverrideUrlLoading(WebView view, String url) {
13            view.loadUrl(url);
14            return true;
15        }
16    });
17    webView.setWebChromeClient(new WebChromeClient() {
18        @Override
19        public void onProgressChanged(WebView view, int newProgress) {
20            super.onProgressChanged(view, newProgress);
21        }
22    });
23    webView.loadUrl(getIntent().getStringExtra("link"));
24 }
25}

这样调用广告页即可。

 1public void showSplash() {
2    SplashHelper.getBuilder(this)
3            .link("https://mp.weixin.qq.com/s/kUKdt5KMdOTuTlRLd5M9sA")
4            .imageUrl("https://ss.csdn.net/p?https://mmbiz.qpic.cn/mmbiz_jpg/LnN03EBxu3zDtUEgeurCkPQVVsQF18rWjkjfSNJ9nOO89y3UR4SWU48GpNceoq51fibIntgrzCtelAkZTibyG9fA/0?wx_fmt=jpeg")
5            .listener(new SplashView.OnClickSplashListener() {
6                @Override
7                public void jumpOver() {
8                    Util.intent(activity,MainActivity.class);
9                }
10                @Override
11                public void clickAdv(String link) {
12                    Bundle bundle = new Bundle();
13                    bundle.putString("link", link);
14                    Util.intent(activity, WebActivity.class, bundle);
15                }
16            })
17            .build().show();
18}

在页面销毁时释放资源。

1@Override
2protected void onDestroy() {
3    super.onDestroy();
4    SplashHelper.destroy();
5}

项目目录结构

640?wx_fmt=png

【END】

猜你喜欢

转载自blog.csdn.net/Duanmuyang/article/details/80103485