Android实现加载广告图片和倒计时的开屏布局
Android 实现 图片 加载 布局 广告 倒计时 开屏
2023-06-13 09:15:39 时间
这是一个android开屏布局的实例,可以用于加载广告图片和倒计时的布局。程序中设置的LayoutParams,划分额外空间比例为6分之5,具体权重比例可根据用户自己需求来自定义,异步加载广告图片,相关的Android代码。
具体实现代码如下:
packagecn.waps.extend; importandroid.app.Activity; importandroid.content.Context; importandroid.content.res.Configuration; importandroid.graphics.Color; importandroid.graphics.drawable.ShapeDrawable; importandroid.graphics.drawable.shapes.RoundRectShape; importandroid.os.AsyncTask; importandroid.os.Handler; importandroid.os.Looper; importandroid.view.Gravity; importandroid.view.View; importandroid.view.ViewGroup.LayoutParams; importandroid.widget.LinearLayout; importandroid.widget.RelativeLayout; importandroid.widget.TextView; importcom.qcn.wzlz.AppConnect; importcom.qcn.wzlz.SDKUtils; publicclassLoadingPopAd{ privatefinalstaticHandlermHandler=newHandler(); privatestaticLoadingPopAdloadingAppPopAd; publicstaticLoadingPopAdgetInstance(){ if(loadingAppPopAd==null){ loadingAppPopAd=newLoadingPopAd(); } if(Looper.myLooper()==null){ Looper.prepare(); } returnloadingAppPopAd; } /** *获取开屏布局 *@paramcontext *@paramtime *@return */ publicViewgetContentView(Contextcontext,inttime){ returngetLoadingLayout(context,time); } privateLinearLayoutgetLoadingLayout(finalContextcontext,finalinttime){ //整体布局 LinearLayoutlayout=newLinearLayout(context); layout.setOrientation(LinearLayout.VERTICAL); layout.setGravity(Gravity.CENTER); intbg_id=context.getResources().getIdentifier("loading_bg","drawable",context.getPackageName()); if(bg_id!=0){ layout.setBackgroundResource(bg_id); } //加载广告图片和倒计时的布局,用与 LinearLayoutl_layout=newLinearLayout(context); l_layout.setGravity(Gravity.CENTER); //设置LayoutParams,划分额外空间比例为6分之5(具体权重比例可根据自己需求自定义) l_layout.setLayoutParams(newLinearLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT,1f)); //加载图片的布局 RelativeLayoutpop_layout=newRelativeLayout(context); TextViewtimeView=newTextView(context); timeView.setText("剩余"+time+"秒"); timeView.setTextSize(10); timeView.setTextColor(Color.BLACK); timeView.setPadding(8,3,6,2); intnum=12; //对手机进行屏幕判断 intdisplaySize=SDKUtils.getDisplaySize(context); if(displaySize==320){ num=8; }elseif(displaySize==240){ num=6; }elseif(displaySize==720){ num=16; }elseif(displaySize==1080){ num=20; } float[]outerRadii=newfloat[]{0,0,num,num,0,0,num,num}; ShapeDrawabletimeView_shapeDrawable=newShapeDrawable(); timeView_shapeDrawable.setShape(newRoundRectShape(outerRadii,null,null)); timeView_shapeDrawable.getPaint().setColor(Color.argb(255,255,255,255)); timeView.setBackgroundDrawable(timeView_shapeDrawable); //异步执行倒计时 //异步加载广告图片 newShowPopAdTask(context,pop_layout,timeView).execute(); newTimeCountDownTask(timeView,time).execute(); TextViewtextView=newTextView(context); textView.setLayoutParams(newLinearLayout.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); returnlayout; } privateclassTimeCountDownTaskextendsAsyncTask<Void,Void,Boolean>{ TextViewtimeView; intlimit_time=0; TimeCountDownTask(TextViewtimeView,inttime){ this.timeView=timeView; this.limit_time=time; } @Override protectedBooleandoInBackground(Void...params){ while(limit_time>0){ mHandler.post(newRunnable(){ @Override publicvoidrun(){ timeView.setText("剩余"+limit_time+"秒"); } }); try{ Thread.sleep(1000); }catch(Exceptione){ e.printStackTrace(); } limit_time--; } returnnull; } } privateclassShowPopAdTaskextendsAsyncTask<Void,Void,Boolean>{ Contextcontext; RelativeLayoutpop_layout; LinearLayoutpopAdView; TextViewtimeView; intheight_full=0; intheight=0; ShowPopAdTask(Contextcontext,RelativeLayoutpop_layout,TextViewtimeView){ this.context=context; this.pop_layout=pop_layout; this.timeView=timeView; } @Override protectedBooleandoInBackground(Void...params){ try{ height_full=((Activity)context).getWindowManager().getDefaultDisplay().getHeight(); intheight_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(newRunnable(){ @Override publicvoidrun(){ pop_layout.addView(popAdView); popAdView.setId(1); //倒计时布局所需的LayoutParams RelativeLayout.LayoutParamsparams=newRelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_TOP,popAdView.getId()); params.addRule(RelativeLayout.ALIGN_RIGHT,popAdView.getId()); //对手机进行屏幕判断 intdisplaySize=SDKUtils.getDisplaySize(context); if(displaySize==320){ params.topMargin=1; params.rightMargin=1; }elseif(displaySize==240){ params.topMargin=1; params.rightMargin=1; }elseif(displaySize==720){ params.topMargin=3; params.rightMargin=3; }elseif(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(InterruptedExceptione){ e.printStackTrace(); } } }catch(Exceptione){ e.printStackTrace(); } returnnull; } } }
相关文章
- android开机动画包制作工具实现「建议收藏」
- android gallery当前view变大,GitHub – hutcwp/GalleryViewDemo: 实现画廊效果(中间放大两边缩小)无限循环向左滑向右滑,Viewpager和Recycl
- delphixe5 android,Delphi XE5 Android手机端转换Ansi字符串
- android-短信验证功能,Android实现获取短信验证码的功能以及自定义GUI短信验证详解…
- android toast全屏,Android Toast实现全屏显示
- Unity+Android GET和POST方式的简单实现API请求(人像动漫化)
- Android 自定义TextView实现文字和图片居中显示
- android 电子签名 手写签名 功能实现
- Android 实现水印背景效果
- 【Android Gradle 插件】Android Plugin DSL Reference 文档介绍 ( 1.2 ~ 3.4 版本文档地址 | 4.1 ~ 7.1 版本文档地址 )
- 【Android UI】贝塞尔曲线 ④ ( 使用 android.graphics.Path 提供的 cubicTo 方法绘制三阶贝塞尔曲线示例 )
- android TabLayout实现京东详情效果详解手机开发
- 开发者让Android应用能运行在Windows、Mac和Linux上
- Android向导框架(Wizard framework)的一种实现(需要源码的同学请留言)
- 对比 Android 和 iPhone 的优缺点
- Android实现PHP连接MySQL进行数据交互(android通过php连接mysql)
- android设置圆角图片实现代码
- Android调用相机并将照片存储到sd卡上实现方法
- Android文件下载进度条的实现代码
- Android文本框搜索和清空效果实现代码及简要概述
- android获取音乐文件的内置专辑图片实现思路及代码
- Android设置桌面背景图片的实现方法
- Android图片缩放与旋转的实现详解
- android在异步任务中关闭Cursor的代码方法
- android将图片转换存到数据库再从数据库读取转换成图片实现代码
- android自定义按钮示例(重写imagebutton控件实现图片按钮)
- Android中自定义加载样式图片的具体实现
- android实现始终显示overflow菜单的方法
- Python实现过滤单个Android程序日志脚本分享