gpt4 book ai didi

java - ConstraintLayout 指南百分比动画

转载 作者:太空狗 更新时间:2023-10-29 16:26:38 25 4
gpt4 key购买 nike

我对使用 ConstraintLayout (java) 还很陌生。

我想要实现的是当小键盘作为幻灯片动画显示/隐藏时,我尝试过这样的事情:

Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
ConstraintLayout.LayoutParams lparams = (ConstraintLayout.LayoutParams) guideline.getLayoutParams();
lparams.guidePercent = 0.5f;
guideline.setLayoutParams(lparams);
}
};
a.setDuration(3000);
a.setFillAfter(true);
guideline.startAnimation(a);

是的,指南(以及附加到它的相应 View )正在移动到屏幕的中心,但它并不像预期的那样平滑。有没有什么办法可以达到动画的流畅度?

如有任何建议,我们将不胜感激!

最佳答案

您可以使用ValueAnimator

Kotlin 示例。

    val guideline = findViewById<View>(R.id.guideline2) as Guideline
val end = (guideline.layoutParams as ConstraintLayout.LayoutParams).guidePercent
// get end percent. start at 0
val valueAnimator = ValueAnimator.ofFloat(0f, end)
valueAnimator.duration = 3000
// set duration
valueAnimator.interpolator = AccelerateDecelerateInterpolator()
// set interpolator and updateListener to get the animated value
valueAnimator.addUpdateListener { valueAnimator ->
val lp = guideline.layoutParams as ConstraintLayout.LayoutParams
// get the float value
lp.guidePercent = valueAnimator.animatedValue as Float
// update layout params
guideline.layoutParams = lp
}
valueAnimator.start()

Java 版本

 final Guideline guideline = findViewById(R.id.guideline2) ;
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams)guideline.getLayoutParams();
float end =lp.guidePercent;
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0f, end);
valueAnimator.setDuration(3000);
valueAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
ConstraintLayout.LayoutParams lp = (ConstraintLayout.LayoutParams)guideline.getLayoutParams();
lp.guidePercent = valueAnimator.getAnimatedFraction();
guideline.setLayoutParams(lp);

}
});

关于java - ConstraintLayout 指南百分比动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49378428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com