Skip to content

Commit 430b941

Browse files
committed
Update
1 parent e44ece1 commit 430b941

3 files changed

Lines changed: 66 additions & 38 deletions

File tree

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

Lines changed: 51 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,44 @@
77
import android.graphics.BitmapFactory;
88
import android.graphics.drawable.BitmapDrawable;
99
import android.graphics.drawable.Drawable;
10+
import android.renderscript.Allocation;
11+
import android.renderscript.Element;
12+
import android.renderscript.RenderScript;
13+
import android.renderscript.ScriptIntrinsicBlur;
1014
import java.io.File;
1115
import java.lang.ref.WeakReference;
1216

1317
public class BlurController {
1418

15-
// Sử dụng biến static để ghi nhớ trạng thái file giữa các lần gọi
1619
private static long lastFileLength = -1;
1720
private static long lastFileModified = -1;
1821

22+
/**
23+
* Thuật toán làm mờ sử dụng RenderScript (Thay thế FastBlurUtility)
24+
*/
25+
private Bitmap blurBitmap(Context context, Bitmap bitmap, float radius) {
26+
if (bitmap == null) return null;
27+
28+
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
29+
RenderScript rs = RenderScript.create(context);
30+
31+
try {
32+
Allocation input = Allocation.createFromBitmap(rs, bitmap);
33+
Allocation output = Allocation.createFromBitmap(rs, outBitmap);
34+
35+
ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
36+
intrinsicBlur.setRadius(radius); // Giá trị tối đa là 25f
37+
intrinsicBlur.setInput(input);
38+
intrinsicBlur.forEach(output);
39+
40+
output.copyTo(outBitmap);
41+
} finally {
42+
rs.destroy();
43+
}
44+
45+
return outBitmap;
46+
}
47+
1948
public void captureAndBlur(Activity activity) {
2049
final WeakReference<Activity> activityRef = new WeakReference<>(activity);
2150

@@ -26,34 +55,30 @@ public void captureAndBlur(Activity activity) {
2655
Bitmap source = null;
2756
Context context = act.getApplicationContext();
2857

29-
// 1. Kiểm tra Wallpaper tùy chỉnh trong /files/home/etc/wallpaper.jpg
58+
// 1. Kiểm tra Wallpaper tùy chỉnh
3059
File customWallpaperFile = new File(act.getFilesDir(), "home/etc/wallpaper.jpg");
3160

3261
if (customWallpaperFile.exists()) {
3362
long currentLength = customWallpaperFile.length();
3463
long currentModified = customWallpaperFile.lastModified();
3564

36-
// KIỂM TRA THAY ĐỔI: Nếu dung lượng và thời gian cũ giống hệt thì thoát
3765
if (currentLength == lastFileLength && currentModified == lastFileModified) {
3866
if (BlurEngine.blurBitmap != null && !BlurEngine.blurBitmap.isRecycled()) {
39-
return; // Không có gì thay đổi, không cần làm mờ lại
67+
return;
4068
}
4169
}
4270

43-
// Cập nhật dấu vết mới
4471
lastFileLength = currentLength;
4572
lastFileModified = currentModified;
4673
source = BitmapFactory.decodeFile(customWallpaperFile.getAbsolutePath());
4774
} else {
48-
// Reset dấu vết nếu file custom bị xóa
4975
lastFileLength = -1;
5076
lastFileModified = -1;
5177
}
5278

53-
// 2. Lấy Wallpaper hệ thống nếu không có file custom
79+
// 2. Lấy Wallpaper hệ thống nếu cần
5480
if (source == null) {
5581
WallpaperManager wm = WallpaperManager.getInstance(context);
56-
// XÓA CACHE: Buộc hệ thống đọc lại ảnh nền mới nhất từ Launcher
5782
wm.forgetLoadedWallpaper();
5883

5984
if (wm.getWallpaperInfo() == null) {
@@ -64,24 +89,26 @@ public void captureAndBlur(Activity activity) {
6489
}
6590
}
6691

67-
// 3. Xử lý làm mờ
68-
Bitmap blurredResult;
92+
// 3. Xử lý làm mờ và tối ưu RAM
6993
if (source != null) {
70-
blurredResult = FastBlurUtility.startBlurBackground(source);
71-
} else {
72-
blurredResult = FastBlurUtility.getBlurBackgroundDrawer(act);
73-
}
74-
75-
if (blurredResult != null) {
76-
BlurEngine.blurBitmap = blurredResult;
77-
BlurEngine.isPaused = false;
94+
// Tối ưu: Giảm kích thước ảnh xuống 4 lần giúp giảm 16 lần lượng RAM tiêu thụ
95+
// và làm hiệu ứng mờ trông "mịn" hơn.
96+
int width = Math.max(source.getWidth() / 4, 1);
97+
int height = Math.max(source.getHeight() / 4, 1);
98+
Bitmap scaledSource = Bitmap.createScaledBitmap(source, width, height, false);
7899

79-
// Ép UI vẽ lại ngay lập tức
80-
act.runOnUiThread(() -> {
81-
if (act != null && !act.isFinishing() && act.getWindow() != null) {
82-
act.getWindow().getDecorView().invalidate();
83-
}
84-
});
100+
Bitmap blurredResult = blurBitmap(context, scaledSource, 15f);
101+
102+
if (blurredResult != null) {
103+
BlurEngine.blurBitmap = blurredResult;
104+
BlurEngine.isPaused = false;
105+
106+
act.runOnUiThread(() -> {
107+
if (act != null && !act.isFinishing() && act.getWindow() != null) {
108+
act.getWindow().getDecorView().invalidate();
109+
}
110+
});
111+
}
85112
}
86113
}).start();
87114
}

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
public final class BlurEngine {
1111
public static BlurController controller = new BlurController();
1212

13-
// CẬP NHẬT: volatile đảm bảo biến được cập nhật ngay lập tức giữa các luồng
13+
// volatile để đảm bảo tính nhất quán dữ liệu giữa Thread của Controller và UI Thread
1414
public static volatile Bitmap blurBitmap;
1515
public static boolean isPaused = true;
1616

@@ -41,13 +41,16 @@ public void setup() {
4141
}
4242

4343
public Bitmap getUpdatedBlurBitmap() {
44-
if (isPaused || blurBitmap == null || targetView.getWidth() <= 0 || targetView.getHeight() <= 0) {
44+
// Kiểm tra an toàn Bitmap trước khi xử lý
45+
if (isPaused || blurBitmap == null || blurBitmap.isRecycled() ||
46+
targetView.getWidth() <= 0 || targetView.getHeight() <= 0) {
4547
return null;
4648
}
4749

4850
targetView.getLocationInWindow(location);
4951

5052
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)
5154
float scaleX = (float) blurBitmap.getWidth() / rootView.getWidth();
5255
float scaleY = (float) blurBitmap.getHeight() / rootView.getHeight();
5356

@@ -56,26 +59,25 @@ public Bitmap getUpdatedBlurBitmap() {
5659
int w = (int) (targetView.getWidth() * scaleX);
5760
int h = (int) (targetView.getHeight() * scaleY);
5861

62+
// Giới hạn vùng cắt bên trong phạm vi Bitmap
5963
x = Math.max(0, Math.min(x, blurBitmap.getWidth() - w));
6064
y = Math.max(0, Math.min(y, blurBitmap.getHeight() - h));
6165

6266
if (w > 0 && h > 0) {
6367
try {
64-
if (blurBitmap == null || blurBitmap.isRecycled()) return null;
65-
6668
if (cachedBitmap == null || cachedBitmap.getWidth() != w || cachedBitmap.getHeight() != h) {
69+
// Giải phóng cached cũ nếu kích thước thay đổi
70+
if (cachedBitmap != null) cachedBitmap.recycle();
6771
cachedBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
6872
cachedCanvas = new Canvas(cachedBitmap);
6973
}
7074

7175
srcRect.set(x, y, x + w, y + h);
7276
cachedCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
7377

74-
if (!blurBitmap.isRecycled()) {
75-
cachedCanvas.drawBitmap(blurBitmap, srcRect, new Rect(0, 0, w, h), null);
76-
cachedCanvas.drawColor(getBlurTintColor());
77-
return cachedBitmap;
78-
}
78+
cachedCanvas.drawBitmap(blurBitmap, srcRect, new Rect(0, 0, w, h), null);
79+
cachedCanvas.drawColor(getBlurTintColor());
80+
return cachedBitmap;
7981
} catch (Exception e) {
8082
return null;
8183
}

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,12 +75,11 @@ private static Bitmap scaleAndDim(Bitmap bitmap, int targetW, int targetH) {
7575

7676
// Tạo bộ lọc màu để giảm độ sáng (contrast 0.85f ~ giảm 15% độ sáng)
7777
ColorMatrix cm = new ColorMatrix();
78-
float contrast = 1.05f;
79-
float brightness = 90f;
78+
float contrast = 0.95f;
8079
cm.set(new float[]{
81-
contrast, 0, 0, 0, brightness,
82-
0, contrast, 0, 0, brightness,
83-
0, 0, contrast, 0, brightness,
80+
contrast, 0, 0, 0, 0,
81+
0, contrast, 0, 0, 0,
82+
0, 0, contrast, 0, 0,
8483
0, 0, 0, 1, 0});
8584
paint.setColorFilter(new ColorMatrixColorFilter(cm));
8685

0 commit comments

Comments
 (0)