1
+ package com .nativescript .material .core ;
2
+
3
+ import android .animation .StateListAnimator ;
4
+ import android .animation .AnimatorSet ;
5
+ import android .animation .ObjectAnimator ;
6
+ import android .view .View ;
7
+ import android .view .ViewGroup ;
8
+ import android .content .Context ;
9
+ import android .graphics .drawable .ShapeDrawable ;
10
+ import android .graphics .drawable .shapes .RoundRectShape ;
11
+ import android .graphics .drawable .RippleDrawable ;
12
+ import android .graphics .drawable .Drawable ;
13
+ import android .content .res .ColorStateList ;
14
+ import android .graphics .drawable .StateListDrawable ;
15
+ import android .graphics .Color ;
16
+ import android .os .Build ;
17
+
18
+ public class Utils {
19
+ static final int shortAnimTime = android .R .integer .config_shortAnimTime ;
20
+ static final int statePressed = android .R .attr .state_pressed ;
21
+ static final int stateEnabled = android .R .attr .state_enabled ;
22
+
23
+ public static void createStateListAnimator (Context context , View view , float elevation , float pressedZ ) {
24
+ int duration = context .getResources ().getInteger (shortAnimTime );
25
+
26
+ AnimatorSet pressedSet = new AnimatorSet ();
27
+ pressedSet .playTogether (ObjectAnimator .ofFloat (view , "translationZ" , pressedZ ).setDuration (duration ),
28
+ ObjectAnimator .ofFloat (view , "elevation" , elevation ).setDuration (0 ));
29
+
30
+ AnimatorSet notPressedSet = new AnimatorSet ();
31
+ notPressedSet .playTogether (ObjectAnimator .ofFloat (view , "translationZ" , 0 ).setDuration (duration ),
32
+ ObjectAnimator .ofFloat (view , "elevation" , elevation ).setDuration (0 ));
33
+
34
+ AnimatorSet defaultSet = new AnimatorSet ();
35
+ defaultSet .playTogether (ObjectAnimator .ofFloat (view , "translationZ" , 0 ).setDuration (0 ),
36
+ ObjectAnimator .ofFloat (view , "elevation" , 0 ).setDuration (0 ));
37
+
38
+ StateListAnimator stateListAnimator = new StateListAnimator ();
39
+ stateListAnimator .addState (new int [] { statePressed , stateEnabled }, pressedSet );
40
+ stateListAnimator .addState (new int [] { stateEnabled }, notPressedSet );
41
+ stateListAnimator .addState (new int [] {}, defaultSet );
42
+
43
+ view .setStateListAnimator (stateListAnimator );
44
+ }
45
+
46
+ public static ColorStateList getEnabledColorStateList (int color , String variant ) {
47
+ int [][] states = new int [][] { new int [] { -android .R .attr .state_enabled }, // enabled
48
+ android .util .StateSet .NOTHING , // disabled
49
+ };
50
+ int disabledColor = (variant == "text" || variant == "outline" ) ? 0 : Color .argb (0.117f , 0f , 0f , 0f );
51
+ int [] colors = new int [] { disabledColor , color };
52
+ return new android .content .res .ColorStateList (states , colors );
53
+ }
54
+
55
+ public static ColorStateList getFullColorStateList (int activeColor , int inactiveColor , int disabledColor ) {
56
+ int [][] states = new int [][] { new int [] { android .R .attr .state_focused }, // focused
57
+ android .util .StateSet .NOTHING , // other
58
+ new int [] { -android .R .attr .state_enabled } // disabled
59
+ };
60
+ int [] colors = new int [] { activeColor , inactiveColor , disabledColor };
61
+ return new android .content .res .ColorStateList (states , colors );
62
+ }
63
+
64
+ public static ShapeDrawable createForegroundShape (float radius ) {
65
+ RoundRectShape shape = new RoundRectShape (
66
+ new float [] { radius , radius , radius , radius , radius , radius , radius , radius }, null , null );
67
+ return new ShapeDrawable (shape );
68
+ }
69
+
70
+ public static Drawable createRippleDrawable (int rippleColor , float radius ) {
71
+ ShapeDrawable rippleShape = radius != 0 ? createForegroundShape (radius ) : null ;
72
+ if (Build .VERSION .SDK_INT >= 22 ) {
73
+ return new RippleDrawable (ColorStateList .valueOf (rippleColor ), null , rippleShape );
74
+ } else {
75
+ StateListDrawable rippleDrawable = new StateListDrawable ();
76
+ rippleShape .getPaint ().setColor (rippleColor );
77
+ rippleDrawable .addState (new int [] { statePressed }, rippleShape );
78
+ return rippleShape ;
79
+ }
80
+ }
81
+
82
+ static void handleClearFocus (View view ) {
83
+ final View root = view .getRootView ();
84
+ boolean oldValue = true ;
85
+ int oldDesc = ViewGroup .FOCUS_BEFORE_DESCENDANTS ;
86
+
87
+ if (root != null ) {
88
+ if (root instanceof ViewGroup ) {
89
+ oldDesc = ((ViewGroup ) root ).getDescendantFocusability ();
90
+ ((ViewGroup ) root ).setDescendantFocusability (ViewGroup .FOCUS_BLOCK_DESCENDANTS );
91
+ }
92
+ oldValue = root .isFocusable ();
93
+ root .setFocusable (false );
94
+ }
95
+ view .clearFocus ();
96
+ if (root != null ) {
97
+ root .setFocusable (oldValue );
98
+ if (root instanceof ViewGroup ) {
99
+ ((ViewGroup ) root ).setDescendantFocusability (oldDesc );
100
+ }
101
+ }
102
+ }
103
+ }
0 commit comments