1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.support.design.widget;
18 
19 import android.animation.AnimatorInflater;
20 import android.animation.ObjectAnimator;
21 import android.animation.StateListAnimator;
22 import android.content.Context;
23 import android.content.res.TypedArray;
24 import android.support.design.R;
25 import android.util.AttributeSet;
26 import android.view.View;
27 import android.view.ViewOutlineProvider;
28 
29 class ViewUtilsLollipop {
30 
31     private static final int[] STATE_LIST_ANIM_ATTRS = new int[] {android.R.attr.stateListAnimator};
32 
setBoundsViewOutlineProvider(View view)33     static void setBoundsViewOutlineProvider(View view) {
34         view.setOutlineProvider(ViewOutlineProvider.BOUNDS);
35     }
36 
setStateListAnimatorFromAttrs(View view, AttributeSet attrs, int defStyleAttr, int defStyleRes)37     static void setStateListAnimatorFromAttrs(View view, AttributeSet attrs,
38            int defStyleAttr,  int defStyleRes) {
39         final Context context = view.getContext();
40         final TypedArray a = context.obtainStyledAttributes(attrs, STATE_LIST_ANIM_ATTRS,
41                 defStyleAttr, defStyleRes);
42         try {
43             if (a.hasValue(0)) {
44                 StateListAnimator sla = AnimatorInflater.loadStateListAnimator(context,
45                         a.getResourceId(0, 0));
46                 view.setStateListAnimator(sla);
47             }
48         } finally {
49             a.recycle();
50         }
51     }
52 
53     /**
54      * Creates and sets a {@link StateListAnimator} with a custom elevation value
55      */
setDefaultAppBarLayoutStateListAnimator(final View view, final float targetElevation)56     static void setDefaultAppBarLayoutStateListAnimator(final View view,
57             final float targetElevation) {
58         final StateListAnimator sla = new StateListAnimator();
59 
60         // Enabled, collapsible and collapsed == elevated
61         sla.addState(new int[]{android.R.attr.enabled, R.attr.state_collapsible,
62                         R.attr.state_collapsed},
63                 ObjectAnimator.ofFloat(view, "elevation", targetElevation));
64 
65         // Enabled and collapsible, but not collapsed != elevated
66         sla.addState(new int[]{android.R.attr.enabled, R.attr.state_collapsible,
67                         -R.attr.state_collapsed},
68                 ObjectAnimator.ofFloat(view, "elevation", 0f));
69 
70         // Enabled but not collapsible == elevated
71         sla.addState(new int[]{android.R.attr.enabled, -R.attr.state_collapsible},
72                 ObjectAnimator.ofFloat(view, "elevation", targetElevation));
73 
74         // Default, none elevated state
75         sla.addState(new int[0], ObjectAnimator.ofFloat(view, "elevation", 0));
76 
77         view.setStateListAnimator(sla);
78     }
79 
80 }
81