66import androidx .core .content .ContextCompat ;
77import com .tool .tree .R ;
88import android .content .Context ;
9+ import android .util .DisplayMetrics ;
910
1011public final class BlurEngine {
1112 public static BlurController controller = new BlurController ();
12-
13- // volatile để đảm bảo tính nhất quán dữ liệu giữa Thread của Controller và UI Thread
1413 public static volatile Bitmap blurBitmap ;
15- public static boolean isPaused = true ;
14+ public static boolean isPaused = false ; // Mặc định để false để có thể chạy ngay
1615
1716 public static float DEFAULT_CORNER_RADIUS = 30.0f ;
1817 public float cornerRadius = DEFAULT_CORNER_RADIUS ;
1918
2019 private View targetView ;
2120 private int [] location = new int [2 ];
22-
2321 private Rect srcRect = new Rect ();
2422 private Bitmap cachedBitmap ;
2523 private Canvas cachedCanvas ;
@@ -41,42 +39,45 @@ public void setup() {
4139 }
4240
4341 public Bitmap getUpdatedBlurBitmap () {
44- // Kiểm tra an toàn Bitmap trước khi xử lý
4542 if (isPaused || blurBitmap == null || blurBitmap .isRecycled () ||
4643 targetView .getWidth () <= 0 || targetView .getHeight () <= 0 ) {
4744 return null ;
4845 }
4946
50- targetView .getLocationInWindow (location );
47+ // Lấy tọa độ tuyệt đối trên màn hình thay vì trong Window
48+ targetView .getLocationOnScreen (location );
5149
52- View rootView = targetView .getRootView ();
53- // Tính toán tỷ lệ dựa trên kích thước thực tế của bitmap (đã được scale nhỏ ở Controller)
54- float scaleX = (float ) blurBitmap .getWidth () / rootView .getWidth ();
55- float scaleY = (float ) blurBitmap .getHeight () / rootView .getHeight ();
50+ Context context = targetView .getContext ();
51+ DisplayMetrics dm = context .getResources ().getDisplayMetrics ();
52+
53+ // Tính tỷ lệ dựa trên kích thước thực tế của màn hình (Screen Metrics)
54+ // Điều này đảm bảo ảnh blur khớp 1:1 với Wallpaper gốc phía sau
55+ float scaleX = (float ) blurBitmap .getWidth () / dm .widthPixels ;
56+ float scaleY = (float ) blurBitmap .getHeight () / dm .heightPixels ;
5657
57- int x = (int ) (location [0 ] * scaleX );
58- int y = (int ) (location [1 ] * scaleY );
5958 int w = (int ) (targetView .getWidth () * scaleX );
6059 int h = (int ) (targetView .getHeight () * scaleY );
60+ int x = (int ) (location [0 ] * scaleX );
61+ int y = (int ) (location [1 ] * scaleY );
6162
62- // Giới hạn vùng cắt bên trong phạm vi Bitmap
63+ // Giới hạn vùng cắt để không bị OutOfBounds
6364 x = Math .max (0 , Math .min (x , blurBitmap .getWidth () - w ));
6465 y = Math .max (0 , Math .min (y , blurBitmap .getHeight () - h ));
6566
6667 if (w > 0 && h > 0 ) {
6768 try {
69+ // Chỉ khởi tạo lại cachedBitmap khi kích thước view thay đổi (rất quan trọng cho ScrollView)
6870 if (cachedBitmap == null || cachedBitmap .getWidth () != w || cachedBitmap .getHeight () != h ) {
69- // Giải phóng cached cũ nếu kích thước thay đổi
7071 if (cachedBitmap != null ) cachedBitmap .recycle ();
7172 cachedBitmap = Bitmap .createBitmap (w , h , Bitmap .Config .ARGB_8888 );
7273 cachedCanvas = new Canvas (cachedBitmap );
7374 }
7475
7576 srcRect .set (x , y , x + w , y + h );
7677 cachedCanvas .drawColor (0 , PorterDuff .Mode .CLEAR );
77-
7878 cachedCanvas .drawBitmap (blurBitmap , srcRect , new Rect (0 , 0 , w , h ), null );
7979 cachedCanvas .drawColor (getBlurTintColor ());
80+
8081 return cachedBitmap ;
8182 } catch (Exception e ) {
8283 return null ;
0 commit comments