可以参考此文章:Android中Activity和AppcompatActivity的区别(详细解析)_today_work的博客-CSDN博客
/**
* 今天接到的需求是Activty 主题是Dialog样式,背景需要是桌面当时截图并做高斯模糊
* 1、Android Studio 默认继承AppcompactActivity,这时需要改成继承Activity 主题使用@android:style/Theme.Translucent.NoTitleBar.Fullscreen
* 2、AppcompaActivity 主界面带有toolbar的标题栏,而Activity 则没有;
* 3、AppcompaActivity theme主题只能用android:theme=”@style/AppTheme (appTheme主题或者其子类),
* 而不能用android:style。否则会提示错误: Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
*/
<application
android:name=".App"
android:allowBackup="true"
android:icon="@mipmap/icon_app_pip_210x"
android:label="@string/app_name"
android:roundIcon="@mipmap/icon_app_pip_210x"
android:supportsRtl="true"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这里是同事给出的方法
/**
* 反射截图
* @param isLandscape 横竖屏方式截屏会有差异
* @param context 上下文
* @return bitmap
*/
public static Bitmap screenshotSystem(boolean isLandscape,Context context) {
Class<?> demo;
Bitmap bitmap = null;
try {
demo = Class.forName("android.view.SurfaceControl");
Method method = demo.getMethod("screenshot", Rect.class, int.class, int.class, int.class);
Object obj = method.invoke(null, new Rect(), Util.getScreenWidth(context), Util.getScreenHeight(context), isLandscape ? 0 : 1);
//最后一个参数是拍摄角度0,1,2,3对应0,90,180,270
bitmap = (Bitmap) obj;
} catch (Exception e) {
e.printStackTrace();
Log.d("yyy", "screenshotSystem: error");
}
return bitmap;
}
/**
* bitmap整体先缩小
* @param bitmap
* @return
*/
public static Bitmap small(Bitmap bitmap) {
if (bitmap == null) {
return null;
}
int w = bitmap.getWidth();
int h = bitmap.getHeight();
int retX, retY;
Bitmap bmp = null;
retX = 0;
retY = 0;
Matrix matrix = new Matrix();
matrix.postScale(1 / 4f, 1 / 4f);
bmp = Bitmap.createBitmap(bitmap, retX, retY, w, h, matrix,
true);
if (!bitmap.equals(bmp) && !bitmap.isRecycled()) {
bitmap.recycle(); //标记为被回收状态(dead),不可再使用
bitmap = null;
}
return bmp;// Bitmap.createBitmap(bitmap, retX, retY, nw, nh, null,
}
/**
* 这里不是很懂 //android api 30把 hardware bitmap 转换成software bitmap
* @param src
* @return
*/
@RequiresApi(api = Build.VERSION_CODES.P)
public static Bitmap convertHardWareBitmap(Bitmap src) {
if (src.getConfig() != Bitmap.Config.HARDWARE) {
//return Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight());
return src;
}
final int w = src.getWidth();
final int h = src.getHeight();
// For hardware bitmaps, use the Picture API to directly create a software bitmap
Picture picture = new Picture();
Canvas canvas = picture.beginRecording(w, h);
canvas.drawBitmap(src, 0, 0, null);
picture.endRecording();
return Bitmap.createBitmap(picture, w, h,
Bitmap.Config.ARGB_8888);
}
/**
* 对缩小的bitmap 进行高斯模糊,效率高
* @param bkg
* @param context
* @return
*/
public static Bitmap blur(Bitmap bkg,Context context) {
final RenderScript rs = RenderScript.create(context);
final Allocation input = Allocation.createFromBitmap(rs, bkg, Allocation.MipmapControl.MIPMAP_NONE,
Allocation.USAGE_SCRIPT);
final Allocation output = Allocation.createTyped(rs, input.getType());
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
script.setRadius(5);
script.setInput(input);
script.forEach(output);
output.copyTo(bkg);
return bkg;
}
/**
* 接收缩小高斯模糊后的bitmap 恢复原来截图的大小
* @param bitmap
* @return
*/
public static Bitmap big(Bitmap bitmap) {
Matrix matrix = new Matrix();
matrix.postScale(4f, 4f); //长和宽放大缩小的比例
Bitmap resizeBmp = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
return resizeBmp;
}
MainActivity 继承Activity 在onCreate 方法截图 给Decorview 设置背景
public class MainActivity extends Activity implements View.OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
backgroundBokeh();
super.onCreate(savedInstanceState);
setContentView(R.layout.source_view);
initView();
}
/**
* 背景虚化
*/
@RequiresApi(api = Build.VERSION_CODES.P)
public void backgroundBokeh() {
Bitmap blur = Util.screenshotSystem(getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE,this);
blur = Util.small(blur);
blur = Util.convertHardWareBitmap(blur);
blur = Util.blur(blur,this);
blur = Util.big(blur);
getWindow().getDecorView().setBackground(new BitmapDrawable(this.getResources(),
blur));
}
}