1 /* 2 * Copyright (C) 2017 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.systemui.statusbar.notification; 18 19 import android.util.FloatProperty; 20 import android.util.Property; 21 import android.view.View; 22 23 import com.android.systemui.R; 24 25 import java.util.function.BiConsumer; 26 import java.util.function.Function; 27 28 /** 29 * An animatable property of a view. Used with {@link PropertyAnimator} 30 */ 31 public abstract class AnimatableProperty { 32 33 public static final AnimatableProperty X = AnimatableProperty.from(View.X, 34 R.id.x_animator_tag, R.id.x_animator_tag_start_value, R.id.x_animator_tag_end_value); 35 36 public static final AnimatableProperty Y = AnimatableProperty.from(View.Y, 37 R.id.y_animator_tag, R.id.y_animator_tag_start_value, R.id.y_animator_tag_end_value); 38 39 public static final AnimatableProperty TRANSLATION_X = AnimatableProperty.from( 40 View.TRANSLATION_X, R.id.x_animator_tag, R.id.x_animator_tag_start_value, 41 R.id.x_animator_tag_end_value); 42 43 public static final AnimatableProperty SCALE_X = AnimatableProperty.from( 44 View.SCALE_X, R.id.scale_x_animator_tag, R.id.scale_x_animator_start_value_tag, 45 R.id.scale_x_animator_end_value_tag); 46 47 public static final AnimatableProperty SCALE_Y = AnimatableProperty.from( 48 View.SCALE_Y, R.id.scale_y_animator_tag, R.id.scale_y_animator_start_value_tag, 49 R.id.scale_y_animator_end_value_tag); 50 51 /** 52 * Similar to X, however this doesn't allow for any other modifications other than from this 53 * property. When using X, it's possible that the view is laid out during the animation, 54 * which could break the continuity 55 */ 56 public static final AnimatableProperty ABSOLUTE_X = AnimatableProperty.from( 57 new FloatProperty<View>("ViewAbsoluteX") { 58 @Override 59 public void setValue(View view, float value) { 60 view.setTag(R.id.absolute_x_current_value, value); 61 View.X.set(view, value); 62 } 63 64 @Override 65 public Float get(View view) { 66 Object tag = view.getTag(R.id.absolute_x_current_value); 67 if (tag instanceof Float) { 68 return (Float) tag; 69 } 70 return View.X.get(view); 71 } 72 }, 73 R.id.absolute_x_animator_tag, 74 R.id.absolute_x_animator_start_tag, 75 R.id.absolute_x_animator_end_tag); 76 77 /** 78 * Similar to Y, however this doesn't allow for any other modifications other than from this 79 * property. When using X, it's possible that the view is laid out during the animation, 80 * which could break the continuity 81 */ 82 public static final AnimatableProperty ABSOLUTE_Y = AnimatableProperty.from( 83 new FloatProperty<View>("ViewAbsoluteY") { 84 @Override 85 public void setValue(View view, float value) { 86 view.setTag(R.id.absolute_y_current_value, value); 87 View.Y.set(view, value); 88 } 89 90 @Override 91 public Float get(View view) { 92 Object tag = view.getTag(R.id.absolute_y_current_value); 93 if (tag instanceof Float) { 94 return (Float) tag; 95 } 96 return View.Y.get(view); 97 } 98 }, 99 R.id.absolute_y_animator_tag, 100 R.id.absolute_y_animator_start_tag, 101 R.id.absolute_y_animator_end_tag); 102 103 public static final AnimatableProperty WIDTH = AnimatableProperty.from( 104 new FloatProperty<View>("ViewWidth") { 105 @Override 106 public void setValue(View view, float value) { 107 view.setTag(R.id.view_width_current_value, value); 108 view.setRight((int) (view.getLeft() + value)); 109 } 110 111 @Override 112 public Float get(View view) { 113 Object tag = view.getTag(R.id.view_width_current_value); 114 if (tag instanceof Float) { 115 return (Float) tag; 116 } 117 return (float) view.getWidth(); 118 } 119 }, 120 R.id.view_width_animator_tag, 121 R.id.view_width_animator_start_tag, 122 R.id.view_width_animator_end_tag); 123 124 public static final AnimatableProperty HEIGHT = AnimatableProperty.from( 125 new FloatProperty<View>("ViewHeight") { 126 @Override 127 public void setValue(View view, float value) { 128 view.setTag(R.id.view_height_current_value, value); 129 view.setBottom((int) (view.getTop() + value)); 130 } 131 132 @Override 133 public Float get(View view) { 134 Object tag = view.getTag(R.id.view_height_current_value); 135 if (tag instanceof Float) { 136 return (Float) tag; 137 } 138 return (float) view.getHeight(); 139 } 140 }, 141 R.id.view_height_animator_tag, 142 R.id.view_height_animator_start_tag, 143 R.id.view_height_animator_end_tag); 144 getAnimationStartTag()145 public abstract int getAnimationStartTag(); 146 getAnimationEndTag()147 public abstract int getAnimationEndTag(); 148 getAnimatorTag()149 public abstract int getAnimatorTag(); 150 getProperty()151 public abstract Property getProperty(); 152 from(String name, BiConsumer<T, Float> setter, Function<T, Float> getter, int animatorTag, int startValueTag, int endValueTag)153 public static <T extends View> AnimatableProperty from(String name, BiConsumer<T, Float> setter, 154 Function<T, Float> getter, int animatorTag, int startValueTag, int endValueTag) { 155 Property<T, Float> property = new FloatProperty<T>(name) { 156 157 @Override 158 public Float get(T object) { 159 return getter.apply(object); 160 } 161 162 @Override 163 public void setValue(T object, float value) { 164 setter.accept(object, value); 165 } 166 }; 167 return new AnimatableProperty() { 168 @Override 169 public int getAnimationStartTag() { 170 return startValueTag; 171 } 172 173 @Override 174 public int getAnimationEndTag() { 175 return endValueTag; 176 } 177 178 @Override 179 public int getAnimatorTag() { 180 return animatorTag; 181 } 182 183 @Override 184 public Property getProperty() { 185 return property; 186 } 187 }; 188 } 189 190 public static <T extends View> AnimatableProperty from(Property<T, Float> property, 191 int animatorTag, int startValueTag, int endValueTag) { 192 return new AnimatableProperty() { 193 @Override 194 public int getAnimationStartTag() { 195 return startValueTag; 196 } 197 198 @Override 199 public int getAnimationEndTag() { 200 return endValueTag; 201 } 202 203 @Override 204 public int getAnimatorTag() { 205 return animatorTag; 206 } 207 208 @Override 209 public Property getProperty() { 210 return property; 211 } 212 }; 213 } 214 } 215