Skip to content

Commit b451c26

Browse files
committed
Update
1 parent 7707ac3 commit b451c26

1 file changed

Lines changed: 45 additions & 31 deletions

File tree

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

Lines changed: 45 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
import android.content.Context;
66
import android.graphics.Bitmap;
77
import android.graphics.BitmapFactory;
8+
import android.graphics.Canvas;
9+
import android.graphics.ColorMatrix;
10+
import android.graphics.ColorMatrixColorFilter;
11+
import android.graphics.Paint;
812
import android.graphics.drawable.BitmapDrawable;
913
import android.graphics.drawable.Drawable;
1014
import android.renderscript.Allocation;
@@ -18,30 +22,46 @@ public class BlurController {
1822

1923
private static long lastFileLength = -1;
2024
private static long lastFileModified = -1;
21-
25+
2226
/**
23-
* Thuật toán làm mờ sử dụng RenderScript (Thay thế FastBlurUtility)
27+
* Tăng độ sáng cho Bitmap sử dụng ColorMatrix
28+
* @param brightness: 0 là bình thường, dương là sáng hơn (ví dụ: 40f)
2429
*/
25-
private Bitmap blurBitmap(Context context, Bitmap bitmap, float radius) {
30+
private Bitmap adjustBrightness(Bitmap bitmap, float brightness) {
2631
if (bitmap == null) return null;
2732

33+
Bitmap newBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
34+
Canvas canvas = new Canvas(newBitmap);
35+
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
36+
37+
// Ma trận điều chỉnh độ sáng: Cộng thêm giá trị vào các kênh R, G, B
38+
ColorMatrix cm = new ColorMatrix(new float[] {
39+
1, 0, 0, 0, brightness,
40+
0, 1, 0, 0, brightness,
41+
0, 0, 1, 0, brightness,
42+
0, 0, 0, 1, 0
43+
});
44+
45+
paint.setColorFilter(new ColorMatrixColorFilter(cm));
46+
canvas.drawBitmap(bitmap, 0, 0, paint);
47+
return newBitmap;
48+
}
49+
50+
private Bitmap blurBitmap(Context context, Bitmap bitmap, float radius) {
51+
if (bitmap == null) return null;
2852
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), bitmap.getConfig());
2953
RenderScript rs = RenderScript.create(context);
30-
3154
try {
3255
Allocation input = Allocation.createFromBitmap(rs, bitmap);
3356
Allocation output = Allocation.createFromBitmap(rs, outBitmap);
34-
3557
ScriptIntrinsicBlur intrinsicBlur = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
36-
intrinsicBlur.setRadius(radius); // Giá trị tối đa là 25f
58+
intrinsicBlur.setRadius(radius);
3759
intrinsicBlur.setInput(input);
3860
intrinsicBlur.forEach(output);
39-
4061
output.copyTo(outBitmap);
4162
} finally {
4263
rs.destroy();
4364
}
44-
4565
return outBitmap;
4666
}
4767

@@ -55,48 +75,39 @@ public void captureAndBlur(Activity activity) {
5575
Bitmap source = null;
5676
Context context = act.getApplicationContext();
5777

58-
// 1. Kiểm tra Wallpaper tùy chỉnh
78+
// 1. Lấy Wallpaper (Tùy chỉnh hoặc Hệ thống)
5979
File customWallpaperFile = new File(act.getFilesDir(), "home/etc/wallpaper.jpg");
60-
6180
if (customWallpaperFile.exists()) {
6281
long currentLength = customWallpaperFile.length();
6382
long currentModified = customWallpaperFile.lastModified();
6483

6584
if (currentLength == lastFileLength && currentModified == lastFileModified) {
66-
if (BlurEngine.blurBitmap != null && !BlurEngine.blurBitmap.isRecycled()) {
67-
return;
68-
}
85+
if (BlurEngine.blurBitmap != null && !BlurEngine.blurBitmap.isRecycled()) return;
6986
}
7087

7188
lastFileLength = currentLength;
7289
lastFileModified = currentModified;
7390
source = BitmapFactory.decodeFile(customWallpaperFile.getAbsolutePath());
7491
} else {
75-
lastFileLength = -1;
76-
lastFileModified = -1;
77-
}
78-
79-
// 2. Lấy Wallpaper hệ thống nếu cần
80-
if (source == null) {
8192
WallpaperManager wm = WallpaperManager.getInstance(context);
8293
wm.forgetLoadedWallpaper();
83-
84-
if (wm.getWallpaperInfo() == null) {
85-
Drawable drawable = wm.getDrawable();
86-
if (drawable instanceof BitmapDrawable) {
87-
source = ((BitmapDrawable) drawable).getBitmap();
88-
}
94+
Drawable drawable = wm.getDrawable();
95+
if (drawable instanceof BitmapDrawable) {
96+
source = ((BitmapDrawable) drawable).getBitmap();
8997
}
9098
}
9199

92-
// 3. Xử lý làm mờtối ưu RAM
100+
// 2. Xử lý làm sánglàm mờ
93101
if (source != null) {
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);
102+
// Bước A: Tăng độ sáng (Cộng 45 đơn vị để lớp mờ trông rực rỡ hơn)
103+
Bitmap brightSource = adjustBrightness(source, 45f);
104+
105+
// Bước B: Scale nhỏ để tối ưu RAM
106+
int width = Math.max(brightSource.getWidth() / 4, 1);
107+
int height = Math.max(brightSource.getHeight() / 4, 1);
108+
Bitmap scaledSource = Bitmap.createScaledBitmap(brightSource, width, height, false);
99109

110+
// Bước C: Blur bằng RenderScript
100111
Bitmap blurredResult = blurBitmap(context, scaledSource, 15f);
101112

102113
if (blurredResult != null) {
@@ -109,6 +120,9 @@ public void captureAndBlur(Activity activity) {
109120
}
110121
});
111122
}
123+
124+
// Giải phóng bitmap tạm để tránh tràn RAM
125+
if (brightSource != source) brightSource.recycle();
112126
}
113127
}).start();
114128
}

0 commit comments

Comments
 (0)