Android 反转动画
我有一个ImageView,当它被添加到布局中时,它会被设置动画。 当它被移除时,我想反转相同的动画Android 反转动画,android,animation,Android,Animation,我有一个ImageView,当它被添加到布局中时,它会被设置动画。 当它被移除时,我想反转相同的动画 有没有一种方法可以在android中反转动画,而无需重新编码和反转参数?没有,很遗憾,您无法使用动画对象进行反转。 但可以使用将反转动画的插值器进行模拟: package com.example.android; import android.view.animation.Interpolator; public class ReverseInterpolator implements In
有没有一种方法可以在android中反转动画,而无需重新编码和反转参数?没有,很遗憾,您无法使用动画对象进行反转。 但可以使用将反转动画的插值器进行模拟:
package com.example.android;
import android.view.animation.Interpolator;
public class ReverseInterpolator implements Interpolator {
@Override
public float getInterpolation(float paramFloat) {
return Math.abs(paramFloat -1f);
}
}
然后,可以在动画中设置新的插值器:
myAnimation.setInterpolator(new ReverseInterpolator());
我对PCAN有类似的方法,但略有不同。它使用一个
插值器
,并将有效地传递出与正常使用传入的插值器
然后在反向模式下相同的值。你不用再考虑动画的错误实现了。相反,整个Android系统都是。参见代码
公共类反转插值器实现插值器{
专用最终插值器mInterpolator;
公共反向内插器(内插器){
mInterpolator=插值器;
}
@凌驾
公共浮点输入(浮点输入){
返回mInterpolator.getInterpolation(反向输入(输入));
}
/**
*映射值0-0.5=0-1和0.5-1=1-0
*/
专用浮点反向输入(浮点输入){
如果(输入如果您使用Object或ValueAnimator设置视图动画,您只需执行以下操作
ValueAnimator myAnimator = new ValueAnimator();
myAnimator.reverse()
可以找到文档。您可以让代码记住原始位置和结束位置。并让您的代码在触发动画时动态获取这些值。基于idea,您可以反转任何插值器,而不仅仅是线性插值器
class ReverseInterpolator implements Interpolator{
private final Interpolator delegate;
public ReverseInterpolator(Interpolator delegate){
this.delegate = delegate;
}
public ReverseInterpolator(){
this(new LinearInterpolator());
}
@Override
public float getInterpolation(float input) {
return 1 - delegate.getInterpolation(input);
}
}
用法
如果您使用的是来自xml的动画,那么一个简单的方法就是为原始动画制作一个完全相同的反向动画。将动画.AnimationListener
添加到原始动画中,然后在onAnimationEnd
方法中启动反向动画。这对我很有效
ObjectAnimator anim = ObjectAnimator.ofFloat(imageViewUpb, "rotation", rotationAngle, rotationAngle + 180);
if (linearLayoutb.getVisibility()==GONE){
linearLayoutb.setVisibility(VISIBLE);
anim.setDuration(500);
anim.start();
rotationAngle += 180;
rotationAngle = rotationAngle%360;
imageViewUpb.animate().rotation(rotationAngle).setDuration(500).start();
}else{
linearLayoutb.setVisibility(GONE);
anim.setDuration(500);
anim.start();
rotationAngle += 180;
rotationAngle = rotationAngle%180;
imageViewUpDownb.animate().rotation(rotationAngle).setDuration(500).start();
}
linearlayoutb是当imageviewUpb面朝上时展开的视图
使int rotationAngle=0;全局参数
成为我提出的最简单的解决方案
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="1.0">
</alpha>
</set>
您需要使用RepeatCount和RepeatMode
kotlin
var anim = TranslateAnimation(0, 100, 0, 100)
anim.repeatCount = Animation.INFINITE // you cant 1,2,...
anim.repeatMode = Animation.REVERSE // you can set REVERSE or RESTART
anim.start()
我知道我迟到了。我尝试了这段代码,但它对我不起作用。动画结束后,它只会返回到开始位置,没有任何动画。我如何才能反向设置动画?您好,我在发布代码之前尝试了这段代码,它对我来说是一个简单的测试。请发布一些代码,说明您如何尝试应用插值器。在某些情况下,这s可能是一个解决方案:animation.setRepeatCount(animation.INFINITE);animation.setRepeatMode(animation.REVERSE);这可以是返回1f-paramFloat;
,因为您正在将[0..1]
映射到[1..0]
我不认为有什么东西像反向插值器?你指的是我的动画.setRepeatMode(Animation.REVERSE);
?更改当前答案对我有效!而@pcans-的解决方案在我从return 1-delegate.getInterpolator(input)更改之前对我无效
toreturn delegate.getInterpolation(1-input);
。很有趣。我想它在所有对称委托中都会起作用。这只是API级别+23的ValueAnimator
类是与动画API一起添加的,它是在API 11中引入的。android:repeatMode=“reverse”保存了我的一天:)
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator">
<alpha
android:duration="2000"
android:fromAlpha="0.1"
android:repeatCount="infinite"
android:repeatMode="reverse"
android:toAlpha="1.0">
</alpha>
</set>
var anim = TranslateAnimation(0, 100, 0, 100)
anim.repeatCount = Animation.INFINITE // you cant 1,2,...
anim.repeatMode = Animation.REVERSE // you can set REVERSE or RESTART
anim.start()