Android的 - 变化留有余量使用动画 [英] Android - Change left margin using animation
问题描述
我改变以下方式的图像视图的左边距:
ViewGroup.MarginLayoutParams的LayoutParams =(MarginLayoutParams)image.getLayoutParams();
layoutParams.leftMargin =价值;
image.setLayoutParams(的LayoutParams);
我想在利润率的变化运用动画。任何线索?
我的尝试:
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(形象,X,VALUE);
objectAnimator.start();
这完美的作品,作为图像移动到指定的X值的动画中,然而的值 layoutParams.leftMargin 保持不变!所以我不能使用这种方法,因为如果我尝试使用 objectAnimator 之后的 layoutParams.leftMargin 的值更改为100值100,应用的价值是不正确的(200加,而不是100,效果如果在 objectAnimator 仍然eventhough我设置左边距按以下方式:
layoutParams.leftMargin = 100;
使用动画类,而不是ObjectAnimator。
最终诠释newLeftMargin =<一些价值取代;
动画=新的动画(){
@覆盖
保护无效applyTransformation(浮动interpolatedTime,变换T){
的LayoutParams PARAMS = yourView.getLayoutParams();
params.leftMargin =(INT)(newLeftMargin * interpolatedTime);
yourView.setLayoutParams(PARAMS);
}
};
a.setDuration(500); //在毫秒
yourView.startAnimation(一);
请注意,您应该使用IE正确LayoutAnimation类,如果你的观点是那么的LinearLayout PARAMS的孩子应该LinearLayout.LayoutParams
I am changing the left margin of an image view in the following manner :
ViewGroup.MarginLayoutParams layoutParams = (MarginLayoutParams) image.getLayoutParams ();
layoutParams.leftMargin = VALUE;
image.setLayoutParams ( layoutParams );
I would like the change in margin to apply with animation. Any clues ?
What I tried :
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat ( image , "x" , VALUE);
objectAnimator.start();
This works perfectly, as the image is moved to the specified X value with animation, HOWEVER the value of layoutParams.leftMargin remains unchanged !! So I cannot use this method, because if I try to change the value of layoutParams.leftMargin to 100 after using the objectAnimator with the value 100, the value applied is not correct ( 200 is applied instead of 100, the effect if the objectAnimator remains eventhough I am setting the left margin in the following manner :
layoutParams.leftMargin = 100;
Use Animation class, not ObjectAnimator.
final int newLeftMargin = <some value>;
Animation a = new Animation() {
@Override
protected void applyTransformation(float interpolatedTime, Transformation t) {
LayoutParams params = yourView.getLayoutParams();
params.leftMargin = (int)(newLeftMargin * interpolatedTime);
yourView.setLayoutParams(params);
}
};
a.setDuration(500); // in ms
yourView.startAnimation(a);
Please note that you should use correct LayoutAnimation class i.e. if your view is the child of LinearLayout then params should be LinearLayout.LayoutParams
这篇关于Android的 - 变化留有余量使用动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!