88import android .content .Context ;
99
1010public final class BlurEngine {
11- // Tỉ lệ thu nhỏ và bán kính mờ để cân bằng hiệu năng
11+ // --- Các thành phần cũ để giữ tương thích hệ thống ---
12+ public static BlurController controller = new BlurController ();
13+ public static volatile Bitmap blurBitmap ;
14+ public static boolean isPaused = false ;
15+ public static float DEFAULT_CORNER_RADIUS = 30.0f ;
16+
17+ // --- Cấu hình cho tính năng Blur thời gian thực (Lựa chọn 2) ---
1218 private static final float SCALE_FACTOR = 0.15f ;
1319 private static final int BLUR_RADIUS = 8 ;
1420
15- public static boolean isPaused = false ;
16- public float cornerRadius = 30.0f ;
21+ public float cornerRadius = DEFAULT_CORNER_RADIUS ;
1722
1823 private View targetView ;
1924 private int [] location = new int [2 ];
@@ -38,7 +43,7 @@ public void setup() {
3843 }
3944
4045 /**
41- * CẬP NHẬT: Tự động chụp nội dung phía sau View hiện tại
46+ * Logic mới: Chụp nội dung phía sau trực tiếp để không bị mất blur khi cuộn dài
4247 */
4348 public Bitmap getUpdatedBlurBitmap () {
4449 if (isPaused || targetView .getWidth () <= 0 || targetView .getHeight () <= 0 ) {
@@ -48,38 +53,43 @@ public Bitmap getUpdatedBlurBitmap() {
4853 View rootView = targetView .getRootView ();
4954 if (rootView == null ) return null ;
5055
51- // 1. Tính toán kích thước thu nhỏ
56+ // 1. Tính toán kích thước thu nhỏ để tối ưu hiệu suất
5257 int w = Math .round (targetView .getWidth () * SCALE_FACTOR );
5358 int h = Math .round (targetView .getHeight () * SCALE_FACTOR );
5459
5560 if (w <= 0 || h <= 0 ) return null ;
5661
5762 try {
58- // 2. Khởi tạo hoặc tái sử dụng Bitmap đệm
63+ // 2. Quản lý bộ nhớ đệm Bitmap
5964 if (cachedBitmap == null || cachedBitmap .getWidth () != w || cachedBitmap .getHeight () != h ) {
6065 if (cachedBitmap != null ) cachedBitmap .recycle ();
6166 cachedBitmap = Bitmap .createBitmap (w , h , Bitmap .Config .ARGB_8888 );
6267 cachedCanvas = new Canvas (cachedBitmap );
6368 }
6469
65- // 3. Xác định vị trí của View trên màn hình
70+ // 3. Xác định tọa độ thực tế trên màn hình
6671 targetView .getLocationOnScreen (location );
6772
68- // 4. CHỤP NỘI DUNG PHÍA SAU
73+ // 4. CHỤP NỘI DUNG NỀN (Lựa chọn 2)
6974 cachedCanvas .save ();
7075 cachedCanvas .drawColor (Color .TRANSPARENT , PorterDuff .Mode .CLEAR );
76+
77+ // Di chuyển và scale canvas để "soi" đúng phần diện tích phía sau targetView
7178 cachedCanvas .scale (SCALE_FACTOR , SCALE_FACTOR );
7279 cachedCanvas .translate (-location [0 ], -location [1 ]);
7380
74- // Tạm ẩn chính nó để không bị hiệu ứng gương (chụp đè chính mình )
81+ // Tạm ẩn chính nó để tránh vòng lặp hiển thị (Recursive drawing )
7582 targetView .setVisibility (View .INVISIBLE );
7683 rootView .draw (cachedCanvas );
7784 targetView .setVisibility (View .VISIBLE );
85+
7886 cachedCanvas .restore ();
7987
80- // 5. LÀM MỜ & NHUỘM MÀU
88+ // 5. Làm mờ trực tiếp trên ảnh đã thu nhỏ
8189 fastBlur (cachedBitmap , BLUR_RADIUS );
82- cachedCanvas .drawColor (getBlurTintColor ());
90+
91+ // 6. Phủ màu Tint theo theme (Dark/Light)
92+ cachedCanvas .drawColor (getBlurTintColor ());
8393
8494 return cachedBitmap ;
8595 } catch (Exception e ) {
@@ -97,7 +107,7 @@ public static Paint getStrokePaint(Context context) {
97107 if (strokePaint == null ) {
98108 strokePaint = new Paint (Paint .ANTI_ALIAS_FLAG );
99109 strokePaint .setStyle (Paint .Style .STROKE );
100- strokePaint .setStrokeWidth (3.0f );
110+ strokePaint .setStrokeWidth (3.0f );
101111 }
102112 int colorRes = ThemeModeState .isDarkMode () ? R .color .colorPirmLight : R .color .colorPirmDark ;
103113 int color = ContextCompat .getColor (context , colorRes );
@@ -106,7 +116,7 @@ public static Paint getStrokePaint(Context context) {
106116 }
107117
108118 /**
109- * Thuật toán StackBlur tích hợp trực tiếp để xử lý tại chỗ
119+ * Thuật toán StackBlur tích hợp (Dùng xử lý ảnh nhỏ cực nhanh)
110120 */
111121 private void fastBlur (Bitmap bitmap , int radius ) {
112122 if (radius < 1 ) return ;
0 commit comments