Android 下滑动画

发布于 2024-12-13 05:49:09 字数 639 浏览 0 评论 0原文

我有一个底部对齐的工具栏和一个位于其上方的 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);

我该怎么做?

I have a toolbar aligned at the bottom and a WebView above it, which fill remaining height. Now I want to slide down the toolbar in order to hide it, and while it's animating the webview height should expand. I've tried using TranslateAnimation but unfortunetely it's not adjusting toolbar's real position, only it's contents are moved. Here's what I tried:

<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" />

And the actual code:

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

How can I do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

逆蝶 2024-12-20 05:49:09

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

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

另请参阅我针对 Android 动画所做的演示

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

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

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

I wrote some code to do actual resize animations. Take a look:

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

Also look at a presentation I did on Android animation

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

Essentially, as mentioned above, the regular animation classes only affect what's in their parent view. The key to understanding them is they aren't "real". Think of the Android animation as a mirage. If you run the example app I created, say you do a scale animation and make a button smaller, if you click outside, where the button USED to be, it still registers as a button click. Android animations don't actually affect real boundaries and dimensions.

What the code in my blog post does, essentially, is implement a ViewGroup.OnHierarchyChangeListener. When stuff is added/removed from the hierarchy, the container animates a physical resize.

乖乖 2024-12-20 05:49:09

当您使用 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);

When you use android animation only the pixels of the view are shifted. To actually move the view, you will need to add an animation listener to the animation object and override the onAnimationEnd method. In that method you need to programatically move the view.

 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文