【问题标题】:Android layout animation questionAndroid布局动画问题
【发布时间】:2011-05-25 22:37:12
【问题描述】:

我正在尝试为过渡设置动画,但它没有给我正确的结果

我的布局如下所示:

  • 线性视图Root
    • 滚动视图Groups
      • 线性视图
        • 平铺1
        • 瓷砖2
        • 瓷砖3
    • 滚动视图SubGroups
      • 线性视图
        • 瓷砖4
        • 瓷砖5
        • 瓷砖6

Root 的方向设置为水平,GroupsSubGroups 的宽度和高度都设置为父填充。

我想要的是将Groups 平移到屏幕左侧,以便仅显示~40 dp,而SubGroups 平移到Groups 后面的左侧,因此只有一小部分@ 987654330@ 已显示,SubGroups 的 90% 可见。

这可能吗?感谢您的帮助!

【问题讨论】:

    标签: android animation android-layout


    【解决方案1】:

    我认为你的意思是这样做:

    TranslateAnimation animateGroups = new TranslateAnimation(0,widthScreen - 40 , 0 , 0);
    animateGroups.setDuration(1200);
    animateGroups.setFillAfter(true);
    
    TranslateAnimation animateSubGroups = new TranslateAnimation(0,widthScreen - 10 , 0 , 0);
    animateSubGroups.setDuration(1200);
    animateSubGroups.setFillAfter(true);
    
    scrollViewGroups.startAnimation(animateGroups);
    scrollViewSubGroups.startAnimation(animateSubGroups);
    

    注意:您可以使用DiplayMetrics类获取屏幕尺寸, 如果要将像素转换为 dp ,请参阅this

    编辑在动画结束后更改视图的位置 要做到这一点,你应该在你的动画上添加一个监听器,

    animateGroups.addAnimationListener(AnimationListener);
    

    并像这样覆盖方法:

     @Override
     public void onAnimationEnd(Animation animation){
    scrollViewGroups.setPadding(0, 0 , screenWidth-40 , 0 ) ;
    //or you can set the Margin like this(i supposed that your scrollView is in a RelativeLayout 
    RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)scrollViewGroups.getLayoutParams();
    params.setMargin(0, 0 , screenWidth-40 , 0);
    scrollViewGroups.setLayoutParams(params);
    }
    

    【讨论】:

    • 感谢动画!不过我有两个问题,动画运行后,第一组(移出屏幕的那个)是正确的,但第二组(子组)根本不显示?我需要做些什么来将可见性设置为 true 吗?此外,动画完成后,我注意到点击坐标不反映翻译。例如,可以通过单击动画播放之前的位置而不是其新位置来单击我的一个按钮。感谢您提供更多帮助:)
    • 我们在这里为您提供帮助;关于动画,只是一个效果而已,当我将FillAfter设置为true时,意味着动画结束时视图保持在最后一个位置,(只是视觉效果,所有视图的真实位置不会改变,所以如果您想更改视图的位置,请参阅我的编辑
    • 感谢您的更新。为 scrollViewGroups 设置填充不会更新视图的位置,只是更新视图内元素的位置。有没有办法设置视图的实际位置以反映动画的当前帧?
    • 将填充应用到滚动视图,如果它不起作用,您可以使用 SetMargins ,再次查看我的编辑
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-05
    • 2012-01-25
    • 2016-08-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多