Search code examples
androidxmlanimationscale

Android animation scale onEnd blink


I have problem with scale in animation. Translate works fine, but when I add scale to my animation it dont come back to point of start and blink when animation starts again.

 <set xmlns:android="http://schemas.android.com/apk/res/android"  
    android:interpolator="@android:anim/linear_interpolator">

 <translate
 android:fromYDelta="-150"  
 android:toYDelta="150"
 android:fillAfter="true"
 android:duration="1500"

 /> 
     <scale 
         android:fillAfter="true"
        android:fromXScale="1"
        android:fromYScale="1"
        android:toXScale="1.2"
        android:toYScale="1.2"
        android:duration="1500"
        android:pivotX="50%"
        android:pivotY="50%"
        />   
  </set>


 <set xmlns:android="http://schemas.android.com/apk/res/android"  
  android:interpolator="@android:anim/linear_interpolator">

 <translate
 android:fromYDelta="150"     
 android:toYDelta="-150"
 android:fillAfter="true"  
 android:duration="1500"
 android:startOffset="1500"

 />  

     <scale 
         android:fillAfter="true"
        android:fromXScale="1.2"
        android:fromYScale="1.2"
        android:toXScale="1"
        android:toYScale="1"
        android:duration="1500"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="1500"
        />  
 </set>

And Android code:

   protected void onCreate(Bundle savedInstanceState) {

       super.onCreate(savedInstanceState);  
       setContentView(R.layout.activity_main);  

       image = (ImageView) findViewById(R.id.image);  
       image.setImageResource(R.drawable.shape);  

       animation2 = AnimationUtils.loadAnimation(this, R.anim.moving_up);
       animation3 = AnimationUtils.loadAnimation(this, R.anim.moving_down);

       animation =  new AnimationSet(true);
       animation.addAnimation(animation2);
       animation.addAnimation(animation3);

       animation.setAnimationListener(this);
       //animation.setRepeatCount(Animation.INFINITE);

       animation.setFillEnabled(true);
       animation.setFillAfter(true);
       image.startAnimation(this.animation);

   }


   @Override  
   public void onAnimationEnd(Animation animation) {

   image.clearAnimation();
   image.startAnimation(animation); 
   }  

Shape is a xml Rect.


Solution

  • If someone would like to know the answer

        private void animateImage(){
    
        float x = 1;
        float scale = 1.1f;
    
            x = (imageViewImage.getHeight()*1.1f - imageViewImage.getHeight())/2;
    
        imageViewImage.animate().translationY(x).scaleX(scale).scaleY(scale).setDuration(8000).withEndAction(new Runnable() {
    
                @Override
                public void run() {
    
                    imageViewImage.animate().translationY(0).scaleX(1f).scaleY(1f).setDuration(8000).withEndAction(new Runnable() {
    
                        @Override
                        public void run() {
    
                            animateImage();
    
                        }
                    });
    
                }
            });
        }