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

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

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

这是一个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,num,num}; ShapeDrawable timeVIEw_shapeDrawable = new ShapeDrawable(); timeVIEw_shapeDrawable.setShape(new RoundRectShape(outerRadii,null,null)); timeVIEw_shapeDrawable.getPaint().setcolor(color.argb(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,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,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实现加载广告图片和倒计时的开屏布局全部内容,希望文章能够帮你解决Android实现加载广告图片和倒计时的开屏布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1142415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存