2015-07-19 2 views
0

Я хотел бы создать анимацию увеличения и уменьшения для моего ImageView, я настроил прослушиватель и установил анимацию RepeatCount в бесконечность.ImageView Увеличить масштаб, уменьшить масштаб бесконечной анимации и onAnimationRepeat issue

Сначала я начинаю с эффекта масштабирования, затем в методе 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

Изображение вид анимации последовательно

  • Initialize я = 0
  • создавать различные анимации объектов
  • создать массив изображений Вид идентификаторами
  • с помощью обработчика решить ваш временной интервал
  • добавить свой файл анимации в папке Anim

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); 



} 
Смежные вопросы