代码:

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端口。

10-06 09:01