@@ -43,51 +43,42 @@ public Bitmap getUpdatedBlurBitmap() {
4343 targetView .getWidth () <= 0 || targetView .getHeight () <= 0 ) {
4444 return null ;
4545 }
46-
46+
47+ // Lấy RootView thực sự (thường là DecorView của Window)
4748 View rootView = targetView .getRootView ();
4849 if (rootView == null ) return null ;
49-
5050 targetView .getLocationOnScreen (location );
51-
52- int w = targetView .getWidth ();
53- int h = targetView .getHeight ();
54-
55- try {
56- if (cachedBitmap == null || cachedBitmap .getWidth () != w || cachedBitmap .getHeight () != h ) {
57- if (cachedBitmap != null ) cachedBitmap .recycle ();
58- cachedBitmap = Bitmap .createBitmap (w , h , Bitmap .Config .ARGB_8888 );
59- cachedCanvas = new Canvas (cachedBitmap );
60- }
61-
62- // 1. Tính toán tỉ lệ scale
63- float scaleX = (float ) blurBitmap .getWidth () / rootView .getWidth ();
64- float scaleY = (float ) blurBitmap .getHeight () / rootView .getHeight ();
65-
66- // 2. Sử dụng Matrix để dịch chuyển Bitmap thay vì dùng srcRect
67- Matrix matrix = new Matrix ();
68- // Dịch chuyển ngược lại vị trí của View trên màn hình để khớp nội dung
69- matrix .postTranslate (-location [0 ], -location [1 ]);
70- // Scale để khớp với kích thước blurBitmap đã được thu nhỏ
71- matrix .postScale (scaleX , scaleY );
72-
73- // 3. Tạo Shader với chế độ CLAMP (Kéo dãn pixel biên khi tràn)
74- BitmapShader shader = new BitmapShader (blurBitmap , Shader .TileMode .CLAMP , Shader .TileMode .CLAMP );
75- shader .setLocalMatrix (matrix );
76-
77- Paint paint = new Paint (Paint .ANTI_ALIAS_FLAG );
78- paint .setShader (shader );
79-
80- // 4. Vẽ
81- cachedCanvas .drawColor (0 , PorterDuff .Mode .CLEAR );
82- cachedCanvas .drawRect (0 , 0 , w , h , paint );
83-
84- // Vẽ thêm lớp màu Tint
85- cachedCanvas .drawColor (getBlurTintColor ());
51+ float scaleX = (float ) blurBitmap .getWidth () / rootView .getWidth ();
52+ float scaleY = (float ) blurBitmap .getHeight () / rootView .getHeight ();
53+
54+ int w = (int ) (targetView .getWidth () * scaleX );
55+ int h = (int ) (targetView .getHeight () * scaleY );
56+ int x = (int ) (location [0 ] * scaleX );
57+ int y = (int ) (location [1 ] * scaleY );
58+
59+ // Chống tràn biên Bitmap
60+ x = Math .max (0 , Math .min (x , blurBitmap .getWidth () - w ));
61+ y = Math .max (0 , Math .min (y , blurBitmap .getHeight () - h ));
62+
63+ if (w > 0 && h > 0 ) {
64+ try {
65+ if (cachedBitmap == null || cachedBitmap .getWidth () != w || cachedBitmap .getHeight () != h ) {
66+ if (cachedBitmap != null ) cachedBitmap .recycle ();
67+ cachedBitmap = Bitmap .createBitmap (w , h , Bitmap .Config .ARGB_8888 );
68+ cachedCanvas = new Canvas (cachedBitmap );
69+ }
8670
87- return cachedBitmap ;
88- } catch (Exception e ) {
89- return null ;
71+ srcRect .set (x , y , x + w , y + h );
72+ cachedCanvas .drawColor (0 , PorterDuff .Mode .CLEAR );
73+
74+ cachedCanvas .drawBitmap (blurBitmap , srcRect , new Rect (0 , 0 , w , h ), null );
75+ cachedCanvas .drawColor (getBlurTintColor ());
76+ return cachedBitmap ;
77+ } catch (Exception e ) {
78+ return null ;
79+ }
9080 }
81+ return null ;
9182 }
9283
9384 private int getBlurTintColor () {
0 commit comments