1

我有二十张图像,并且一个接一个地进行过渡。转换与定时器一起在以下循环中实现。

    final Runnable mUpdateResults = new Runnable() {
        public void run() {
            AnimateandSlideShow();
        }
    };

    final int delay = 2000; 
    final long period = 2000;
    final Timer timer = new Timer();
    final Handler mHandler = new Handler();

    timer.scheduleAtFixedRate(new TimerTask() {
        public void run() {
             mHandler.post(mUpdateResults);
        }
    }, delay, period);

    private void AnimateandSlideShow() {
        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), IMAGE_IDS[currentimageindex%IMAGE_IDS.length]);
        slidingimage = (ImageView)findViewById(R.id.mainImageView);
        int imViewheight = 400;
        int imViewwidth = 600;
        Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imViewwidth, imViewheight, true);              
        slidingimage.setImageBitmap(convertColorIntoBlackAndWhiteImage(scaledBitmap));
        currentimageindex++; 

        slidingimage.startAnimation(fadeInAnimation);
        slidingimage.startAnimation(fadeOutAnimation);
    }

我喜欢做过渡淡入,显示 1000 毫秒,淡出。它被实现为

    Animation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
    Animation fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);
    fadeInAnimation.setDuration(2000);
    fadeOutAnimation.setDuration(2000);

现在发生的只是淡入或淡出。只有其中之一。如何使图像先淡入,然后显示 1000 毫秒并淡出。

4

2 回答 2

3

我可以使用AnimationSet来实现它,如下所示:

animationSet.addAnimation(fadeInAnimation);
animationSet.addAnimation(fadeOutAnimation);
fadeInAnimation.setDuration(2000);
fadeInAnimation.setStartOffset(0);
fadeOutAnimation.setDuration(2000);
fadeOutAnimation.setStartOffset(2000 + 1000);

slidingImage.startAnimation(animationSet);

看起来当前计时器逻辑对于您要完成的任务有点复杂。我建议依靠动画来计时(下面只是一个草稿代码):

ImageView slidingImage;
final static int[] IMAGE_IDS = new int[] {
        // some ids
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
        R.drawable.ic_launcher,
};

int currentImageIndex = 0;

final Animation.AnimationListener animationListener = new Animation.AnimationListener() {
    @Override
    public void onAnimationStart(final Animation animation) {
        // nothing to do here
    }

    @Override
    public void onAnimationEnd(final Animation animation) {
        // launch showing of next image on the end of the animation
        animateAndSlideShow();
    }

    @Override
    public void onAnimationRepeat(final Animation animation) {
        // nothing to do here
    }
};

private void animateAndSlideShow() {
    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), IMAGE_IDS[currentImageIndex % IMAGE_IDS.length]);

    slidingImage = (ImageView)findViewById(R.id.mainImageView);
    int imViewheight = 400;
    int imViewwidth = 600;
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, imViewwidth, imViewheight, true);
    //slidingImage.setImageBitmap(convertColorIntoBlackAndWhiteImage(scaledBitmap));
    slidingImage.setImageBitmap(bitmap);
    currentImageIndex++;

    // TODO: it's not needed to create new AnimationSet every time.
    final AnimationSet animationSet = new AnimationSet(true);
    final Animation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
    final Animation fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);

    animationSet.addAnimation(fadeInAnimation);
    animationSet.addAnimation(fadeOutAnimation);
    fadeInAnimation.setDuration(2000);
    fadeInAnimation.setStartOffset(0);
    fadeOutAnimation.setDuration(2000);
    fadeOutAnimation.setStartOffset(2000 + 1000);

    animationSet.setAnimationListener(animationListener);
    slidingImage.startAnimation(animationSet);
}
于 2013-06-30T06:16:28.370 回答
0

尝试这样

淡入淡出.xml

<?xml version="1.0" encoding="UTF-8"?>
       <set xmlns:android="http://schemas.android.com/apk/res/android">
         <alpha android:fromAlpha="0.0" android:toAlpha="1.0" 
          android:interpolator="@android:anim/accelerate_interpolator" 
          android:duration="2000"/>
     </set>

淡出.xml

<?xml version="1.0" encoding="UTF-8"?>
       <set xmlns:android="http://schemas.android.com/apk/res/android">
         <alpha android:fromAlpha="1.0" android:toAlpha="0.0" 
          android:interpolator="@android:anim/accelerate_interpolator" 
          android:duration="2000"/>
     </set>

并像这样加载动画

    Animation fadeInAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_in_anim);
   Animation fadeoutAnimation = AnimationUtils.loadAnimation(this, R.anim.your_fade_out_anim);
   imageView.startAnimation(fadeinAnim);
    imageView.startAnimation(fadeoutAnim);
于 2013-06-30T06:19:58.470 回答