1.如何正确使用平移动画
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
mView.startAnimation(translate);
问题:当动画结束后,View会跳回到原始位置。
改进:
AnimationSet set = new AnimationSet(true);
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
set.addAnimation(translate);
set.setFillAfter(true);
mView.startAnimation(set);
setFillAfter文档说明:
If fillAfter is true, the transformation that this animation performed
will persist when it is finished. Defaults to false if not set.
设为true之后,界面会停留在动画播放完时的界面。
问题:动画结束后界面显示正确,但是View上各控件的实际位置和看上去的位置不对应,
实际位置还在View的原始位置,因此button的点击位置会有问题,和看见的位置有偏差。
正确方法:
AnimationSet set = new AnimationSet(true);
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0.5, Animation.RELATIVE_TO_SELF, 0);
set.addAnimation(translate);
set.setFillAfter(true);
mView.offsetTopAndBottom(-mView.getHeight() / 2);
mView.startAnimation(set);
先将View向上平移自身高度一半的距离,然后播放动画,从最初位置一直向上移动目标位置。
setFillBefore文档说明:
If fillBefore is true, this animation will apply its transformation
before the start time of the animation. Defaults to true if
setFillEnabled(boolean) is not set to true.
对TranslateAnimation,setFillBefore默认为true,也就是说在动画开始前,先将transformation
apply到View,这也就是为什么offsetTopAndBottom()后,View依然从原始位置开始运动。
如果setFillBefore设为false,动画播放时会有一个跳动,可以看到View从目标位置跳到原始位置。
总结:
使用Animation、AnimationSet框架实现的动画效果,必须先将View放置到最终的目标位置,
然后倒过来,播放从原始位置到目标位置的动画。
2.WPF 利用后台C#代码实现一个平移动画
这个简单
Storyboard s = new Storyboard();
d = new ();
d.KeyFrames.Add(new LinearDoubleKeyFrame(30,KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.6))));
d.KeyFrames.Add(new LinearDoubleKeyFrame(变动终值, KeyTime.FromTimeSpan(TimeSpan.FromSeconds(0.8))));
s.Children.Add(d);
Storyboard.SetTargetName(d, 目标名);
Storyboard.SetTargetProperty(d, new PropertyPath(Canvas.LeftProperty));
s.Begin(bor);
中间那个d是加了关键帧了,我就不摘出来了,不喜欢关键帧去点就行了
3.如何正确使用平移动画
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
mView.startAnimation(translate);
问题:当动画结束后,View会跳回到原始位置。
改进:
AnimationSet set = new AnimationSet(true);
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);
set.addAnimation(translate);
set.setFillAfter(true);
mView.startAnimation(set);
setFillAfter文档说明:
If fillAfter is true, the transformation that this animation performed
will persist when it is finished. Defaults to false if not set.
设为true之后,界面会停留在动画播放完时的界面。
问题:动画结束后界面显示正确,但是View上各控件的实际位置和看上去的位置不对应,
实际位置还在View的原始位置,因此button的点击位置会有问题,和看见的位置有偏差。
正确方法:
AnimationSet set = new AnimationSet(true);
TranslateAnimation translate = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, 0.5, Animation.RELATIVE_TO_SELF, 0);
set.addAnimation(translate);
set.setFillAfter(true);
mView.offsetTopAndBottom(-mView.getHeight() / 2);
mView.startAnimation(set);
先将View向上平移自身高度一半的距离,然后播放动画,从最初位置一直向上移动目标位置。
setFillBefore文档说明:
If fillBefore is true, this animation will apply its transformation
before the start time of the animation. Defaults to true if
setFillEnabled(boolean) is not set to true.
对TranslateAnimation,setFillBefore默认为true,也就是说在动画开始前,先将transformation
apply到View,这也就是为什么offsetTopAndBottom()后,View依然从原始位置开始运动。
如果setFillBefore设为false,动画播放时会有一个跳动,可以看到View从目标位置跳到原始位置。
总结:
使用Animation、AnimationSet框架实现的动画效果,必须先将View放置到最终的目标位置,
然后倒过来,播放从原始位置到目标位置的动画。
4.如何正确使用平移动画(关于fillBefore和fillAfter的一点说明)
如何实现将View向上平移自身高度一半的距离? TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);mView.startAnimation(translate);问题:当动画结束后,View会跳回到原始位置。
改进:AnimationSet set = new AnimationSet(true);TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5f);set.addAnimation(translate);set.setFillAfter(true);mView.startAnimation(set); setFillAfter文档说明:If fillAfter is true, the transformation that this animation performed will persist when it is finished. Defaults to false if not set.设为true之后,界面会停留在动画播放完时的界面。 问题:动画结束后界面显示正确,但是View上各控件的实际位置和看上去的位置不对应,实际位置还在View的原始位置,因此button的点击位置会有问题,和看见的位置有偏差。
正确方法:AnimationSet set = new AnimationSet(true);TranslateAnimation translate = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0.5, Animation.RELATIVE_TO_SELF, 0);set.addAnimation(translate);set.setFillAfter(true);mView.offsetTopAndBottom(-mView.getHeight() / 2);mView.startAnimation(set); 先将View向上平移自身高度一半的距离,然后播放动画,从最初位置一直向上移动目标位置。 setFillBefore文档说明:If fillBefore is true, this animation will apply its transformation before the start time of the animation. Defaults to true if setFillEnabled(boolean) is not set to true. 对TranslateAnimation,setFillBefore默认为true,也就是说在动画开始前,先将transformation apply到View,这也就是为什么offsetTopAndBottom()后,View依然从原始位置开始运动。
如果setFillBefore设为false,动画播放时会有一个跳动,可以看到View从目标位置跳到原始位置。 总结:使用Animation、AnimationSet框架实现的动画效果,必须先将View放置到最终的目标位置,然后倒过来,播放从原始位置到目标位置的动画。
5.怎么设置 imageview 平移动画循环
代码如下
[UIView animateWithDuration:3 delay:0.0 options: animations:^{
[imageview setTransform:((-600,0))];
[imageview setAlpha:0];
[imageview :0];
}
];
仅供参考。