安卓笔记:为View设置VISIBLE、GONE的时候添加过度动画

举报
程思扬 发表于 2022/01/13 23:18:13 2022/01/13
【摘要】 直接上代码: 1、VISIBLE动画 TranslateAnimation showAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f, Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIV...

直接上代码:

1、VISIBLE动画


  
  1. TranslateAnimation showAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
  2. Animation.RELATIVE_TO_SELF, 0.0f,
  3. Animation.RELATIVE_TO_SELF, 0.0f,
  4. Animation.RELATIVE_TO_SELF, 0.0f);
  5. showAnim.setDuration(500);
  6. view.startAnimation(showAnim);
  7. view.setVisibility(View.VISIBLE);


2、GONE动画


  
  1. TranslateAnimation hideAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
  2. Animation.RELATIVE_TO_SELF, 1.0f,
  3. Animation.RELATIVE_TO_SELF, 0.0f,
  4. Animation.RELATIVE_TO_SELF, 0.0f);
  5. hideAnim.setDuration(500);
  6. view.startAnimation(hideAnim);
  7. view.setVisibility(View.GONE);

 


这里,最重要的是TranslateAnimation,进入这个构造函数:


  
  1. /**
  2. * Constructor to use when building a TranslateAnimation from code
  3. *
  4. * @param fromXType Specifies how fromXValue should be interpreted. One of
  5. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  6. * Animation.RELATIVE_TO_PARENT.
  7. * @param fromXValue Change in X coordinate to apply at the start of the
  8. * animation. This value can either be an absolute number if fromXType
  9. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  10. * @param toXType Specifies how toXValue should be interpreted. One of
  11. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  12. * Animation.RELATIVE_TO_PARENT.
  13. * @param toXValue Change in X coordinate to apply at the end of the
  14. * animation. This value can either be an absolute number if toXType
  15. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  16. * @param fromYType Specifies how fromYValue should be interpreted. One of
  17. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  18. * Animation.RELATIVE_TO_PARENT.
  19. * @param fromYValue Change in Y coordinate to apply at the start of the
  20. * animation. This value can either be an absolute number if fromYType
  21. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  22. * @param toYType Specifies how toYValue should be interpreted. One of
  23. * Animation.ABSOLUTE, Animation.RELATIVE_TO_SELF, or
  24. * Animation.RELATIVE_TO_PARENT.
  25. * @param toYValue Change in Y coordinate to apply at the end of the
  26. * animation. This value can either be an absolute number if toYType
  27. * is ABSOLUTE, or a percentage (where 1.0 is 100%) otherwise.
  28. */
  29. public TranslateAnimation(int fromXType, float fromXValue, int toXType, float toXValue,
  30. int fromYType, float fromYValue, int toYType, float toYValue) {
  31. mFromXValue = fromXValue;
  32. mToXValue = toXValue;
  33. mFromYValue = fromYValue;
  34. mToYValue = toYValue;
  35. mFromXType = fromXType;
  36. mToXType = toXType;
  37. mFromYType = fromYType;
  38. mToYType = toYType;
  39. }


可以看到,这个构造函数主要是传入设置起始点坐标(x,y)、终点坐标(x,y),每个坐标值对应一个类型type。

Animation.RELATIVE_TO_SELF代表着坐标以当前view为基准。

0.0f即0%,代表view初始位置坐标;

1.0f即100%,代表以view初始位置为原点,相应x坐标/y坐标增加父宽度/父高度的100%;


  
  1. TranslateAnimation showAnim = new TranslateAnimation(Animation.RELATIVE_TO_SELF, 1.0f,
  2. Animation.RELATIVE_TO_SELF, 0.0f,
  3. Animation.RELATIVE_TO_SELF, 0.0f,
  4. Animation.RELATIVE_TO_SELF, 0.0f);


表示从(1.0f,0.0f)移动到(0.0f,0.0f),即view从屏幕右侧不可见区域移动到初始位置,符合VISIBLE效果。


  
  1. new TranslateAnimation(Animation.RELATIVE_TO_SELF, 0.0f,
  2. Animation.RELATIVE_TO_SELF, 1.0f,
  3. Animation.RELATIVE_TO_SELF, 0.0f,
  4. Animation.RELATIVE_TO_SELF, 0.0f);

表示从(0.0f,0.0f)移动到(1.0f,0.0f),即view从初始位置移动到屏幕右侧不可见区域,符合GONE效果。

文章来源: chengsy.blog.csdn.net,作者:程思扬,版权归原作者所有,如需转载,请联系作者。

原文链接:chengsy.blog.csdn.net/article/details/82661550

【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。