77import android .graphics .BitmapFactory ;
88import android .graphics .drawable .BitmapDrawable ;
99import android .graphics .drawable .Drawable ;
10+ import android .renderscript .Allocation ;
11+ import android .renderscript .Element ;
12+ import android .renderscript .RenderScript ;
13+ import android .renderscript .ScriptIntrinsicBlur ;
1014import java .io .File ;
1115import java .lang .ref .WeakReference ;
1216
1317public 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 }
0 commit comments