• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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 com.android.launcher3.util;
18 
19 import android.util.Property;
20 import android.view.View;
21 
22 /**
23  * Utility class to handle separating a single value as a factor of multiple values
24  */
25 public class MultiValueAlpha {
26 
27     public static final Property<AlphaProperty, Float> VALUE =
28             new Property<AlphaProperty, Float>(Float.TYPE, "value") {
29 
30                 @Override
31                 public Float get(AlphaProperty alphaProperty) {
32                     return alphaProperty.mValue;
33                 }
34 
35                 @Override
36                 public void set(AlphaProperty object, Float value) {
37                     object.setValue(value);
38                 }
39             };
40 
41     private final View mView;
42     private final AlphaProperty[] mMyProperties;
43 
44     private int mValidMask;
45 
MultiValueAlpha(View view, int size)46     public MultiValueAlpha(View view, int size) {
47         mView = view;
48         mMyProperties = new AlphaProperty[size];
49 
50         mValidMask = 0;
51         for (int i = 0; i < size; i++) {
52             int myMask = 1 << i;
53             mValidMask |= myMask;
54             mMyProperties[i] = new AlphaProperty(myMask);
55         }
56     }
57 
getProperty(int index)58     public AlphaProperty getProperty(int index) {
59         return mMyProperties[index];
60     }
61 
62     public class AlphaProperty {
63 
64         private final int mMyMask;
65 
66         private float mValue = 1;
67         // Factor of all other alpha channels, only valid if mMyMask is present in mValidMask.
68         private float mOthers = 1;
69 
AlphaProperty(int myMask)70         AlphaProperty(int myMask) {
71             mMyMask = myMask;
72         }
73 
setValue(float value)74         public void setValue(float value) {
75             if (mValue == value) {
76                 return;
77             }
78 
79             if ((mValidMask & mMyMask) == 0) {
80                 // Our cache value is not correct, recompute it.
81                 mOthers = 1;
82                 for (AlphaProperty prop : mMyProperties) {
83                     if (prop != this) {
84                         mOthers *= prop.mValue;
85                     }
86                 }
87             }
88 
89             // Since we have changed our value, all other caches except our own need to be
90             // recomputed. Change mValidMask to indicate the new valid caches (only our own).
91             mValidMask = mMyMask;
92             mValue = value;
93 
94             mView.setAlpha(mOthers * mValue);
95         }
96 
getValue()97         public float getValue() {
98             return mValue;
99         }
100     }
101 }
102