图像动画在导航到选项卡并返回动画选项卡后表现不同

时间:2015-12-29 11:43:55

标签: android

我有一种在imageview图像中设置动画的方法

我想在一段时间后改变图像,并希望为奇数偶数图像位置设置不同的动画

// **** set Animation and image in imageview *****
private void setAnimation() {
     handler = new Handler();


    Runnable runnable = new Runnable() {


        public void run() {

                imgView.setImageResource(setImg[i]);

                if (i % 2 == 0) {
                    animation = AnimationUtils.loadAnimation(context, R.anim.zoom_in_fade_out);
                    imgView.startAnimation(animation);
                    i++;
                } else {
                    animation = AnimationUtils.loadAnimation(context, R.anim.zoom_out_fade_in);
                    imgView.startAnimation(animation);
                    i++;
                }


                if (i > setImg.length - 1) {
                    i = 0;
                }

handler.postDelayed(this,8000); //为间隔...             }

    };
   handler.postDelayed(runnable,10); //for initial delay..


}

它工作得很好但是当我导航到不同的标签一段时间并返回动画标签时它表现不同

**下面是我的动画xml **

<set xmlns:android="http://schemas.android.com/apk/res/android"

&GT;

<!-- ***** Set Zoom In Animation   *****-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="8000"
    android:fromXScale="1"
    android:fromYScale="1"
    android:pivotX="50%"
    android:pivotY="50%"
    android:startOffset="500"
    android:toXScale="1.2"
    android:toYScale="1.2"/>

<!-- ***** Set Fade Out Animation   *****-->

<alpha
    android:duration="1000"
    android:fromAlpha="1"
    android:startOffset="500"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="0.8" />

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"

&GT;

<!-- ***** Set Fade In Animation   *****-->

<alpha
    android:duration="1000"
    android:fromAlpha="0.8"
    android:startOffset="500"
    android:interpolator="@android:anim/accelerate_interpolator"
    android:toAlpha="1" />

<!-- ***** Set Zoom Out Animation   *****-->
<scale xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="8000"
    android:fromXScale="1.2"
    android:fromYScale="1.2"
    android:pivotX="50%"
    android:pivotY="50%"
    android:startOffset="500"
    android:toXScale="1"
    android:toYScale="1"/>

1 个答案:

答案 0 :(得分:0)

我解决了我遇到的问题。我改变了代码,如下面

  @Override
public void onResume() {
    super.onResume();
    Log.i("TAG", "HOME RESUME");
    startTimer();


}
 @Override
public void onDestroyView() {
    super.onDestroyView();
    stopTimerTask();
}

 @Override
public void onPause() {
    super.onPause();
    Log.i("TAG", "HOME PAUSE");stopTimerTask(); 

}
  public void startTimer() {
    //set a new Timer
    timer = new Timer();
    //initialize the TimerTask's job
    initializeTimerTask();
    //schedule the timer, after the first 0ms the TimerTask will run every 8000ms

    timer.schedule(timerTask, 0, 8000); //
}

public void stopTimerTask() {

    //stop the timer, if it's not already null
    if (timer != null) {
        timer.cancel();

        timer = null;
    }

}
public void initializeTimerTask() {

    timerTask = new TimerTask() {
        @Override
        public void run() {
            handler.post(new Runnable() {

                public void run() {

                    final Animation animation;
                    imgView.setImageResource(setImg[i]);

                    if (i % 2 == 0) {
                        animation = AnimationUtils.loadAnimation(getActivity().getBaseContext(), R.anim.zoom_in_fade_out);
                        imgView.startAnimation(animation);

                    } else {
                        animation = AnimationUtils.loadAnimation(getActivity().getBaseContext(),R.anim.zoom_out_fade_in);
                        imgView.startAnimation(animation);

                    }
                    i++;
                    if (i >= setImg.length) {
                        i = 0;
                    }

                }

            });

        }

    };


}