Android下滑动画

时间:2011-11-02 16:20:34

标签: android view translate-animation

我在底部对齐了一个工具栏,在它上方有一个WebView,它填充了剩余的高度。现在我想向下滑动工具栏以隐藏它,并且当它动画时,webview高度应该扩展。我尝试过使用TranslateAnimation但不幸的是它没有调整工具栏的实际位置,只是它的内容被移动了。这是我试过的:

<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromXDelta="0%"
    android:toXDelta="0%"
    android:fromYDelta="0%"
    android:toYDelta="100%"
    android:duration="700"
    android:fillAfter="true" />

实际代码:

final View v = findViewById(R.id.browseToolbar);
Animation animation = AnimationUtils.loadAnimation(someContext, R.anim.toolbar_hide);
v.startAnimation(animation);

我该怎么做?

2 个答案:

答案 0 :(得分:1)

我写了一些代码来做实际调整大小的动画。看看:

http://www.touchlab.co/blog/resize-animation/

另请参阅我在Android动画上所做的演示

https://docs.google.com/present/view?id=djqv5kb_187c62jvbf7

基本上,如上所述,常规动画类仅影响其父视图中的内容。理解它们的关键是它们不是“真实的”。将Android动画视为海市蜃楼。如果您运行我创建的示例应用程序,请说您执行缩放动画并缩小按钮,如果单击外部,按钮USED为,它仍然会注册为按钮单击。 Android动画实际上不会影响真实的边界和尺寸。

我的博客文章中的代码基本上是实现ViewGroup.OnHierarchyChangeListener。当从层次结构中添加/删除内容时,容器会为物理调整大小设置动画。

答案 1 :(得分:0)

当您使用Android动画时,仅移动视图的像素。要实际移动视图,您需要向动画对象添加动画侦听器并覆盖onAnimationEnd方法。在该方法中,您需要以编程方式移动视图。

 AnimationListener animListener = new AnimationListener() {
     @Override
     public void onAnimationStart(Animation arg0) {}            
     @Override
     public void onAnimationRepeat(Animation arg0) {}                   
     @Override
     public void onAnimationEnd(Animation arg0) {
        Move the view here          
     }
 };

 animation.setAnimationListener(animListener);