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 #include "core/components/list/watch_interactive_effect.h" 17 18 #include "core/components/list/render_list_item.h" 19 20 namespace OHOS::Ace { 21 BuildClickAnimation()22void WatchInteractiveEffect::BuildClickAnimation() 23 { 24 if (!theme_) { 25 LOGE("theme is invalid, stop build animation"); 26 return; 27 } 28 29 if (!NeedClickAnimation()) { 30 return; 31 } 32 33 RefPtr<KeyframeAnimation<double>> scaleAnimation = AceType::MakeRefPtr<KeyframeAnimation<double>>(); 34 RefPtr<KeyframeAnimation<double>> alphaAnimation = AceType::MakeRefPtr<KeyframeAnimation<double>>(); 35 BuildClickScaleAnimation(scaleAnimation); 36 BuildClickAlphaAnimation(alphaAnimation); 37 if (controller_) { 38 controller_->ClearInterpolators(); 39 controller_->AddInterpolator(scaleAnimation); 40 controller_->AddInterpolator(alphaAnimation); 41 controller_->SetDuration(clickDuration_); 42 controller_->Play(); 43 } 44 } 45 BuildClickScaleAnimation(const RefPtr<KeyframeAnimation<double>> & scaleAnimation)46void WatchInteractiveEffect::BuildClickScaleAnimation(const RefPtr<KeyframeAnimation<double>>& scaleAnimation) 47 { 48 auto scaleFrameStart = AceType::MakeRefPtr<Keyframe<double>>(ANIMATION_ZERO_TIME, scaleBegin_); 49 auto scaleFrameMid = AceType::MakeRefPtr<Keyframe<double>>(ANIMATION_HALF_TIME, scaleEnd_); 50 auto scaleFrameEnd = AceType::MakeRefPtr<Keyframe<double>>(ANIMATION_END_TIME, scaleBegin_); 51 scaleFrameMid->SetCurve(Curves::FRICTION); 52 scaleFrameEnd->SetCurve(Curves::FRICTION); 53 54 scaleAnimation->AddKeyframe(scaleFrameStart); 55 scaleAnimation->AddKeyframe(scaleFrameMid); 56 scaleAnimation->AddKeyframe(scaleFrameEnd); 57 scaleAnimation->AddListener([weakEffect = AceType::WeakClaim(this)](double value) { 58 auto effect = weakEffect.Upgrade(); 59 if (effect) { 60 effect->SetScale(value); 61 effect->MarkItemRender(); 62 } 63 }); 64 } 65 66 } // namespace OHOS::Ace 67