Skip to content

Commit 2c75024

Browse files
committed
Update
1 parent 86558fb commit 2c75024

2 files changed

Lines changed: 42 additions & 33 deletions

File tree

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

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -43,42 +43,51 @@ public Bitmap getUpdatedBlurBitmap() {
4343
targetView.getWidth() <= 0 || targetView.getHeight() <= 0) {
4444
return null;
4545
}
46-
47-
// Lấy RootView thực sự (thường là DecorView của Window)
46+
4847
View rootView = targetView.getRootView();
4948
if (rootView == null) return null;
49+
5050
targetView.getLocationOnScreen(location);
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-
}
70-
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;
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);
7960
}
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());
86+
87+
return cachedBitmap;
88+
} catch (Exception e) {
89+
return null;
8090
}
81-
return null;
8291
}
8392

8493
private int getBlurTintColor() {

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

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

1515
@Override
1616
public boolean onPreDraw() {
17-
// CẬP NHẬT: Không còn phụ thuộc vào BlurEngine.blurBitmap tĩnh
18-
if (targetView.isShown() && !BlurEngine.isPaused) {
17+
// Chỉ vẽ lại khi View đang hiển thị trên màn hình (tránh lãng phí tài nguyên cho các phần bị khuất trong ScrollView)
18+
if (targetView.isShown() && !BlurEngine.isPaused && BlurEngine.blurBitmap != null) {
1919
targetView.invalidate();
2020
}
2121
return true;

0 commit comments

Comments
 (0)