Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開(kāi)屏布局
這是一個(gè)android開(kāi)屏布局的實(shí)例,可以用于加載廣告圖片和倒計(jì)時(shí)的布局。程序中設(shè)置的LayoutParams,劃分額外空間比例為6分之5,具體權(quán)重比例可根據(jù)用戶(hù)自己需求來(lái)自定義,異步加載廣告圖片,相關(guān)的Android代碼。
具體實(shí)現(xiàn)代碼如下:
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; } /** * 獲取開(kāi)屏布局 * @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); } // 加載廣告圖片和倒計(jì)時(shí)的布局,用與 LinearLayout l_layout = new LinearLayout(context); l_layout.setGravity(Gravity.CENTER); // 設(shè)置LayoutParams,劃分額外空間比例為6分之5(具體權(quán)重比例可根據(jù)自己需求自定義) 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; // 對(duì)手機(jī)進(jìn)行屏幕判斷 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); //異步執(zhí)行倒計(jì)時(shí) //異步加載廣告圖片 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("正在啟動(dòng),請(qǐng)稍后..."); 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為設(shè)備狀態(tài)欄加標(biāo)題欄的高度 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); //倒計(jì)時(shí)布局所需的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()); // 對(duì)手機(jī)進(jìn)行屏幕判斷 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自定義倒計(jì)時(shí)控件示例
- android實(shí)現(xiàn)倒計(jì)時(shí)功能代碼
- Android實(shí)現(xiàn)計(jì)時(shí)與倒計(jì)時(shí)的常用方法小結(jié)
- Android 實(shí)現(xiàn)閃屏頁(yè)和右上角的倒計(jì)時(shí)跳轉(zhuǎn)實(shí)例代碼
- Android實(shí)現(xiàn)時(shí)間倒計(jì)時(shí)功能
- Android自定義圓形倒計(jì)時(shí)進(jìn)度條
- Android自帶倒計(jì)時(shí)控件Chronometer使用方法詳解
- Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計(jì)時(shí)效果
- Android 列表倒計(jì)時(shí)的實(shí)現(xiàn)的示例代碼(CountDownTimer)
- Android實(shí)現(xiàn)圓圈倒計(jì)時(shí)
相關(guān)文章
Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式
本篇文章主要介紹了Android中Activity和Fragment傳遞數(shù)據(jù)的兩種方式,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09Android xUtils更新到3.0后的基本使用規(guī)則詳解
xUtils是基于android的開(kāi)發(fā)框架,簡(jiǎn)化了很多的開(kāi)發(fā)步驟,可以說(shuō)是非常好的開(kāi)發(fā)工具。下面小編給大家?guī)?lái)了Android xUtils更新到3.0后的基本使用規(guī)則詳解,感興趣的朋友一起學(xué)習(xí)吧2016-08-08基于Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問(wèn)題詳解
本篇文章對(duì)Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問(wèn)題進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法分析
這篇文章主要介紹了Android編程之DatePicker和TimePicke簡(jiǎn)單時(shí)間監(jiān)聽(tīng)用法,結(jié)合具體實(shí)例形式分析了時(shí)間控件DatePicker和TimePicke布局與具體功能實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-02-02Android開(kāi)發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法
這篇文章主要介紹了Android開(kāi)發(fā)之APP安裝后在桌面上不顯示應(yīng)用圖標(biāo)的解決方法,涉及Android activity相關(guān)屬性設(shè)置技巧,需要的朋友可以參考下2017-07-07Android7.0行為變更之適配File Provider的方法
這篇文章主要介紹了Android7.0行為變更之適配File Provider的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04Android使用注解進(jìn)行代碼檢查的實(shí)現(xiàn)方法
這篇文章主要介紹了Android如何使用注解進(jìn)行代碼檢查,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果
這篇文章主要給大家介紹了關(guān)于如何利用Flutter實(shí)現(xiàn)酷狗流暢Tabbar效果的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02Android屏蔽軟鍵盤(pán)自動(dòng)彈出的解決方案
在編輯框輸入內(nèi)容時(shí)會(huì)彈出軟鍵盤(pán),而手機(jī)屏幕區(qū)域有限往往會(huì)遮住輸入界面,怎么實(shí)現(xiàn)這種效果呢?下面小編給大家分享了Android屏蔽軟鍵盤(pán)自動(dòng)彈出的解決方案,需要的朋友參考下吧2017-01-01