最近在翻看学长之前的佳作,发现了个好玩的,也是自己以前一直想实现却没有实现的一个东西,高斯模糊。话不多说,发车!
[TOC]
一、高斯模糊是什么
在 Android 中,Dialog 的高斯模糊指的是将 Dialog 界面进行模糊处理,使得 Dialog 呈现出一种模糊、朦胧的效果。
高斯模糊是一种常见的图像处理技术,通过对图像中的像素进行加权平均来减少图像的细节和噪点,从而达到模糊效果。在 Dialog 中使用高斯模糊可以用来实现一些特殊的视觉效果,或者提供一种更加柔和的界面体验。
在 Android 中,实现 Dialog 的高斯模糊通常有两种方式:
使用系统提供的 DialogFragment:可以在 DialogFragment 中使用 Window.setBackgroundDrawable() 方法来设置 Dialog 的背景,通过自定义 Drawable 实现高斯模糊效果,然后将该 Drawable 设置为 Dialog 的背景即可。
自定义 Dialog 样式:可以通过自定义 Dialog 的样式文件,在样式文件中设置 Dialog 的背景为自定义的 Drawable,然后在该 Drawable 中实现高斯模糊效果。
本文将采用第二种方式进行实现,第一种本人也实现过,不过是学习他人代码进行实现的。
二、实现Dialog高斯模糊的准备工作
首先做一些准备工作,在项目中导入这些东西。
这是自定义的一个dialog主题样式。
1 2 3 4 5 6 7
   | <style name="SquareEntranceDialogStyle" parent="@android:style/Theme.Dialog">     <item name="android:windowNoTitle">true</item>     <item name="android:windowIsFloating">false</item>     <item name="android:windowBackground">@android:color/transparent</item>     <item name="android:windowTranslucentNavigation">true</item>     <item name="android:statusBarColor">@android:color/transparent</item> </style>
 
  | 
 
这是一张白色的背景图。
1 2 3 4 5
   | <?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android">     <corners android:radius="15dp"/>     <solid android:color="@color/white" /> </shape>
 
  | 
 
dialog弹窗的话,这里由于是自定义,因人而异,我就不贴学长的代码了,因为是学习学长的哈哈。
三、具体代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55
   |              Bitmap bitmap = getCurrentBitmap();
              AddCategoryDialog dialog = new AddCategoryDialog.Builder(mainActivity).setCancelableOutside(true).create();             Window window = dialog.getWindow();             View decorView = window.getDecorView();             window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));                          int option = View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;             decorView.setSystemUiVisibility(option);             window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);             window.setStatusBarColor(Color.TRANSPARENT);                          window.setNavigationBarColor(Color.TRANSPARENT);                          window.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL);                          Bitmap blurBg = BlurBitmap.blur(mainActivity, bitmap);             window.setBackgroundDrawable(new BitmapDrawable(getResources(), blurBg));             bitmap.recycle();
              View view_bt = LayoutInflater.from(mainActivity).inflate(R.layout.dialog_add_category,null);
  红色的这里是你的dialog布局             ImageView bt_cancel = (ImageView) view_bt.findViewById(R.id.bt_cancel_add);             bt_cancel.setOnClickListener(view_cancel -> {                                  dialog.dismiss();             });             ImageView bt_finish = (ImageView) view_bt.findViewById(R.id.bt_finish_add);                          EditText text = (EditText) view_bt.findViewById(R.id.edit_add);             TextView count_text = (TextView) view_bt.findViewById(R.id.count_name);             dialog.setContentView(view_bt);                          dialog.setOnDismissListener(dialog1 -> {                                  if (blurBg != null && !blurBg.isRecycled()) {                     blurBg.recycle();                     Handler handler = new Handler();                     new Thread(){                         @Override                         public void run() {                             super.run();                             handler.post(new Runnable() {                                 @Override                                 public void run() {                                     System.out.println("我是在new Hhread里面使用handler.post");                                 }                             });                         }                     }.start();                 }             });             dialog.show();
 
  | 
 
1 2 3 4 5 6 7 8
   | public Bitmap getCurrentBitmap () {     View activityView = getWindow().getDecorView();     activityView.setDrawingCacheEnabled(true);     activityView.destroyDrawingCache();     activityView.buildDrawingCache();     Bitmap bmp = activityView.getDrawingCache();     return bmp; }
 
  | 
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
   | public class BlurBitmap {     
 
      private static final float BITMAP_SCALE = 0.4f;     
 
      private static final float BLUR_RADIUS = 12f;
      
 
 
 
 
 
      public static Bitmap blur(Context context, Bitmap image) {                  int width = Math.round(image.getWidth() * BITMAP_SCALE);         int height = Math.round(image.getHeight() * BITMAP_SCALE);
                   Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);                  Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
                   RenderScript rs = RenderScript.create(context);
                   ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
                            Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);         Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
                   blurScript.setRadius(BLUR_RADIUS);                  blurScript.setInput(tmpIn);                  blurScript.forEach(tmpOut);
                   tmpOut.copyTo(outputBitmap);
          return outputBitmap;     } }
  当然,上面也说了还可以用另外一种方式实现,可以自己去尝试。
 
  |