reset animation effects in android

2019-07-17 02:12发布

I am performing a fade effect on a button.

AnimationSet set = new AnimationSet(true);
Animation animation2 = new AlphaAnimation((float) 0, 1);
animation2.setDuration(1500);
animation2.setRepeatMode(0);
set.addAnimation(animation2);
set.setFillAfter(true); // leaves the animation in its final status
btn.startAnimation(set);

I am using the setFillAfter(true) option to leave the button visible. That works fine. Now I would like to perform the animation again, but can't never set again my button to be invisible on the screen, unless I restart the app. (alos tried invalidate() with no success...)

Any idea would be "so" welcome.

Thanks in advance! Paul

2条回答
Luminary・发光体
2楼-- · 2019-07-17 02:49

Thank you for you help. Actually I found the answer on the developpers doc. There is a big difference between "View animation" (which I tried to do) and "Property Animation". Basically View animation redraws an image of the view you are working on, but does not affect the original view. So If you translate your view from example, on the screen the view is moved but programatically it remained in its initial state and will catch events in its original position.

Here is the explanation:

How Property Animation Differs from View Animation The view animation system provides the capability to only animate View objects, so if you wanted to animate non-View objects, you have to implement your own code to do so. The view animation system is also constrained in the fact that it only exposes a few aspects of a View object to animate, such as the scaling and rotation of a View but not the background color, for instance.

Another disadvantage of the view animation system is that it only modified where the View was drawn, and not the actual View itself. For instance, if you animated a button to move across the screen, the button draws correctly, but the actual location where you can click the button does not change, so you have to implement your own logic to handle this.

With the property animation system, these constraints are completely removed, and you can animate any property of any object (Views and non-Views) and the object itself is actually modified. The property animation system is also more robust in the way it carries out animation. At a high level, you assign animators to the properties that you want to animate, such as color, position, or size and can define aspects of the animation such as interpolation and synchronization of multiple animators.

The view animation system, however, takes less time to setup and requires less code to write. If view animation accomplishes everything that you need to do, or if your existing code already works the way you want, there is no need to use the property animation system. It also might make sense to use both animation systems for different situations if the use case arises.

http://developer.android.com/guide/topics/graphics/prop-animation.html

For my case I used Property animation and it matches all my animation needs. Thank you for your support! :)

Paul

查看更多
Bombasti
3楼-- · 2019-07-17 02:58

take out set.setFillAfter(true); and use an animationListener to set the Button to visible when the animation finishes, it will give the same effect to the user, and you'll be able to make it invisible again with View.setVisibility(View.INVISIBLE);

AnimationListener animListener;


animListener = new Animation.AnimationListener() {

        @Override
        public void onAnimationStart(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationRepeat(Animation animation) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            btn.setVisibility(View.VISIBLE);
        }
};
animation2.setAnimationListener(animListener);
查看更多
登录 后发表回答