android - TranslateAnimation 无法正常工作
问题描述
部分代码可以正常工作。然而,在第二种情况下,动画发生了,但不是以适当的方式发生的。
应该滑动的视图会跳转到它们各自的位置(它们会转到正确的位置,但动画不正确)。
有没有人可以解决这种不良行为?
代码:
public void animateCollapse(){
final int count = getChildCount();
final int tWidth = getMeasuredWidth();
int widthSum = 0;
for (int i = 0; i < count; i++) {
final RelativeLayout child = (RelativeLayout) getChildAt(i);
widthSum += child.getWidth();
child.clearAnimation();
}
if(!mTagLayout.isCollapsed()){
//Removed piece of code, that works.
}
} else{
int currentPosX = 0;
if(tWidth> widthSum) return;
for (int i = 0; i < count; i++) {
final View child = getChildAt(i);
int lastpos = tWidth - child.getMeasuredWidth();
float value =currentPosX - child.getX();
TranslateAnimation anim = new TranslateAnimation(0, value, 0, 0);
anim.setDuration(1000);
anim.setAnimationListener(new TranslateAnimation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) { }
@Override
public void onAnimationRepeat(Animation animation) { }
@Override
public void onAnimationEnd(Animation animation)
{
isAnimating= false;
}
});
anim.setFillAfter(true);
child.startAnimation(anim);
currentPosX += child.getMeasuredWidth();
mTagLayout.setCollapsed(false);
}
}
@Edit 在动画期间视图不会被破坏和重建
解决方案
我通过使用反向插值器修复
if (mTagLayout.isCollapsed()) {
anim.setInterpolator(new ReverseInterpolator());
mTagLayout.setCollapsed(false);
} else {
mTagLayout.setCollapsed(true);
}
public class ReverseInterpolator implements Interpolator {
@Override
public float getInterpolation(float paramFloat) {
return Math.abs(paramFloat - 1f);
}
}
推荐阅读
- flutter - flutter_ffmpeg 包名
- vb.net - VB.net HTTP POST 请求
- javascript - CSS 过渡百分比(用于进度条)
- css - 在产品页面图像滑块上自动移动(滚动)产品缩略图
- python - 在 python 中使用 ghostscript 和 facturx 库创建符合 factur-x PDF/A-3 的 (Zugferd)
- vue.js - 关键依赖:依赖的请求是一个表达式,当使用 v-bind 时
- java - 在中断处理程序线程开始时运行任务
- delphi - 从 URL 中提取 URL 参数?在德尔福
- powerbi - 循环参考 Power BI
- tosca - Tosca 字符串操作模块不替换双引号