代码:
RelativLayout.LayoutParams params = (RelativLayout.LayoutParams) view1.getLayoutParams();
params.setMargins(50, 0, 0, 0);
view1.setLayoutParams(params);
上面的代码工作正常,但我想对其进行动画处理。
代码:
RelativLayout.LayoutParams params = (RelativLayout.LayoutParams) view1.getLayoutParams();
params.setMargins(50, 0, 0, 0);
view1.setLayoutParams(params);
上面的代码工作正常,但我想对其进行动画处理。
你可以使用ValueAnimator
这样的:
ValueAnimator varl = ValueAnimator.ofInt(50);
varl.setDuration(4000);
varl.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view1.getLayoutParams();
lp.setMargins((Integer) animation.getAnimatedValue(), 0, 0, 0);
view1.setLayoutParams(lp);
}
});
varl.start();
可ValueAnimator
从 Honeycomb 获得,但您拥有NineOldAndroids
端口。
或者更好的是,使用动画:
Animation animation = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
LayoutParams params = view1.getLayoutParams();
params.leftMargin = (int)(50 * interpolatedTime);
view1.setLayoutParams(params);
}
};
animation.setDuration(300);
animation.setInterpolator(new OvershootInterpolator());
view1.startAnimation(animation);
或者更好的是,使用辅助库:
ViewPropertyObjectAnimator.animate(view1).leftMargin(50).setDuration(300).start();