如何在Android的视图中制作动画? [英] How to do animations in views in Android?

查看:96
本文介绍了如何在Android的视图中制作动画?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我根据要求进行了很多练习和示例,但我仍然认为我在某处缺少了一些东西。我正在尝试隐藏和显示带有上下滑动动画的视图。

I went through lots of tuts and examples for my requirement but still I think I am missing something somewhere. I am trying to hide and show a view with slide up and slide down animation.

这就是我想要的

图1(单击按钮1之前)

fig 1 (before button 1 is clicked)

============================
|       view1              |
============================
| button 1 |
============

图2(按钮后单击1)

============================
|       view1              |
============================
|                          | 
|       view 2             |
|                          |
============================
| button 1 |
============

您可以看到我我想在单击按钮1时显示和隐藏view2。

As you can see I want to show and hide view2 when I click on button 1.

我已经通过以下代码成功做到了这一点。隐藏:

I have managed to do this by the following code. To hide:

TranslateAnimation slide = new TranslateAnimation(0, 0,0,-200);
slide.setDuration(1000);
slide.setFillAfter(true);
view2.startAnimation(slide);
view2.setVisibility(View.GONE);

要显示:

TranslateAnimation slide = new TranslateAnimation(0, 0,-200,0);
slide.setDuration(1000);
slide.setFillAfter(true);
view2.startAnimation(slide);
view2.setVisibility(View.VISIBLE);

这很好,但是代码中唯一的问题是消失和可见代码在动画完成之前完成。即按钮1在单击以显示时向下移动,动画随后完成。隐藏操作也一样,如果完成,按钮1会在动画之前向上移动。

This works fine but the only problem in both the code is that the 'Gone' and 'Visible' code is completed before the animation is completed. i.e. 'button 1' moves down when clicked to show and the animation is completed later. same happens for hide, the 'button 1' moves up before the animation if completed.

我想将按钮与动画一起移动。

I want to move the button along with the animation.

推荐答案

动画具有方法setAnimationListener(AnimationListener)。 AnimationListener具有树回调

the animation has the method setAnimationListener(AnimationListener). AnimationListener has tree callback


  1. onAnimationStart

  2. onAnimationRepeat

  3. onAnimationEnd

您可以在调用onAnimationEnd时更改可见性。

you can change the visibility when the onAnimationEnd is called.

例如:

slide.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
                              // here you can change the visibility
        }
);

这篇关于如何在Android的视图中制作动画?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆