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.animation.Animator; 20 import android.animation.ObjectAnimator; 21 import android.util.FloatProperty; 22 import android.view.View; 23 24 import com.android.launcher3.anim.AlphaUpdateListener; 25 26 import java.util.Arrays; 27 import java.util.function.Consumer; 28 29 /** 30 * Utility class to handle separating a single value as a factor of multiple values 31 */ 32 public class MultiValueAlpha { 33 34 public static final FloatProperty<AlphaProperty> VALUE = 35 new FloatProperty<AlphaProperty>("value") { 36 37 @Override 38 public Float get(AlphaProperty alphaProperty) { 39 return alphaProperty.mValue; 40 } 41 42 @Override 43 public void setValue(AlphaProperty object, float value) { 44 object.setValue(value); 45 } 46 }; 47 48 private final View mView; 49 private final AlphaProperty[] mMyProperties; 50 51 private int mValidMask; 52 // Whether we should change from INVISIBLE to VISIBLE and vice versa at low alpha values. 53 private boolean mUpdateVisibility; 54 MultiValueAlpha(View view, int size)55 public MultiValueAlpha(View view, int size) { 56 mView = view; 57 mMyProperties = new AlphaProperty[size]; 58 59 mValidMask = 0; 60 for (int i = 0; i < size; i++) { 61 int myMask = 1 << i; 62 mValidMask |= myMask; 63 mMyProperties[i] = new AlphaProperty(myMask); 64 } 65 } 66 67 @Override toString()68 public String toString() { 69 return Arrays.toString(mMyProperties); 70 } 71 getProperty(int index)72 public AlphaProperty getProperty(int index) { 73 return mMyProperties[index]; 74 } 75 76 /** Sets whether we should update between INVISIBLE and VISIBLE based on alpha. */ setUpdateVisibility(boolean updateVisibility)77 public void setUpdateVisibility(boolean updateVisibility) { 78 mUpdateVisibility = updateVisibility; 79 } 80 81 public class AlphaProperty { 82 83 private final int mMyMask; 84 85 private float mValue = 1; 86 // Factor of all other alpha channels, only valid if mMyMask is present in mValidMask. 87 private float mOthers = 1; 88 89 private Consumer<Float> mConsumer; 90 AlphaProperty(int myMask)91 AlphaProperty(int myMask) { 92 mMyMask = myMask; 93 } 94 setValue(float value)95 public void setValue(float value) { 96 if (mValue == value) { 97 return; 98 } 99 100 if ((mValidMask & mMyMask) == 0) { 101 // Our cache value is not correct, recompute it. 102 mOthers = 1; 103 for (AlphaProperty prop : mMyProperties) { 104 if (prop != this) { 105 mOthers *= prop.mValue; 106 } 107 } 108 } 109 110 // Since we have changed our value, all other caches except our own need to be 111 // recomputed. Change mValidMask to indicate the new valid caches (only our own). 112 mValidMask = mMyMask; 113 mValue = value; 114 115 final float alpha = mOthers * mValue; 116 mView.setAlpha(alpha); 117 if (mUpdateVisibility) { 118 AlphaUpdateListener.updateVisibility(mView); 119 } 120 if (mConsumer != null) { 121 mConsumer.accept(mValue); 122 } 123 } 124 getValue()125 public float getValue() { 126 return mValue; 127 } 128 setConsumer(Consumer<Float> consumer)129 public void setConsumer(Consumer<Float> consumer) { 130 mConsumer = consumer; 131 if (mConsumer != null) { 132 mConsumer.accept(mValue); 133 } 134 } 135 136 @Override toString()137 public String toString() { 138 return Float.toString(mValue); 139 } 140 141 /** 142 * Creates and returns an Animator from the current value to the given value. Future 143 * animator on the same target automatically cancels the previous one. 144 */ animateToValue(float value)145 public Animator animateToValue(float value) { 146 ObjectAnimator animator = ObjectAnimator.ofFloat(this, VALUE, value); 147 animator.setAutoCancel(true); 148 return animator; 149 } 150 } 151 } 152