1
1
using System . Collections . Generic ;
2
2
using Android . App ;
3
3
using Android . Content . PM ;
4
- using Android . Graphics . Drawables ;
5
4
using Android . OS ;
6
5
using Android . Views ;
7
6
using Android . Widget ;
8
7
using Java . Lang ;
9
8
using Com . EightbitLab . BlurViewBinding ;
10
- using AndroidX . ViewPager . Widget ;
11
- using AndroidX . AppCompat . App ;
12
- using Google . Android . Material . Tabs ;
13
- using AndroidX . Fragment . App ;
14
- //using Com.EightbitLab.SupportRenderScriptBlurBinding;
15
-
16
- // Ported from:
17
- // https://github.com/Dimezis/BlurView/blob/b5f6f414ae16885acb709fe5a65d24df05c4c62a/app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java
9
+ using AndroidX . ViewPager . Widget ;
10
+ using AndroidX . AppCompat . App ;
11
+ using Google . Android . Material . Tabs ;
12
+ using AndroidX . Fragment . App ;
13
+ using Math = Java . Lang . Math ;
14
+ using String = Java . Lang . String ;
15
+
16
+ //using Com.EightbitLab.SupportRenderScriptBlurBinding;
17
+
18
+ // Ported from:
19
+ // https://github.com/Dimezis/BlurView/blob/b5f6f414ae16885acb709fe5a65d24df05c4c62a/app/src/main/java/com/eightbitlab/blurview_sample/MainActivity.java
18
20
namespace SampleApp
19
21
{
20
22
[ Activity (
@@ -25,73 +27,88 @@ namespace SampleApp
25
27
Theme = "@style/Theme.MyTheme" ) ]
26
28
public class MainActivity : AppCompatActivity
27
29
{
28
- ViewGroup root ;
29
- ViewPager viewPager ;
30
- SeekBar radiusSeekBar ;
31
- BlurView topBlurView ;
32
- BlurView bottomBlurView ;
33
- TabLayout tabLayout ;
30
+ private ViewPager viewPager ;
31
+ private TabLayout tabLayout ;
32
+ private BlurView bottomBlurView ;
33
+ private BlurView topBlurView ;
34
+ private SeekBar radiusSeekBar ;
35
+ private ViewGroup root ;
34
36
35
37
protected override void OnCreate ( Bundle savedInstanceState )
36
38
{
37
39
base . OnCreate ( savedInstanceState ) ;
38
40
SetContentView ( Resource . Layout . activity_main ) ;
41
+ InitView ( ) ;
42
+ SetupBlurView ( ) ;
43
+ SetupViewPager ( ) ;
44
+ }
39
45
40
- root = FindViewById < ViewGroup > ( Resource . Id . root ) ;
46
+ private void InitView ( )
47
+ {
41
48
viewPager = FindViewById < ViewPager > ( Resource . Id . viewPager ) ;
42
- radiusSeekBar = FindViewById < SeekBar > ( Resource . Id . radiusSeekBar ) ;
43
- topBlurView = FindViewById < BlurView > ( Resource . Id . topBlurView ) ;
44
- bottomBlurView = FindViewById < BlurView > ( Resource . Id . bottomBlurView ) ;
45
49
tabLayout = FindViewById < TabLayout > ( Resource . Id . tabLayout ) ;
50
+ bottomBlurView = FindViewById < BlurView > ( Resource . Id . bottomBlurView ) ;
51
+ topBlurView = FindViewById < BlurView > ( Resource . Id . topBlurView ) ;
52
+ radiusSeekBar = FindViewById < SeekBar > ( Resource . Id . radiusSeekBar ) ;
53
+ root = FindViewById < ViewGroup > ( Resource . Id . root ) ;
54
+ }
55
+
56
+ private void SetupViewPager ( )
57
+ {
58
+ viewPager . OffscreenPageLimit = 2 ;
59
+ viewPager . Adapter = new ViewPagerAdapter ( SupportFragmentManager ) ;
60
+ tabLayout . SetupWithViewPager ( viewPager ) ;
61
+ }
46
62
47
- SetupBlurView ( ) ;
48
- SetupViewPager ( ) ;
49
- }
50
-
51
63
void SetupBlurView ( )
52
64
{
53
- float radius = 25f ;
54
- float minBlurRadius = 10f ;
55
- float step = 4f ;
65
+ const float radius = 25f ;
66
+ const float minBlurRadius = 4f ;
67
+ const float step = 4f ;
56
68
57
69
//set background, if your root layout doesn't have one
58
- Drawable windowBackground = Window . DecorView . Background ;
59
-
60
- var topViewSettings = topBlurView . SetupWith ( root )
70
+ var windowBackground = Window ? . DecorView . Background ;
71
+ var algorithm = GetBlurAlgorithm ( ) ;
72
+
73
+ var topViewSettings = topBlurView . SetupWith ( root , algorithm )
61
74
. SetFrameClearDrawable ( windowBackground )
62
- . SetBlurAlgorithm ( new RenderScriptBlur ( this ) ) // SupportRenderScriptBlur
63
- . SetBlurRadius ( radius )
64
- . SetHasFixedTransformationMatrix ( true ) ;
75
+ . SetBlurRadius ( radius ) ;
65
76
66
- var bottomViewSettings = bottomBlurView . SetupWith ( root )
77
+ var bottomViewSettings = bottomBlurView . SetupWith ( root , algorithm )
67
78
. SetFrameClearDrawable ( windowBackground )
68
- . SetBlurAlgorithm ( new RenderScriptBlur ( this ) ) // SupportRenderScriptBlur
69
- . SetBlurRadius ( radius )
70
- . SetHasFixedTransformationMatrix ( true ) ;
79
+ . SetBlurRadius ( radius ) ;
71
80
72
- int initialProgress = ( int ) ( radius * step ) ;
81
+ var initialProgress = ( int ) ( radius * step ) ;
73
82
radiusSeekBar . Progress = initialProgress ;
74
83
75
84
radiusSeekBar . ProgressChanged += ( sender , args ) =>
76
85
{
77
- float blurRadius = args . Progress / step ;
86
+ var blurRadius = args . Progress / step ;
78
87
blurRadius = Math . Max ( blurRadius , minBlurRadius ) ;
79
88
topViewSettings . SetBlurRadius ( blurRadius ) ;
80
89
bottomViewSettings . SetBlurRadius ( blurRadius ) ;
81
90
} ;
82
91
}
83
92
84
- void SetupViewPager ( )
93
+ private IBlurAlgorithm GetBlurAlgorithm ( )
85
94
{
86
- viewPager . OffscreenPageLimit = 2 ;
87
- viewPager . Adapter = new ViewPagerAdapter ( SupportFragmentManager ) ;
88
- tabLayout . SetupWithViewPager ( viewPager ) ;
95
+ IBlurAlgorithm algorithm ;
96
+ if ( Build . VERSION . SdkInt > BuildVersionCodes . R )
97
+ {
98
+ algorithm = new RenderEffectBlur ( ) ;
99
+ }
100
+ else
101
+ {
102
+ algorithm = new RenderScriptBlur ( this ) ;
103
+ }
104
+
105
+ return algorithm ;
89
106
}
90
107
}
91
108
92
109
class ViewPagerAdapter : FragmentPagerAdapter
93
110
{
94
- List < BaseFragment > pages ;
111
+ private readonly List < BaseFragment > pages ;
95
112
96
113
public ViewPagerAdapter ( AndroidX . Fragment . App . FragmentManager fragmentManager )
97
114
: base ( fragmentManager , BehaviorResumeOnlyCurrentFragment )
@@ -113,8 +130,7 @@ public override AndroidX.Fragment.App.Fragment GetItem(int position)
113
130
114
131
public override ICharSequence GetPageTitleFormatted ( int position )
115
132
{
116
- return new Java . Lang . String ( pages [ position ] . Title ) ;
133
+ return new String ( pages [ position ] . Title ) ;
117
134
}
118
135
}
119
136
}
120
-
0 commit comments