取消switch按钮获焦失焦时动效

Signed-off-by: zhangwenbin <zhangwenbin35@h-partners.com>
This commit is contained in:
zhangwenbin 2024-09-26 15:26:03 +08:00
parent a358c5af96
commit cd3af57584
3 changed files with 41 additions and 12 deletions

View File

@ -80,13 +80,15 @@ public:
auto offsetIsRtl = GreatOrEqual(actualSize_.Width(), actualSize_.Height())
? (isSelect_->Get() ? 0.0f : actualSize_.Width() - actualSize_.Height())
: (isSelect_->Get() ? actualTrackRadius_ : actualSize_.Width() - actualTrackRadius_);
if (isCancelAnimation_ && !isFocusOrBlur_) {
AnimationOption colorOption = AnimationOption();
colorOption.SetDuration(colorAnimationDuration_);
colorOption.SetCurve(Curves::FAST_OUT_SLOW_IN);
AnimationUtils::Animate(colorOption, [&]() {
animatableBoardColor_->Set(isSelect_->Get() ? LinearColor(userActiveColor_) : LinearColor(inactiveColor_));
animatableBoardColor_->Set(isSelect_->Get() ?
LinearColor(userActiveColor_) : LinearColor(inactiveColor_));
});
}
AnimationOption pointOption = AnimationOption();
pointOption.SetDuration(pointAnimationDuration_);
pointOption.SetCurve(Curves::FAST_OUT_SLOW_IN);
@ -119,6 +121,13 @@ public:
}
}
void SetBoardColor(Color color)
{
if (animatableBoardColor_) {
animatableBoardColor_->Set(LinearColor(color));
}
}
void InitializeParam();
void PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset, const SizeF& contentSize);
float GetSwitchWidth(const SizeF& contentSize) const;
@ -278,6 +287,11 @@ public:
actualTrackRadius_ = borderRadius;
}
void SetIsFocusOrBlur(bool isFocusOrBlur)
{
isFocusOrBlur_ = isFocusOrBlur;
}
private:
float actualWidth_ = 0.0f;
float actualHeight_ = 0.0f;
@ -294,6 +308,7 @@ private:
Dimension hoverRadius_ = 8.0_vp;
Dimension focusRadius_ = 8.0_vp;
bool isUseDiffPointColor_ = false;
bool isCancelAnimation_ = false;
float hoverDuration_ = 0.0f;
float hoverToTouchDuration_ = 0.0f;
float touchDuration_ = 0.0f;
@ -302,6 +317,7 @@ private:
bool isDragEvent_ = false;
bool isFirstCreated_ = true;
bool showHoverEffect_ = true;
bool isFocusOrBlur_ = false;
float actualTrackRadius_ = 0.0f;
OffsetF hotZoneOffset_;

View File

@ -93,6 +93,7 @@ void SwitchModifier::InitializeParam()
pointColorUnchecked_ = switchTheme->GetPointColorUnchecked();
isUseDiffPointColor_ = switchTheme->GetSwitchUseDiffPointColor();
focusBoardColor_ = switchTheme->GetFocusBoardColor();
isCancelAnimation_ = isUseDiffPointColor_;
}
float SwitchModifier::CalcActualWidth(float width, float height, double actualGap, double defaultWidthGap)
@ -143,9 +144,9 @@ void SwitchModifier::PaintSwitch(RSCanvas& canvas, const OffsetF& contentOffset,
float boardIdealHeight =
(switchTheme->GetFocusBoardHeight() - switchTheme->GetHotZoneVerticalPadding() * NUM_TWO).ConvertToPx();
if (animateTrackRadius_->Get() < 0 && idealHeight != 0) {
focusRadius_ = focusRadius_ * height / idealHeight;
focusRadius_ = focusRadius_ * height / idealHeight + switchTheme->GetFocusPaintPadding();
} else if (boardIdealHeight != 0) {
focusRadius_ = focusRadius_ * height / boardIdealHeight;
focusRadius_ = focusRadius_ * height / boardIdealHeight + switchTheme->GetFocusPaintPadding();
}
OffsetF hoverBoardOffset;
@ -163,9 +164,8 @@ void SwitchModifier::DrawRectCircle(RSCanvas& canvas, const OffsetF& contentOffs
auto xOffset = contentOffset.GetX();
auto yOffset = contentOffset.GetY();
auto height = contentSize.Height();
auto radius = height / NUM_TWO;
auto trackRadius =
(animateTrackRadius_->Get() < 0) ? radius : animateTrackRadius_->Get();
(animateTrackRadius_->Get() < 0) ? height / NUM_TWO : animateTrackRadius_->Get();
RSRect rect = RSRect(xOffset, yOffset, xOffset + contentSize.Width(), yOffset + height);
RSRoundRect roundRect(rect, trackRadius, trackRadius);
@ -175,6 +175,7 @@ void SwitchModifier::DrawRectCircle(RSCanvas& canvas, const OffsetF& contentOffs
ToRSColor(animatableBoardColor_->Get().BlendOpacity(static_cast<float>(DISABLED_ALPHA) / ENABLED_ALPHA)));
} else {
brush.SetColor(ToRSColor(animatableBoardColor_->Get()));
SetIsFocusOrBlur(false);
}
brush.SetBlendMode(RSBlendMode::SRC_OVER);
brush.SetAntiAlias(true);
@ -208,7 +209,7 @@ void SwitchModifier::DrawRectCircle(RSCanvas& canvas, const OffsetF& contentOffs
} else {
point.SetX(xOffset + pointOffset_->Get());
}
point.SetY(yOffset + radius);
point.SetY(yOffset + height / NUM_TWO);
canvas.DrawCircle(point, pointRadius_);
canvas.DetachBrush();
}

View File

@ -198,6 +198,18 @@ void SwitchPattern::OnIsFocusActiveUpdate(bool isFocusAcitve)
CHECK_NULL_VOID(switchModifier);
switchModifier->SetIsFocused(isFocusAcitve);
isFocus_ = isFocusAcitve;
CHECK_NULL_VOID(pipeline_);
auto switchTheme = pipeline_->GetTheme<SwitchTheme>();
CHECK_NULL_VOID(switchTheme);
Color color;
if (isFocus_) {
color = isOn_.value_or(false) ? switchTheme->GetActiveColor() : switchTheme->GetFocusedBGColorUnselected();
} else {
color = isOn_.value_or(false) ? switchTheme->GetActiveColor() : switchTheme->GetInactiveColor();
}
switchModifier->SetIsFocusOrBlur(true);
switchModifier->SetBoardColor(color);
}
void SwitchPattern::UpdateSwitchPaintProperty()