簡體   English   中英

Android RotateAnimation無法按預期工作

[英]Android RotateAnimation not working as expected

我想以這種方式為imageView設置動畫:(角度符號定義為三角法)

  1. 平滑旋轉從0到-45
  2. 平滑旋轉從-45到+45
  3. 平滑旋轉從+45到-45
  4. 重啟到2(無限循環)

使用下面的代碼,我得到:

  1. 平滑旋轉從0到+45
  2. 立刻回到0
  3. 平滑旋轉從0到-45
  4. 平滑旋轉從-45到0
  5. 重啟到3(無限循環)

所以,根本不是我想要的!

有誰看到如何解決這個問題?

謝謝 !

這是我的代碼:

float angle = 45f;

        RotateAnimation rotateAnimation1 = new RotateAnimation(0, -angle,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);            
        rotateAnimation1.setStartOffset(0);
        rotateAnimation1.setDuration(2000);
        rotateAnimation1.setInterpolator(new LinearInterpolator());



        RotateAnimation rotateAnimation2 = new RotateAnimation(-angle, angle,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        //  animationSet.addAnimation(rotateAnimation);
        rotateAnimation2.setStartOffset(0);
        rotateAnimation2.setDuration(4000);
        rotateAnimation2.setInterpolator(new LinearInterpolator());



        RotateAnimation rotateAnimation3 = new RotateAnimation(angle, -angle,
                Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);       
        rotateAnimation3.setStartOffset(4000);
        rotateAnimation3.setDuration(4000);
        rotateAnimation3.setInterpolator(new LinearInterpolator());



        final AnimationSet animSet1 = new AnimationSet(true);
        animSet1.setFillEnabled(true);
        animSet1.addAnimation(rotateAnimation1);


        final AnimationSet animSet2 = new AnimationSet(true);
       // animSet2.setFillEnabled(true);
        animSet2.addAnimation(rotateAnimation2);
        animSet2.addAnimation(rotateAnimation3);


        animSet1.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                view.startAnimation(animSet2);


            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });


        animSet2.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {

                view.startAnimation(animSet2);

            }

            @Override
            public void onAnimationRepeat(Animation animation) {
            }
        });





        view.startAnimation(animSet1);

視圖返回零的原因是視圖的rotation屬性不會更改。 您應該在第一個動畫結束后將旋轉設置為視圖,或者使用ObjectAnimator.ofFloat(imageview ,"rotation", 0f, 360f); 默認設置視圖的旋轉。

1.從0到-45的平滑旋轉:反轉角度符號和setFillAfter(true)使視圖保持結束動畫位置

    RotateAnimation rotateAnimation1 = new RotateAnimation(0, 45f,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
    rotateAnimation1.setFillAfter(true); //This keeps view at animation ended position            
    rotateAnimation1.setStartOffset(0);
    rotateAnimation1.setDuration(2000);
    rotateAnimation1.setInterpolator(new LinearInterpolator());

2.從-45到+45的平滑旋轉:將動畫起始角度設置為0,因為視圖已經旋轉。

    // Since view already is at -45 position due to rotateAnimation1.setFillAfter(true), now start point is 0 again
    RotateAnimation rotateAnimation2 = new RotateAnimation(0, -90,
            Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
    rotateAnimation2.setFillAfter(true);
    rotateAnimation2.setStartOffset(0);
    rotateAnimation2.setDuration(4000);
    rotateAnimation2.setInterpolator(new LinearInterpolator());

其余步驟遵循上述邏輯是直截了當的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM