android – 如何为展开/折叠视图的图标设置动画?

前端之家收集整理的这篇文章主要介绍了android – 如何为展开/折叠视图的图标设置动画?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有这个图标:

当我在列表视图中按下我的项目时,我希望它旋转180°.当我再次点击时,我希望它再旋转180°,这样就可以达到它的原始位置.

首先我试过:

  1. view.animate().rotation(180).setDuration(500).start();

但它只发射一次.之后我尝试了:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <set xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:fillAfter="true"
  4. android:fillEnabled="true">
  5.  
  6. <rotate
  7. android:duration="500"
  8. android:fromDegrees="0"
  9. android:pivotX="50%"
  10. android:pivotY="50%"
  11. android:toDegrees="180" />
  12. </set>

但是,即使箭头已经显示在顶部,动画始终以箭头显示到按钮并旋转到顶部开始.

那我怎么能让它工作呢?

解决方法

用户按照代码单击图像事件:
  1. ObjectAnimator anim = ObjectAnimator.ofFloat(v,"rotation",rotationAngle,rotationAngle + 180);
  2. anim.setDuration(500);
  3. anim.start();
  4. rotationAngle += 180;
  5. rotationAngle = rotationAngle%360;

并使rotationAngle成为一个全局变量

  1. int rotationAngle = 0;

猜你在找的Android相关文章