Android动画刻度onEnd闪烁

时间:2014-07-02 08:31:40

标签: android xml animation scale

我在动画中有缩放问题。翻译工作正常,但是当我为我的动画添加比例时,它不会回到开始点并在动画再次开始时闪烁。

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

Android代码:

   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是一个xml Rect。

2 个答案:

答案 0 :(得分:1)

animation.setFillAfter(true);

动画完成后,

将强制图像保持在动画位置。

如果要将图像返回到起始位置,请将其设置为false。

animation.setFillAfter(false);

答案 1 :(得分:0)

如果有人想知道答案

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

                    }
                });

            }
        });
    }