Animate viewgroup without animating children

亡梦爱人 提交于 2020-01-14 03:11:47

问题


i have built a custom viewgroup and m beginning to implement animations tot his viewgroup. the problem i am having is that i need the viewgroup to be able to animate without it's children being animated by the same animation.

Lets say i am scaling the viewgroup to 2x size, but i still want to keep children at the same size (thus fitting more children into the viewgroup).

Right now i am calling the animation as follows:

    ScaleAnimation expandAnimation =  new ScaleAnimation(1,2,1,2);
    this.startAnimation(expandAnimation);

Thanks in advance! :)


回答1:


You can't directly do what you're seeking to do with the old Android animation API. You've already noticed one limitation - that scaling the group also includes the children.

The second limitation is that the old Android animation API only affects the visual render of the item you're animating, and only for the time the animation is active. In other words, it doesn't actually alter the size or positioning of the item in question in terms of view layout. It only temporarily renders it a different way. So for instance, to animate a button moving from x to y across the screen, you would need to run the animation, then when the animation completes actually reposition the button using the layout APIs.

Probably the way to do what you're looking for is:

  1. Incorporate an extra ViewGroup widget in your layout, positioned and sized over top the widget containing the "real" items. Initially, it's invisible.
  2. Make it visible, and play the animation.
  3. When animation complete, hide the extra widget, then resize your real ViewGroup widget via LayoutParams and add your extra items.
  4. You'll probably need to tweak this general outline to make the effect look smooth. (For instance, resize the real viewgroup in the background before you remove the fake viewgroup from the screen).

Android introduced a newer, more flexible animation API in 3.0 Honeycomb, but unfortunately it's not available if you're targeting 2.x devices (i.e., phones). Hopefully this will be easier once the new APIs are mainstream.



来源:https://stackoverflow.com/questions/6524696/animate-viewgroup-without-animating-children

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!