android - 动画后使 Imageview 消失
问题描述
我有一个方法可以为具有指定参数的 ImageView 对象创建动画:
public void animateMove(float x, float y, int milsecs)
{
float origX = view.getX();
float origY = view.getY();
view.setVisibility(View.VISIBLE);
Path linePath = new Path();
linePath.lineTo(x, y);
ObjectAnimator anim = ObjectAnimator.ofFloat(view, "translationX", "translationY", linePath);
anim.setDuration(milsecs);
anim.start();
view.setVisibility(View.INVISIBLE); // this code is the problem
view.setX(origX);
view.setY(origY);
}
但是,当我调用 setVisibility 方法使 ImageView 不可见时,它会在动画发生的同时运行,因此实际上什么都看不到。如果我删除这段代码,我可以很好地看到视图的动画。
我怎样才能让这个方法创建一个动画并在整个动画完成后才将其变为不可见?
解决方案
将此添加到您的代码之前anim.start()
:
anim.addListener(new Animator.AnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
}
@Override
public void onAnimationEnd(Animator animation) {
view.setVisibility(View.INVISIBLE);
view.setX(origX);
view.setY(origY);
}
@Override
public void onAnimationCancel(Animator animation) {
}
@Override
public void onAnimationRepeat(Animator animation) {
}
});
推荐阅读
- java - 如何为 Lucene 8.2 编写 IP 地址正则表达式查询?
- vue.js - 更改字段掩码 v-currency-field?
- java - @Embedded 对象在私有时不保存
- docker - 通过 GitLab 容器注册表构建 Docker 失败
- angular - 如何在打字稿中处理循环依赖数据结构
- json - Google Classroom Restful API - 上交作业
- node.js - 找不到 $geoNear 查询的索引,代码:2,代号:'badValue'
- angular - 路由更改后可观察对象未重置为初始状态?
- ruby-on-rails - 将 Factory Bot 添加到 Rails 测试套件会破坏它
- java - 如何对多个条件使用相同的参数