Android實(shí)現(xiàn)加載廣告圖片和倒計(jì)時(shí)的開(kāi)屏布局
這是一個(gè)android開(kāi)屏布局的實(shí)例,可以用于加載廣告圖片和倒計(jì)時(shí)的布局。程序中設(shè)置的LayoutParams,劃分額外空間比例為6分之5,具體權(quán)重比例可根據(jù)用戶自己需求來(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 實(shí)現(xiàn)ScrollView自動(dòng)滾動(dòng)的實(shí)例代碼
這篇文章主要介紹了android 實(shí)現(xiàn)ScrollView自動(dòng)滾動(dòng)的實(shí)例代碼,有需要的朋友可以參考一下2014-01-01
Android開(kāi)發(fā)之登錄驗(yàn)證實(shí)例教程
這篇文章主要介紹了Android開(kāi)發(fā)之登錄驗(yàn)證實(shí)現(xiàn)方法,包括發(fā)送數(shù)據(jù)、服務(wù)器端驗(yàn)證、配置文件等,需要的朋友可以參考下2014-08-08
HorizontalScrollView水平滾動(dòng)控件使用方法詳解
這篇文章主要為大家詳細(xì)介紹了HorizontalScrollView水平滾動(dòng)控件的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
Android AlertDialog對(duì)話框用法示例
這篇文章主要介紹了Android AlertDialog對(duì)話框用法,結(jié)合實(shí)例形式分析了AlertDialog對(duì)話框的功能及常見(jiàn)使用技巧,需要的朋友可以參考下2016-06-06
Android LinearLayout實(shí)現(xiàn)自動(dòng)換行
這篇文章主要為大家詳細(xì)介紹了Android LinearLayout實(shí)現(xiàn)自動(dòng)換行,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-08-08
Android 登錄處理簡(jiǎn)單實(shí)例(源碼下載)
這篇文章主要介紹了Android 登錄處理簡(jiǎn)單實(shí)例(源碼下載)的相關(guān)資料,需要的朋友可以參考下2017-03-03
Android進(jìn)階從字節(jié)碼插樁技術(shù)了解美團(tuán)熱修復(fù)實(shí)例詳解
這篇文章主要為大家介紹了Android進(jìn)階從字節(jié)碼插樁技術(shù)了解美團(tuán)熱修復(fù)實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-01-01
Android 手機(jī)瀏覽器調(diào)試使用Chrome進(jìn)行調(diào)試實(shí)例詳解
這篇文章主要介紹了Android 手機(jī)瀏覽器調(diào)試使用Chrome進(jìn)行調(diào)試實(shí)例詳解的相關(guān)資料,這里提供了實(shí)例,需要的朋友可以參考下2016-12-12

