服务器之家

服务器之家 > 正文

Android实现加载广告图片和倒计时的开屏布局

时间:2021-03-04 13:21     来源/作者:Android开发网

这是一个android开屏布局的实例,可以用于加载广告图片和倒计时的布局。程序中设置的LayoutParams,划分额外空间比例为6分之5,具体权重比例可根据用户自己需求来自定义,异步加载广告图片,相关的Android代码。

具体实现代码如下:

?
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package cn.waps.extend;
import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RoundRectShape;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.qcn.wzlz.AppConnect;
import com.qcn.wzlz.SDKUtils;
public class LoadingPopAd {
 private final static Handler mHandler = new Handler();
 private static LoadingPopAd loadingAppPopAd;
 public static LoadingPopAd getInstance(){
 if(loadingAppPopAd == null){
  loadingAppPopAd = new LoadingPopAd();
 }
 if (Looper.myLooper() == null) {
  Looper.prepare();
 }
 return loadingAppPopAd;
 }
 /**
 * 获取开屏布局
 * @param context
 * @param time
 * @return
 */
 public View getContentView(Context context, int time){
 return getLoadingLayout(context, time);
 }
 private LinearLayout getLoadingLayout(final Context context, final int time){
 // 整体布局
 LinearLayout layout = new LinearLayout(context);
 layout.setOrientation(LinearLayout.VERTICAL);
 layout.setGravity(Gravity.CENTER);
 int bg_id = context.getResources().getIdentifier("loading_bg", "drawable", context.getPackageName());
 if(bg_id != 0){
  layout.setBackgroundResource(bg_id);
 }
 // 加载广告图片和倒计时的布局,用与
 LinearLayout l_layout = new LinearLayout(context);
 l_layout.setGravity(Gravity.CENTER);
 // 设置LayoutParams,划分额外空间比例为6分之5(具体权重比例可根据自己需求自定义)
 l_layout.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 1f));
 // 加载图片的布局
 RelativeLayout pop_layout = new RelativeLayout(context);
 TextView timeView = new TextView(context);
 timeView.setText("剩余" + time + "秒");
 timeView.setTextSize(10);
 timeView.setTextColor(Color.BLACK);
 timeView.setPadding(8, 3, 6, 2);
 int num = 12;
 // 对手机进行屏幕判断
 int displaySize = SDKUtils.getDisplaySize(context);
 if(displaySize == 320){
  num = 8;
 }else if(displaySize == 240){
  num = 6;
 }else if(displaySize == 720){
  num = 16;
 }else if(displaySize == 1080){
  num = 20;
 }
 float[] outerRadii = new float[] { 0, 0, num, num, 0, 0, num, num};
 ShapeDrawable timeView_shapeDrawable = new ShapeDrawable();
 timeView_shapeDrawable.setShape(new RoundRectShape(outerRadii, null, null));
 timeView_shapeDrawable.getPaint().setColor(Color.argb(255, 255, 255, 255));
 timeView.setBackgroundDrawable(timeView_shapeDrawable);
 //异步执行倒计时
 //异步加载广告图片
 new ShowPopAdTask(context, pop_layout, timeView).execute();
 new TimeCountDownTask(timeView, time).execute();
 TextView textView = new TextView(context);
 textView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT, 5f));
 textView.setText("正在启动,请稍后...");
 textView.setGravity(Gravity.CENTER);
 textView.setTextColor(Color.WHITE);
 l_layout.addView(pop_layout);
 layout.addView(l_layout);
 layout.addView(textView);
 return layout;
 }
 private class TimeCountDownTask extends AsyncTask<Void, Void, Boolean>{
 TextView timeView;
 int limit_time = 0;
 TimeCountDownTask(TextView timeView, int time){
 this.timeView = timeView;
 this.limit_time = time;
 }
 @Override
 protected Boolean doInBackground(Void... params) {
  while(limit_time > 0){
  mHandler.post(new Runnable(){
   @Override
   public void run() {
   timeView.setText("剩余" + limit_time + "秒");
   }
  });
  try {
   Thread.sleep(1000);
  } catch (Exception e) {
   e.printStackTrace();
  }
  limit_time--;
  }
  return null;
 }
 }
 private class ShowPopAdTask extends AsyncTask<Void, Void, Boolean>{
 Context context;
 RelativeLayout pop_layout;
 LinearLayout popAdView;
 TextView timeView;
 int height_full = 0;
 int height = 0;
 ShowPopAdTask(Context context, RelativeLayout pop_layout, TextView timeView){
  this.context = context;
  this.pop_layout = pop_layout;
  this.timeView = timeView;
 }
 @Override
 protected Boolean doInBackground(Void... params) {
  try {
  height_full = ((Activity)context).getWindowManager().getDefaultDisplay().getHeight();
  int height_tmp = height_full - 75;//75为设备状态栏加标题栏的高度
  height = height_tmp * 5/6;
  while(true){
   if(((Activity)context).getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE
   && height_full <= 480){
   popAdView = AppConnect.getInstance(context).getPopAdView(context, height, height);
   }else{
   popAdView = AppConnect.getInstance(context).getPopAdView(context);
   }
   if(popAdView != null){
   mHandler.post(new Runnable(){
    @Override
    public void run() {
    pop_layout.addView(popAdView);
    popAdView.setId(1);
    //倒计时布局所需的LayoutParams
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_TOP, popAdView.getId());
    params.addRule(RelativeLayout.ALIGN_RIGHT, popAdView.getId());
    // 对手机进行屏幕判断
    int displaySize = SDKUtils.getDisplaySize(context);
    if(displaySize == 320){
     params.topMargin=1;
     params.rightMargin=1;
    }else if(displaySize == 240){
     params.topMargin=1;
     params.rightMargin=1;
    }else if(displaySize == 720){
     params.topMargin=3;
     params.rightMargin=3;
    }else if(displaySize == 1080){
     params.topMargin=4;
     params.rightMargin=4;
    }else{
     params.topMargin=2;
     params.rightMargin=2;
    }
    pop_layout.addView(timeView, params);
    }
   });
   break;
   }
   try {
   Thread.sleep(500);
   } catch (InterruptedException e) {
   e.printStackTrace();
   }
  }
  } catch (Exception e) {
  e.printStackTrace();
  }
  return null;
 }
 }
}

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
返回顶部