1 /* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_ANIMATION_H 17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_ANIMATION_H 18 19 #include "core/animation/animation.h" 20 #include "core/animation/interpolator.h" 21 #include "core/animation/spring_motion.h" 22 23 namespace OHOS::Ace { 24 25 // Spring interpolator for elastic animation. Reference config:Mass(1.0) stiffness(100.0) damping(15.0). 26 class SpringAnimation : public Animation<float> { 27 DECLARE_ACE_TYPE(SpringAnimation, Animation); 28 29 public: 30 explicit SpringAnimation(const RefPtr<SpringProperty>& property); 31 SpringAnimation(const RefPtr<SpringProperty>& property, float velocity); 32 SpringAnimation(const RefPtr<SpringProperty>& property, float velocity, float valueThreshold); 33 ~SpringAnimation() override = default; 34 GetValue()35 const float& GetValue() const override 36 { 37 return currentPosition_; 38 } 39 40 private: 41 void SetEndPosition(float endPosition, float startVelocity); 42 43 void UpdatePosition(float normalized); 44 45 void InitEstimateDuration(); 46 OnNormalizedTimestampChanged(float normalized,bool reverse)47 void OnNormalizedTimestampChanged(float normalized, bool reverse) override 48 { 49 if (normalized < NORMALIZED_DURATION_MIN || normalized > NORMALIZED_DURATION_MAX) { 50 LOGE("normalized time check failed. normalized: %{public}f", normalized); 51 return; 52 } 53 UpdatePosition(normalized); 54 NotifyListener(currentPosition_); 55 } 56 57 RefPtr<Curve> GetCurve() override; 58 59 float estimateDuration_ = 1.0f; 60 float currentPosition_ = 0.0f; 61 float currentVelocity_ = 0.0f; 62 float valueThreshold_ = 0.001f; 63 float velocityThreshold_ = 0.001f; 64 float startPosition_ = 0.0f; 65 float endPosition_ = 0.0f; 66 RefPtr<SpringProperty> property_; // Contain: mass & stiffness & damping 67 RefPtr<SpringModel> solution_; // Maybe: CriticalDamped or Overdamped or Underdamped 68 }; 69 70 } // namespace OHOS::Ace 71 72 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_ANIMATION_SPRING_ANIMATION_H 73