2015-07-19 145 views
0

我想爲我的ImageView構建放大和縮小動畫,我設置了一個偵聽器並將動畫RepeatCount設置爲無限。ImageView放大,縮小無限動畫和onAnimation重複問題

首先我開始放大效果,然後在onAnimationRepeat方法中,我使用布爾值創建縮小部分,我想重新開始放大整個效果。但是在第一次onAnimationRepeat沒有被再次調用之後,動畫重複播放,但它仍然卡在縮小部分。

我錯過了什麼?

//image animation 
     Animation anim = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f); 
     anim.setInterpolator(new LinearInterpolator()); 
     anim.setRepeatCount(Animation.INFINITE); 
     anim.setDuration(10000); 
     zoomIn = true; 

     // Start animating the image 
     final ImageView splash = (ImageView) findViewById(R.id.imageView); 
     splash.startAnimation(anim); 

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

      } 

      @Override 
      public void onAnimationEnd(Animation animation) { 
      } 

      @Override 
      public void onAnimationRepeat(Animation animation) { 
       if(zoomIn) { 
        Log.w("", "we zoom out, and zoomIn is: " + zoomIn); 
        Animation anim = new ScaleAnimation(1.1f, 1f, 1.1f, 1f); 
        anim.setInterpolator(new LinearInterpolator()); 
        anim.setRepeatCount(Animation.INFINITE); 
        anim.setDuration(10000); 
        splash.startAnimation(anim); 
        zoomIn = false; 

       } else if(!zoomIn) { 
        Log.w("", "we zoom in, and zoomIn is: " + zoomIn); 
        Animation anim = new ScaleAnimation(1.0f, 1.1f, 1.0f, 1.1f); 
        anim.setInterpolator(new LinearInterpolator()); 
        anim.setRepeatCount(Animation.INFINITE); 
        anim.setDuration(10000); 
        splash.startAnimation(anim); 
        zoomIn = true; 
       } 


      } 
     }); 


    } 

回答

2

剛切換到ObjectAnimator並使用以下:

ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, 1.1f); 
ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, 1.1f); 

AnimatorSet scaleAnim = new AnimatorSet(); 
scaleAnim.setDuration(10000); 
scaleAnim.play(scaleX).with(scaleY); 

scaleAnim.setRepeatCount(ObjectAnimator.INFINITE); 
scaleAnim.setRepeatMode(ObjectAnimator.RESTART/REVERSE...); 
+2

應該有'scaleAnim.start()的調用;在'結束。 'setRepeatCount'和'setRepeatMode'是'ObjectAnimator'的方法,而不是'AnimatorSet'。 –

3

圖像視圖的動畫順序

  • 初始化I = 0
  • 創建不同的動畫對象
  • 創建圖像陣列視圖id的
  • 與處理器的幫助下決定自己的時隙
  • 添加動畫文件夾中的動畫文件

final Animation animFirst = AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_expand); 
final Animation animSecond = AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_expand); 
final Animation animThird = AnimationUtils.loadAnimation(getActivity(), R.anim.shrink_expand); 


final int[] imageId = new int[]{R.id.step_1, R.id.step_2, R.id.step_3}; 
final List<Animation> anim = new ArrayList<>(); 
anim.add(animFirst); 
anim.add(animSecond); 
anim.add(animThird); 


final Handler handler = new Handler(); 
handler.postDelayed(new Runnable() { 


    public ImageView imageView; 

    public void run() { 
     if (i < imageId.length) { 
      imageView= ((ImageView) mBinding.getRoot().findViewById(imageId[i])); 
      imageView .startAnimation(anim.get(i)); 
      anim.get(i).setFillAfter(true); 

      i++; 
     } else { 
      i = 0; 
      anim.get(0).setFillAfter(false); 
      anim.get(1).setFillAfter(false); 
      anim.get(2).setFillAfter(false); 

     } 
     handler.postDelayed(this, 2000); 
    } 

}, 500); 



}