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 androidx.dynamicanimation.animation; 18 19 import android.util.FloatProperty; 20 21 import androidx.annotation.RequiresApi; 22 23 /** 24 * <p>FloatPropertyCompat is an abstraction that can be used to represent a mutable float value that 25 * is held in a host object. To access this float value, {@link #setValue(Object, float)} and getter 26 * {@link #getValue(Object)} need to be implemented. Both the setter and the getter take the 27 * primitive <code>float</code> type and avoids autoboxing and other overhead associated with the 28 * <code>Float</code> class. 29 * 30 * <p>For API 24 and later, {@link FloatProperty} instances can be converted to 31 * {@link FloatPropertyCompat} through 32 * {@link FloatPropertyCompat#createFloatPropertyCompat(FloatProperty)}. 33 * 34 * @param <T> the class on which the Property is declared 35 */ 36 public abstract class FloatPropertyCompat<T> { 37 final String mPropertyName; 38 39 /** 40 * A constructor that takes an identifying name. 41 */ FloatPropertyCompat(String name)42 public FloatPropertyCompat(String name) { 43 mPropertyName = name; 44 } 45 46 /** 47 * Create a {@link FloatPropertyCompat} wrapper for a {@link FloatProperty} object. The new 48 * {@link FloatPropertyCompat} instance will access and modify the property value of 49 * {@link FloatProperty} through the {@link FloatProperty} instance's setter and getter. 50 * 51 * @param property FloatProperty instance to be wrapped 52 * @param <T> the class on which the Property is declared 53 * @return a new {@link FloatPropertyCompat} wrapper for the given {@link FloatProperty} object 54 */ 55 @RequiresApi(24) createFloatPropertyCompat( final FloatProperty<T> property)56 public static <T> FloatPropertyCompat<T> createFloatPropertyCompat( 57 final FloatProperty<T> property) { 58 return new FloatPropertyCompat<T>(property.getName()) { 59 @Override 60 public float getValue(T object) { 61 return property.get(object); 62 } 63 64 @Override 65 public void setValue(T object, float value) { 66 property.setValue(object, value); 67 } 68 }; 69 } 70 71 /** 72 * Returns the current value that this property represents on the given <code>object</code>. 73 * 74 * @param object object which this property represents 75 * @return the current property value of the given object 76 */ 77 public abstract float getValue(T object); 78 79 /** 80 * Sets the value on <code>object</code> which this property represents. 81 * 82 * @param object object which this property represents 83 * @param value new value of the property 84 */ 85 public abstract void setValue(T object, float value); 86 } 87