44import android .graphics .Bitmap ;
55import android .graphics .Canvas ;
66import android .graphics .Rect ;
7+ import android .graphics .RectF ;
78import android .util .AttributeSet ;
89import android .widget .LinearLayout ;
910
1011public class BlurViewLinearLayout extends LinearLayout {
1112 protected BlurEngine engine ;
12- private android . graphics . RectF strokeRect = new android . graphics . RectF ();
13+ private RectF strokeRect = new RectF ();
1314 private Rect srcRect = new Rect ();
1415 private Rect dstRect = new Rect ();
1516
1617 public BlurViewLinearLayout (Context context , AttributeSet attrs ) {
1718 super (context , attrs );
1819 this .engine = new BlurEngine (this );
20+ // Quan trọng: Phải set false để hệ thống gọi hàm onDraw của ViewGroup
1921 setWillNotDraw (false );
2022 }
2123
24+ public BlurEngine getEngine () {
25+ return engine ;
26+ }
27+
2228 @ Override
2329 protected void onAttachedToWindow () {
2430 super .onAttachedToWindow ();
@@ -27,25 +33,36 @@ protected void onAttachedToWindow() {
2733
2834 @ Override
2935 protected void onDraw (Canvas canvas ) {
36+ // 1. Vẽ lớp kính mờ (Blur) ngay tại tọa độ hiện tại
3037 if (!BlurEngine .isPaused ) {
31- // Lấy trực tiếp bitmap mờ từ Engine (đã bao gồm capture & blur)
38+ // Lấy bitmap đã được engine cắt và nhuộm màu sẵn
3239 Bitmap blurFragment = engine .getUpdatedBlurBitmap ();
3340
3441 if (blurFragment != null && !blurFragment .isRecycled ()) {
42+ // Thiết lập vùng nguồn (toàn bộ bitmap đã cắt)
3543 srcRect .set (0 , 0 , blurFragment .getWidth (), blurFragment .getHeight ());
44+ // Thiết lập vùng đích (khớp hoàn toàn với kích thước View hiện tại)
3645 dstRect .set (0 , 0 , getWidth (), getHeight ());
46+
47+ // Vẽ trực tiếp lên canvas trước khi vẽ các View con
3748 canvas .drawBitmap (blurFragment , srcRect , dstRect , null );
3849 }
3950 }
4051
52+ // 2. Vẽ nội dung của View (super.onDraw sẽ vẽ TabLayout, chữ, icon...)
4153 super .onDraw (canvas );
54+
55+ // 3. Vẽ viền (Stroke) lên trên cùng
4256 drawStroke (canvas );
4357 }
4458
4559 protected void drawStroke (Canvas canvas ) {
60+ // Sử dụng paint tĩnh từ engine để tối ưu
4661 android .graphics .Paint paint = BlurEngine .getStrokePaint (getContext ());
4762 float radius = engine .cornerRadius ;
4863 float strokeWidth = paint .getStrokeWidth ();
64+
65+ // Inset nửa độ dày viền để nét vẽ nằm trọn bên trong biên View
4966 float inset = strokeWidth / 2f ;
5067 strokeRect .set (inset , inset , getWidth () - inset , getHeight () - inset );
5168
@@ -60,6 +77,9 @@ protected void drawStroke(Canvas canvas) {
6077 @ Override
6178 protected void onDetachedFromWindow () {
6279 super .onDetachedFromWindow ();
63- if (engine != null ) engine .destroy ();
80+ // Dọn dẹp bộ đệm bitmap để tránh rò rỉ bộ nhớ
81+ if (engine != null ) {
82+ engine .destroy ();
83+ }
6484 }
6585}
0 commit comments