View animation right to left android

前端 未结 3 1923
隐瞒了意图╮
隐瞒了意图╮ 2020-12-13 01:20

I am not able to put view animation for inflated layouts. I used the following code snippet

pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.         


        
相关标签:
3条回答
  • 2020-12-13 01:24

    I know that you have already accepted the answer. But I think this reply will be helpful for somebody reading this. You can try removing .xml from, pageView.startAnimation(AnimationUtils.loadAnimation(this,R.anim.right_to_left_anim.xml));

    0 讨论(0)
  • 2020-12-13 01:25

    Here is the code for the sliding animation for view.

    1)inFromRightAnimation
    
        private Animation inFromRightAnimation() {
    
            Animation inFromRight = new TranslateAnimation(
                    Animation.RELATIVE_TO_PARENT, +1.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f,
                    Animation.RELATIVE_TO_PARENT, 0.0f);
            inFromRight.setDuration(500);
            inFromRight.setInterpolator(new AccelerateInterpolator());
            return inFromRight;
            }
    
     2)outToLeftAnimation   
        private Animation outToLeftAnimation() {
        Animation outtoLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, -1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoLeft.setDuration(500);
        outtoLeft.setInterpolator(new AccelerateInterpolator());
        return outtoLeft;
        }
    
    3)inFromLeftAnimation
    
        private Animation inFromLeftAnimation() {
        Animation inFromLeft = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, -1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
        inFromLeft.setDuration(500);
        inFromLeft.setInterpolator(new AccelerateInterpolator());
        return inFromLeft;
        }
    
    4)outToRightAnimation
    
        private Animation outToRightAnimation() {
        Animation outtoRight = new TranslateAnimation(
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, +1.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f,
            Animation.RELATIVE_TO_PARENT, 0.0f);
        outtoRight.setDuration(500);
        outtoRight.setInterpolator(new AccelerateInterpolator());
        return outtoRight;
        }
    

    and now start Animation on view

    pageView.startAnimation(inFromRightAnimation());
    

    Thanks,

    0 讨论(0)
  • 2020-12-13 01:45

    If you're trying to animate the view when it's first created, then you either need to set the layoutAnimation XML property or call setLayoutAnimation().

    If you just want to make your view look like it's moving, you need a TranslateAnimation; see this answer: https://stackoverflow.com/a/4214490/832776 Also if you'd like to repeat the animation, then call setAnimationListener() and in onAnimationEnd() just start the animation again.

    If you're trying to move the view permanently, see this: http://www.clingmarks.com/how-to-permanently-move-view-with-animation-effect-in-android/400

    0 讨论(0)
提交回复
热议问题