Android实现加载广告图片和倒计时的开屏布局
作者:shichen2014 时间:2023-04-29 13:54:45
这是一个android开屏布局的实例,可以用于加载广告图片和倒计时的布局。程序中设置的LayoutParams,划分额外空间比例为6分之5,具体权重比例可根据用户自己需求来自定义,异步加载广告图片,相关的Android代码。
具体实现代码如下:
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;
}
}
}
标签:Android,布局
0
投稿
猜你喜欢
利用Java8 Optional类优雅如何地解决空指针问题
2023-07-30 04:58:13
Mybatis自关联查询一对多查询的实现示例
2021-08-21 04:06:16
详解path和classpath的区别
2023-05-02 13:28:32
string boot 与 自定义interceptor的实例讲解
2023-10-27 17:03:20
Devexpress treelist 简介
2023-02-13 23:07:56
Android实现记住用户名和密码功能
2023-10-06 13:02:07
Android获取内外置存储卡的方法
2023-12-03 00:41:09
java使用FileVisitor遍历文件和目录
2021-07-17 13:25:21
WinForm实现自定义右下角提示效果的方法
2023-01-19 08:19:57
解决@RequestBody部分属性丢失的问题
2023-08-01 15:00:21
如何优雅的处理Spring Boot异常信息详解
2023-11-29 09:50:02
Unity封装延时调用定时器
2022-10-14 05:43:53
MyBatis源码浅析(一)开篇
2022-09-28 03:28:24
java多态的向上转型的概念及实例分析
2023-06-12 08:30:35
Mybatis 动态SQL的几种实现方法
2023-11-10 12:15:15
计算机编程语言发展史
2022-10-07 21:28:49
Spring Boot配置Thymeleaf(gradle)的简单使用
2023-04-18 07:41:42
SpringBoot使用prometheus监控的示例代码
2021-05-29 02:37:41
C#实现发送邮件的三种方法
2021-08-06 07:36:00
springboot+nginx+https+linux实现负载均衡加域名访问简单测试
2022-04-26 22:35:13