简体   繁体   English

Android-动画化以编程方式添加到布局的视图

[英]Android - Animate views that are programatically added to the layout

I am creating a couple of TextViews programmatically and adding to a Linear Layout.Everything works fine. 我正在以编程方式创建几个TextViews并添加到Linear Layout。一切正常。

Now I want to add some animations on the TextViews. 现在,我想在TextViews上添加一些动画。 After adding the textviews I am trying to add animations on them using the ViewPropertyAnimator. 添加textview后,我试图使用ViewPropertyAnimator在其上添加动画。 But I don't see any animations. 但我看不到任何动画。 If I add them in the xml for testing, it works fine. 如果我将它们添加到xml中进行测试,则效果很好。

I found some thing like we need to add an event for when the textview is attached to the screen and code the animation here. 我发现有些事情,例如当textview附加到屏幕上并在此处编码动画时,我们需要添加一个事件。 But I have a set of animations to be run on each TextView and after it is done I need to start animations on the other. 但是我在每个TextView上都有一组动画要运行,完成后,我需要在另一个上启动动画。

Any ideas or pointers how I can achieve this? 有什么想法或建议可以实现这一目标吗?

My code is some thing like, 我的代码是这样的,

-- Create the TextViews(invisible) in a loop and add them to the layout. -循环创建TextViews(不可见)并将其添加到布局中。
-- After the above step trying to animate like below each of the text views one by one. -在执行上述步骤后,尝试像下面每个文本视图一样进行动画处理。 I need to chain as I need to fade in one by one. 我需要连锁,因为我需要一张一张地淡入淡出。

        tv1.setVisibility(View.VISIBLE);
        tv1.animate().alpha(1).setDuration(500);

But the animations are not working. 但是动画不起作用。

Thanks for any help. 谢谢你的帮助。

The ViewPropertyAnimator animates properties from their current value to a specified value. ViewPropertyAnimator属性从当前值动画ViewPropertyAnimator指定值。 Since the alpha of newly instantiated Views is 1, your animation will animate alpha from 1 to 1. You should first set the alpha to 0 rather than setting the visibility: 由于新实例化的“视图”的Alpha为1,因此动画将使Alpha的动画从1变为1。您应该首先将Alpha设置为0,而不是设置可见性:

tv1.setAlpha(0);
tv1.animate().alpha(1).setDuration(500);

将此添加到您包含的视图组android:animateLayoutChanges="true"并在此处http://developer.android.com/reference/android/animation/LayoutTransition.html上查看LayoutTransition

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM