gpt4 book ai didi

android - 如何反转正在运行的动画?

转载 作者:行者123 更新时间:2023-11-29 01:16:19 26 4
gpt4 key购买 nike

Button 被触摸时,它会启动缩放动画。

Animation zoomIn = AnimationUtils.loadAnimation(context, R.anim.zoomin);
view.startAnimation(zoomIn);

zoomin.xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillEnabled="true"
android:fillAfter="true">
<scale
android:fromXScale="1.0"
android:toXScale="1.3"
android:fromYScale="1.0"
android:toYScale="1.3"
android:pivotX="50%"
android:pivotY="50%"
android:duration="200"
android:interpolator="@android:anim/accelerate_interpolator"/>
</set>

但如果 Button 在这个仍在运行的动画期间被释放,它应该从这个时间点反转动画。

我怎样才能做到这一点?

最佳答案

您可以使用 ViewPropertyAnimator 来实现类似的目的。

例如:

Button button = (Button) findViewById(R.id.yourButton);

button.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {

// adjust the scale and duration values according to your needs

if (event.getAction() == MotionEvent.ACTION_DOWN) {
v.animate().scaleX(2).scaleY(2).setDuration(2000);
} else if (event.getAction() == MotionEvent.ACTION_UP) {
v.animate().scaleX(1).scaleY(1).setDuration(2000);
}
return false;
}
});

关于android - 如何反转正在运行的动画?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39371816/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com