setLayoutParams() 动画持续时间不起作用
setLayoutParams() animation duration doesnt work
我有 LayoutParams
过渡 addRule()
。
我的观点改变了位置,但持续时间是瞬间的。
我在 API 15 上工作,所以我不能使用 beginDelayedTransition()
。
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(layoutFalse.getWidth(), layoutFalse.getHeight());
positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
positionRules.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutFalse.requestLayout();
layoutFalse.setLayoutParams(positionRules);
}
};
a.setDuration(3000);
layoutFalse.startAnimation(a);
这里,
layoutFalse.requestLayout();
layoutFalse.setLayoutParams(positionRules);
你的顺序错了。因为你应该先设置 layoutParams
然后在你的 view
.
上调用 requestLayout
应该是这样的
layoutFalse.setLayoutParams(positionRules);
layoutFalse.requestLayout();
applyTransformation()
为每个动画帧调用,转换应使用 interpolatedTime
参数来计算该帧的转换。您的 applyTransformation()
无条件应用更改,因此所有帧的结果都相同。
如果您只是想在指定时间后更改布局,请使用 layoutFalse.postDelayed()
到 post 和 Runnable
延迟后应用更改。
在此块中,您不执行任何动画操作。
你只要说:
让这个对象在每一帧里都有我想要的约束。没有了。
所以,我认为,如果你想为某些东西设置动画,你必须像这样使用 interpolatedTime:
在没有动画的 RelativeLayout 中设置视图的位置:
public void setTopLeftCornerF(float leftMargin, float topMargin) {
if (this.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.getLayoutParams();
params.width = (int)_width;//w();
params.height = (int)_height;//h();
params.setMargins((int)leftMargin, (int)topMargin, (int)-leftMargin, (int)-topMargin);
this.setLayoutParams(params);
}
}
在某处使用动画:
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
float posX = (float) (mFromX + ((mToX - mFromX) * interpolatedTime));
float posY = (float) (mFromY + ((mToY - mFromY) * interpolatedTime));
t.getMatrix().setTranslate(posX,posY);
}
或按照说明使用TranslateAnimation
我有 LayoutParams
过渡 addRule()
。
我的观点改变了位置,但持续时间是瞬间的。
我在 API 15 上工作,所以我不能使用 beginDelayedTransition()
。
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
final RelativeLayout.LayoutParams positionRules = new RelativeLayout.LayoutParams(layoutFalse.getWidth(), layoutFalse.getHeight());
positionRules.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
positionRules.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
layoutFalse.requestLayout();
layoutFalse.setLayoutParams(positionRules);
}
};
a.setDuration(3000);
layoutFalse.startAnimation(a);
这里,
layoutFalse.requestLayout();
layoutFalse.setLayoutParams(positionRules);
你的顺序错了。因为你应该先设置 layoutParams
然后在你的 view
.
requestLayout
应该是这样的
layoutFalse.setLayoutParams(positionRules);
layoutFalse.requestLayout();
applyTransformation()
为每个动画帧调用,转换应使用 interpolatedTime
参数来计算该帧的转换。您的 applyTransformation()
无条件应用更改,因此所有帧的结果都相同。
如果您只是想在指定时间后更改布局,请使用 layoutFalse.postDelayed()
到 post 和 Runnable
延迟后应用更改。
在此块中,您不执行任何动画操作。 你只要说:
让这个对象在每一帧里都有我想要的约束。没有了。
所以,我认为,如果你想为某些东西设置动画,你必须像这样使用 interpolatedTime: 在没有动画的 RelativeLayout 中设置视图的位置:
public void setTopLeftCornerF(float leftMargin, float topMargin) {
if (this.getLayoutParams() instanceof RelativeLayout.LayoutParams) {
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)this.getLayoutParams();
params.width = (int)_width;//w();
params.height = (int)_height;//h();
params.setMargins((int)leftMargin, (int)topMargin, (int)-leftMargin, (int)-topMargin);
this.setLayoutParams(params);
}
}
在某处使用动画:
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
// TODO Auto-generated method stub
super.applyTransformation(interpolatedTime, t);
float posX = (float) (mFromX + ((mToX - mFromX) * interpolatedTime));
float posY = (float) (mFromY + ((mToY - mFromY) * interpolatedTime));
t.getMatrix().setTranslate(posX,posY);
}
或按照说明使用TranslateAnimation