Parcourir la source

Implement collapsedTitleBarBackgroundColor style property (#786)

Animate between translucent and solid background in collapsed titlebar
Guy Carmeli il y a 7 ans
Parent
révision
eb8eb4be16

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/params/CollapsingTopBarParams.java Voir le fichier

@@ -7,7 +7,6 @@ import com.reactnativenavigation.views.collapsingToolbar.behaviours.CollapseBeha
7 7
 public class CollapsingTopBarParams {
8 8
     public @Nullable String imageUri;
9 9
     public @Nullable String reactViewId;
10
-    public int reactViewHeight;
11 10
     public StyleParams.Color scrimColor;
12 11
     public CollapseBehaviour collapseBehaviour;
13 12
     public boolean expendOnTopTabChange;

+ 0
- 1
android/app/src/main/java/com/reactnativenavigation/params/parsers/CollapsingTopBarParamsParser.java Voir le fichier

@@ -32,7 +32,6 @@ class CollapsingTopBarParamsParser extends Parser {
32 32
         CollapsingTopBarParams result = new CollapsingTopBarParams();
33 33
         result.imageUri = params.getString("collapsingToolBarImage", null);
34 34
         result.reactViewId = params.getString("collapsingToolBarComponent", null);
35
-        result.reactViewHeight = params.getInt("collapsingToolBarComponentHeight");
36 35
         result.expendOnTopTabChange = params.getBoolean("expendCollapsingToolBarOnTopTabChange");
37 36
         result.scrimColor = getColor(params, "collapsingToolBarCollapsedColor", new StyleParams.Color(Color.WHITE));
38 37
         result.showTitleWhenCollapsed = hasReactView;

+ 6
- 2
android/app/src/main/java/com/reactnativenavigation/views/TitleBar.java Voir le fichier

@@ -72,7 +72,7 @@ public class TitleBar extends Toolbar {
72 72
         setTitleTextColor(params);
73 73
         setSubtitleTextColor(params);
74 74
         colorOverflowButton(params);
75
-        setTranslucent(params);
75
+        setBackground(params);
76 76
     }
77 77
 
78 78
     private void colorOverflowButton(StyleParams params) {
@@ -82,7 +82,11 @@ public class TitleBar extends Toolbar {
82 82
         }
83 83
     }
84 84
 
85
-    private void setTranslucent(StyleParams params) {
85
+    protected void setBackground(StyleParams params) {
86
+        setTranslucent(params);
87
+    }
88
+
89
+    protected void setTranslucent(StyleParams params) {
86 90
         if (params.topBarTranslucent) {
87 91
             setBackground(new TranslucentTitleBarBackground());
88 92
         }

+ 37
- 0
android/app/src/main/java/com/reactnativenavigation/views/TranslucentAndSolidTitleBarBackground.java Voir le fichier

@@ -0,0 +1,37 @@
1
+package com.reactnativenavigation.views;
2
+
3
+import android.graphics.drawable.ColorDrawable;
4
+import android.graphics.drawable.Drawable;
5
+import android.graphics.drawable.TransitionDrawable;
6
+
7
+import com.reactnativenavigation.params.StyleParams;
8
+
9
+public class TranslucentAndSolidTitleBarBackground extends TransitionDrawable {
10
+    private static final int DURATION = 200;
11
+    private enum DrawableType {Translucent, Solid}
12
+
13
+    private DrawableType displayedDrawable = DrawableType.Translucent;
14
+
15
+    public TranslucentAndSolidTitleBarBackground(StyleParams.Color color) {
16
+        super(new Drawable[] {
17
+                new TranslucentTitleBarBackground(),
18
+                new ColorDrawable(color.getColor())
19
+        });
20
+    }
21
+
22
+    public void showTranslucentBackground() {
23
+        if (displayedDrawable == DrawableType.Translucent) {
24
+            return;
25
+        }
26
+        displayedDrawable = DrawableType.Translucent;
27
+        reverseTransition(DURATION);
28
+    }
29
+
30
+    public void showSolidBackground() {
31
+        if (displayedDrawable == DrawableType.Solid) {
32
+            return;
33
+        }
34
+        displayedDrawable = DrawableType.Solid;
35
+        startTransition(DURATION);
36
+    }
37
+}

+ 33
- 0
android/app/src/main/java/com/reactnativenavigation/views/collapsingToolbar/CollapsingTitleBar.java Voir le fichier

@@ -1,6 +1,7 @@
1 1
 package com.reactnativenavigation.views.collapsingToolbar;
2 2
 
3 3
 import android.content.Context;
4
+import android.support.annotation.Nullable;
4 5
 import android.view.MotionEvent;
5 6
 import android.view.View;
6 7
 
@@ -8,12 +9,14 @@ import com.reactnativenavigation.params.CollapsingTopBarParams;
8 9
 import com.reactnativenavigation.params.StyleParams;
9 10
 import com.reactnativenavigation.utils.ViewUtils;
10 11
 import com.reactnativenavigation.views.TitleBar;
12
+import com.reactnativenavigation.views.TranslucentAndSolidTitleBarBackground;
11 13
 
12 14
 public class CollapsingTitleBar extends TitleBar implements View.OnTouchListener {
13 15
     private CollapsingTextView title;
14 16
     private int collapsedHeight;
15 17
     private final ScrollListener scrollListener;
16 18
     private final CollapsingTopBarParams params;
19
+    private @Nullable TranslucentAndSolidTitleBarBackground titleBarBackground;
17 20
 
18 21
     public CollapsingTitleBar(Context context, int collapsedHeight, ScrollListener scrollListener, CollapsingTopBarParams params) {
19 22
         super(context);
@@ -39,6 +42,22 @@ public class CollapsingTitleBar extends TitleBar implements View.OnTouchListener
39 42
         }
40 43
     }
41 44
 
45
+    @Override
46
+    public void hideTitle() {
47
+        super.hideTitle();
48
+        if (titleBarBackground != null) {
49
+            titleBarBackground.showTranslucentBackground();
50
+        }
51
+    }
52
+
53
+    @Override
54
+    public void showTitle() {
55
+        super.showTitle();
56
+        if (titleBarBackground != null) {
57
+            titleBarBackground.showSolidBackground();
58
+        }
59
+    }
60
+
42 61
     private void addCollapsingTitle() {
43 62
         if (params.hasBackgroundImage()) {
44 63
             title = new CollapsingTextView(getContext(), collapsedHeight);
@@ -71,6 +90,20 @@ public class CollapsingTitleBar extends TitleBar implements View.OnTouchListener
71 90
         }
72 91
     }
73 92
 
93
+    @Override
94
+    protected void setBackground(StyleParams params) {
95
+        if (hasTranslucentAndSolidBackground(params)) {
96
+            titleBarBackground = new TranslucentAndSolidTitleBarBackground(params.collapsingTopBarParams.scrimColor);
97
+            setBackground(titleBarBackground);
98
+        } else {
99
+            setTranslucent(params);
100
+        }
101
+    }
102
+
103
+    private boolean hasTranslucentAndSolidBackground(StyleParams params) {
104
+        return params.topBarTranslucent && params.collapsingTopBarParams.scrimColor.hasColor();
105
+    }
106
+
74 107
     public void collapse(CollapseAmount amount) {
75 108
         if (amount.hasExactAmount()) {
76 109
             collapse(amount.get());