From 1093ceb659eeeb5c9aaf6307749e64627b40b3d3 Mon Sep 17 00:00:00 2001 From: Joshua Tsuji Date: Tue, 14 Jan 2020 16:48:45 -0500 Subject: [PATCH] Add FloatProperties, which contains helpful properties for animating Rects. Test: atest SystemUITests Change-Id: I0f6584e1898b75c19e2e27ba2915dcdff42f419b --- .../util/animation/FloatProperties.kt | 107 ++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 packages/SystemUI/src/com/android/systemui/util/animation/FloatProperties.kt diff --git a/packages/SystemUI/src/com/android/systemui/util/animation/FloatProperties.kt b/packages/SystemUI/src/com/android/systemui/util/animation/FloatProperties.kt new file mode 100644 index 0000000000000..ecd3afd687b36 --- /dev/null +++ b/packages/SystemUI/src/com/android/systemui/util/animation/FloatProperties.kt @@ -0,0 +1,107 @@ +/* + * Copyright (C) 2020 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.systemui.util.animation + +import android.graphics.Rect +import android.graphics.RectF +import androidx.dynamicanimation.animation.FloatPropertyCompat + +/** + * Helpful extra properties to use with the [PhysicsAnimator]. These allow you to animate objects + * such as [Rect] and [RectF]. + * + * There are additional, more basic properties available in [DynamicAnimation]. + */ +class FloatProperties { + companion object { + /** + * Represents the x-coordinate of a [Rect]. Typically used to animate moving a Rect + * horizontally. + * + * This property's getter returns [Rect.left], and its setter uses [Rect.offsetTo], which + * sets [Rect.left] to the new value and offsets [Rect.right] so that the width of the Rect + * does not change. + */ + @JvmField + val RECT_X = object : FloatPropertyCompat("RectX") { + override fun setValue(rect: Rect?, value: Float) { + rect?.offsetTo(value.toInt(), rect.top) + } + + override fun getValue(rect: Rect?): Float { + return rect?.left?.toFloat() ?: -Float.MAX_VALUE + } + } + + /** + * Represents the y-coordinate of a [Rect]. Typically used to animate moving a Rect + * vertically. + * + * This property's getter returns [Rect.top], and its setter uses [Rect.offsetTo], which + * sets [Rect.top] to the new value and offsets [Rect.bottom] so that the height of the Rect + * does not change. + */ + @JvmField + val RECT_Y = object : FloatPropertyCompat("RectY") { + override fun setValue(rect: Rect?, value: Float) { + rect?.offsetTo(rect.left, value.toInt()) + } + + override fun getValue(rect: Rect?): Float { + return rect?.top?.toFloat() ?: -Float.MAX_VALUE + } + } + + /** + * Represents the x-coordinate of a [RectF]. Typically used to animate moving a RectF + * horizontally. + * + * This property's getter returns [RectF.left], and its setter uses [RectF.offsetTo], which + * sets [RectF.left] to the new value and offsets [RectF.right] so that the width of the + * RectF does not change. + */ + @JvmField + val RECTF_X = object : FloatPropertyCompat("RectFX") { + override fun setValue(rect: RectF?, value: Float) { + rect?.offsetTo(value, rect.top) + } + + override fun getValue(rect: RectF?): Float { + return rect?.left ?: -Float.MAX_VALUE + } + } + + /** + * Represents the y-coordinate of a [RectF]. Typically used to animate moving a RectF + * vertically. + * + * This property's getter returns [RectF.top], and its setter uses [RectF.offsetTo], which + * sets [RectF.top] to the new value and offsets [RectF.bottom] so that the height of the + * RectF does not change. + */ + @JvmField + val RECTF_Y = object : FloatPropertyCompat("RectFY") { + override fun setValue(rect: RectF?, value: Float) { + rect?.offsetTo(rect.left, value) + } + + override fun getValue(rect: RectF?): Float { + return rect?.top ?: -Float.MAX_VALUE + } + } + } +} \ No newline at end of file