How can I apply the changes to the position of a view after an animation?

孤者浪人 提交于 2019-12-11 00:44:24

问题


I applyed a TranslateAnimation to an EditText with the FillAfter=true in order to keep its position at the end of the animation. The animation works properly but the problem is that I can't enter to the edittext anymore. I think it's due to the fact that the animation affects only the rendering without modifying the actual view coordinates.

Is there any possibility to maintain the final coordinates with the edittext normally working?

Thanks, Daniele


回答1:


Unfotunately the animation only renders the raw pixels of the animated element, but not its "android-intern" position. The best solution (that i am able to come up with) is to use an AnimationListener and set the position of the animated element correctly after the animation has finished. Here is my code to slideDown a searchWrapper:

public void toggleSearchWrapper() {

    AnimationSet set = new AnimationSet(true);

    // slideDown Animation
    Animation animation = new TranslateAnimation(
          Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
          Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f
    );



    animation.setDuration(300);
    animation.setFillEnabled(false);


    animation.setAnimationListener(new AnimationListener() {

        @Override
        public void onAnimationStart(final Animation anim) {
        };

        @Override
        public void onAnimationRepeat(final Animation anim) {
        };

        @Override
        public void onAnimationEnd(final Animation anim) {


            // clear animation to prevent flicker
            searchWrapper.clearAnimation();

            // set new "real" position of wrapper
            RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
            lp.addRule(RelativeLayout.BELOW, R.id.branchFinderIncludeHeader);
            searchWrapper.setLayoutParams(lp);

        }   

    });


    set.addAnimation(animation);

    // set and start animation
    searchWrapper.startAnimation(animation);



}


来源:https://stackoverflow.com/questions/4397443/how-can-i-apply-the-changes-to-the-position-of-a-view-after-an-animation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!