国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

Android實現加載廣告圖片和倒計時的開屏布局

2020-04-11 11:47:39
字體:
來源:轉載
供稿:網友

這是一個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; } }}

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桦南县| 镇远县| 巫山县| 荃湾区| 乐安县| 青州市| 桃园市| 赣榆县| 安庆市| 淄博市| 荥阳市| 衡山县| 南召县| 齐河县| 乐安县| 甘泉县| 夹江县| 会昌县| 天祝| 革吉县| 尉氏县| 孟村| 东阳市| 宁南县| 泰来县| 尼勒克县| 炉霍县| 加查县| 兴安盟| 瑞安市| 抚远县| 陇川县| 宜城市| 繁峙县| 松原市| 眉山市| 嘉黎县| 遂宁市| 瑞昌市| 廉江市| 辰溪县|