Android实现加载广告图片和倒计时的开屏布局
程序员文章站
2022-05-27 10:24:03
这是一个android开屏布局的实例,可以用于加载广告图片和倒计时的布局。程序中设置的layoutparams,划分额外空间比例为6分之5,具体权重比例可根据用户自己需求来...
这是一个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; } } }