gpt4 book ai didi

java - Android布局上的折叠动画

转载 作者:行者123 更新时间:2023-11-29 09:14:29 24 4
gpt4 key购买 nike

我遇到了这个问题,我有一个类似 Accordion 的 Activity ,带有根据其状态展开或折叠布局的按钮,然后问题出在动画中,展开动画非常完美,但折叠布局时折叠动画它通过父按钮上方,我想折叠它相对于布局。

对不起,我的英语不好,这是代码:

public class Animations extends Animation {

/**
* Initializes expand collapse animation, has two types, collapse (1) and expand (0).
* @param view The view to animate
* @param duration
* @param type The type of animation: 0 will expand from gone and 0 size to visible and layout size defined in xml.
* 1 will collapse view and set to gone
*/
public AnimationSet AnimationSet;

public Animations(String type) {
if (type == "Expand")
AnimationSet = ExpandAnimation();
if (type == "Collapse")
AnimationSet = CollapseAnimation();
}
public AnimationSet ExpandAnimation() {

AnimationSet _set = new AnimationSet(true);

Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(250);
_set.addAnimation(animation);

animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f, Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(150);
_set.addAnimation(animation);

return _set;

}
public AnimationSet CollapseAnimation() {

AnimationSet set = new AnimationSet(true);

Animation animation = new AlphaAnimation(1.0f, 1.0f);
animation.setDuration(250);
set.addAnimation(animation);

animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, -1.0f
);
animation.setDuration(250);
set.addAnimation(animation);

return set;

}

最佳答案

为什么不直接使用 ReverseInterpolator 来反转已经非常完美的展开动画?

public class ReverseInterpolator implements Interpolator {
public float getInterpolation(float paramFloat) {
return Math.abs(paramFloat -1f);
}
}

在您的 _set 变量上:

_set.setInterpolator(new ReverseInterpolator());

public AnimationSet CollapseAnimation() {
AnimationSet set = someObj.ExpandAnimation();
set.setInterpolator(new ReverseInterpolator());
return set;
}

我还要强调一些您做错的事情:

使用.equals 来比较字符串而不是==。这是错误的 -> if (type == "Expand")。此外,正确命名您的方法和变量,例如,

public AnimationSet AnimationSet; //poor
public AnimationSet expandAnim; //better
public AnimationSet ExpandAnimation() { //poor
public AnimationSet expand() { //better

关于java - Android布局上的折叠动画,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10352818/

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