Skip to content

Commit fc3c352

Browse files
committed
Update
1 parent 4eee92e commit fc3c352

2 files changed

Lines changed: 19 additions & 17 deletions

File tree

app/src/main/java/com/omarea/common/ui/BlurEngine.java

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,18 @@
66
import androidx.core.content.ContextCompat;
77
import com.tool.tree.R;
88
import android.content.Context;
9+
import android.util.DisplayMetrics;
910

1011
public 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;

app/src/main/java/com/omarea/common/ui/BlurPreDrawListener.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ public BlurPreDrawListener(BlurEngine engine, View view) {
1414

1515
@Override
1616
public boolean onPreDraw() {
17-
// Chỉ ra lệnh vẽ lại khi có dữ liệu bitmap và không bị tạm dừng
18-
if (!BlurEngine.isPaused && BlurEngine.blurBitmap != null && !BlurEngine.blurBitmap.isRecycled()) {
17+
// Kiểm tra hiển thị: Nếu View không hiển thị thì không cần phí tài nguyên vẽ
18+
if (targetView.isShown() && !BlurEngine.isPaused && BlurEngine.blurBitmap != null) {
19+
// Ép View vẽ lại liên tục để cập nhật vị trí Blur theo ScrollView
1920
targetView.invalidate();
2021
}
2122
return true;

0 commit comments

Comments
 (0)