目录
1.在android 10版本,SystemUI 下拉列表界面实现高斯模糊效果
2.在android 13版本,长按关机按键,弹窗显示关机画面,背景使用全屏高斯模糊背景效果
一.截图功能和高斯模糊效果
frameworks/base/packages/SystemUI/src/com/android/systemui/util/BlurUtil.java
/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License
*/
package com.android.systemui.util;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Rect;
import android.renderscript.Element;
import android.renderscript.Allocation;
import android.renderscript.ScriptIntrinsicBlur;
import android.renderscript.RenderScript;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.SurfaceControl;
import android.view.WindowManager;
import android.util.Log;
public class BlurUtil {
//截图功能,Android 12 版本以下使用
public static Bitmap screenshotSurface(Context context) {
float BITMAP_SCALE = 0.35f;
Bitmap bitmap;
DisplayMetrics displayMetrics = new DisplayMetrics();
Display defaultDisplay = ((WindowManager) context.getSystemService("window")).getDefaultDisplay();
defaultDisplay.getRealMetrics(displayMetrics);
int rotation = defaultDisplay.getRotation();
bitmap = SurfaceControl.screenshot(new Rect(), Math.round(displayMetrics.widthPixels * BITMAP_SCALE),
Math.round(displayMetrics.heightPixels * BITMAP_SCALE), false, rotation);
if (bitmap == null) {
Log.e("ScreenShotHelper", "screenShotBitmap error bitmap is null");
return null;
}
bitmap.prepareToDraw();
return bitmap.copy(Bitmap.Config.ARGB_8888, true);
}
public static Bitmap blur(Context context, Bitmap inputBitmap, int intensity) {
float BLUR_RADIUS = 7.5f;
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
if (intensity > 0 && intensity <= 100) {
BLUR_RADIUS = (float) intensity * 0.25f;
}
theIntrinsic.setRadius(BLUR_RADIUS);
theIntrinsic.setInput(tmpIn);
theIntrinsic.forEach(tmpOut);
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
//截图功能,Android 12 版本以上使用
public static Bitmap screenshotSurface(Context context) {
Bitmap bmp;
DisplayMetrics displayMetrics = new DisplayMetrics();
Rect sourceCrop = new Rect(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
Display defaultDisplay = ((WindowManager) context.getSystemService("window")).getDefaultDisplay();
defaultDisplay.getRealMetrics(displayMetrics);
int rotation = defaultDisplay.getRotation();
final DisplayAddress.Physical physicalAddress = (DisplayAddress.Physical) defaultDisplay.getAddress();
final IBinder displayToken = SurfaceControl.getPhysicalDisplayToken(
physicalAddress.getPhysicalDisplayId());
final SurfaceControl.DisplayCaptureArgs captureArgs =
new SurfaceControl.DisplayCaptureArgs.Builder(displayToken)
.setSourceCrop(sourceCrop)
.setSize(displayMetrics.widthPixels, displayMetrics.heightPixels)
.build();
final SurfaceControl.ScreenshotHardwareBuffer screenshotBuffer =
SurfaceControl.captureDisplay(captureArgs);
bmp = screenshotBuffer == null ? null : screenshotBuffer.asBitmap();
return bmp;
}
mBlurView.setRenderEffect(RenderEffect.createBlurEffect(500F, 500F, Shader.TileMode.CLAMP));
二.案例
1.在android 10版本,SystemUI 下拉列表界面实现高斯模糊效果
[SystemUI]增加背景高斯模糊功能,去掉锁屏下拉列表功能
Changes to be committed:
modified: frameworks/base/packages/SystemUI/res/layout/status_bar_expanded.xml
modified: frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
new file: frameworks/base/packages/SystemUI/src/com/android/systemui/util/BitmapUtils.java
new file: frameworks/base/packages/SystemUI/src/com/android/systemui/util/BlurUtil.java
frameworks/base/packages/SystemUI/src/com/android/systemui/statusbar/phone/NotificationPanelView.java
import javax.inject.Inject;
import javax.inject.Named;
import com.android.systemui.util.BitmapUtils;
import com.android.systemui.util.ScreenShotUtil;
import com.android.systemui.util.BlurUtil;
import android.graphics.Bitmap;
import android.widget.ImageView;
public class NotificationPanelView extends PanelView implements
ExpandableView.OnHeightChangedListener,
@@ -232,6 +237,12 @@
// Used for two finger gesture as well as accessibility shortcut to QS.
private boolean mQsExpandImmediate;
private boolean mTwoFingerQsExpandPossible;
private static final int BLUR_START = 200;
private static final int BLUR_END = 700;
private View mAlphaView;
private ImageView mBlurView;
private boolean mIsFullClose;
/**
* If we are in a panel collapsing