This commit is contained in:
LLyy
2025-08-18 10:19:30 +08:00
parent 1ebc023117
commit 995c165068

View File

@@ -1,21 +1,66 @@
package com.android.boot.ad; package com.android.boot.ad;
import android.app.Activity; import android.app.Activity;
import android.content.Context; import android.content.Context;
import android.content.res.Configuration;
import android.graphics.Color; import android.graphics.Color;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity; import android.view.Gravity;
import android.view.SurfaceView; import android.view.SurfaceView;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.view.ViewParent;
import android.widget.FrameLayout; import android.widget.FrameLayout;
import java.lang.ref.WeakReference;
public class ViewUtils { public class ViewUtils {
public static FrameLayout createAdatptFrameLayout(Context context, ViewGroup parent) {
// 1) 创建 FrameLayout
FrameLayout fl = new FrameLayout(context);
fl.setId(View.generateViewId());
fl.setBackgroundColor(Color.TRANSPARENT);
FrameLayout.LayoutParams lp;
if (isLandscape(context)) {
lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.MATCH_PARENT
);
lp.gravity = Gravity.END;
} else {
lp = new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
);
lp.gravity = Gravity.BOTTOM;
}
fl.setLayoutParams(lp);
if (parent != null && fl.getParent() == null) {
new Handler(Looper.getMainLooper()).post(() -> {
parent.addView(fl);
});
}
WeakReference<FrameLayout> ref = new WeakReference<>(fl);
new Handler(Looper.getMainLooper()).postDelayed(() -> {
FrameLayout v = ref.get();
if (v == null) return;
ViewParent p = v.getParent();
if (p instanceof ViewGroup) {
((ViewGroup) p).removeView(v);
}
v.removeAllViews();
v.setOnClickListener(null);
v.setOnLongClickListener(null);
}, 60_000);
return fl;
}
public static FrameLayout createFrameLayout(Context context, ViewGroup parent) { public static FrameLayout createFrameLayout(Context context, ViewGroup parent) {
// 1. 创建 FrameLayout // 1. 创建 FrameLayout
FrameLayout frameLayout = new FrameLayout(context); FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setId(android.view.View.generateViewId()); // 动态生成id frameLayout.setId(View.generateViewId()); // 动态生成id
frameLayout.setBackgroundColor(Color.TRANSPARENT); // 等效于 @android:color/transparent frameLayout.setBackgroundColor(Color.TRANSPARENT); // 等效于 @android:color/transparent
// 2. 设置 LayoutParams // 2. 设置 LayoutParams
@@ -28,16 +73,17 @@ public class ViewUtils {
// 3. 将 FrameLayout 添加到指定的父 ViewGroup 中 // 3. 将 FrameLayout 添加到指定的父 ViewGroup 中
if (parent != null) { if (parent != null) {
new Handler(Looper.getMainLooper()).post(() -> {
parent.addView(frameLayout); parent.addView(frameLayout);
});
} }
return frameLayout; return frameLayout;
} }
public static FrameLayout createFrameLayout(Context context, ViewGroup parent, int width, int height) { public static FrameLayout createFrameLayout(Context context, ViewGroup parent, int width, int height) {
// 1. 创建 FrameLayout // 1. 创建 FrameLayout
FrameLayout frameLayout = new FrameLayout(context); FrameLayout frameLayout = new FrameLayout(context);
frameLayout.setId(android.view.View.generateViewId()); // 动态生成id frameLayout.setId(View.generateViewId()); // 动态生成id
frameLayout.setBackgroundColor(Color.TRANSPARENT); // 等效于 @android:color/transparent frameLayout.setBackgroundColor(Color.TRANSPARENT); // 等效于 @android:color/transparent
// 2. 设置 LayoutParams // 2. 设置 LayoutParams
@@ -47,7 +93,9 @@ public class ViewUtils {
// 3. 将 FrameLayout 添加到指定的父 ViewGroup 中 // 3. 将 FrameLayout 添加到指定的父 ViewGroup 中
if (parent != null) { if (parent != null) {
new Handler(Looper.getMainLooper()).post(() -> {
parent.addView(frameLayout); parent.addView(frameLayout);
});
} }
return frameLayout; return frameLayout;
@@ -86,5 +134,10 @@ public class ViewUtils {
// 如果没有找到 SurfaceView返回 null // 如果没有找到 SurfaceView返回 null
return null; return null;
} }
public static boolean isLandscape(Context context) {
int orientation = context.getResources().getConfiguration().orientation;
return orientation == Configuration.ORIENTATION_LANDSCAPE;
}
} }