gpt4 book ai didi

java - 同时淡入和淡出

转载 作者:太空宇宙 更新时间:2023-11-04 12:09:01 25 4
gpt4 key购买 nike

我正在使用这部分代码来创建动画:

private void animate(final ImageView imageView, final Drawable[] images, final int imageIndex,
final boolean forever) {

//imageView <-- The View which displays the images
//images[] <-- Holds R references to the images to display
//imageIndex <-- index of the first image to show in images[]
//forever <-- If equals true then after the last image it starts all over again with the
// first image resulting in an infinite loop. You have been warned.

int fadeInDuration = 1000; // Configure time values here
int timeBetween = 300;
int fadeOutDuration = 1000;

imageView.setVisibility(View.VISIBLE); //Visible or invisible by default -
// this will apply when the animation ends
imageView.setImageDrawable(images[imageIndex]);

Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);

Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
fadeOut.setStartOffset(fadeInDuration + timeBetween);
fadeOut.setDuration(fadeOutDuration);

AnimationSet animation = new AnimationSet(false); // change to false
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
animation.setRepeatCount(1);
imageView.setAnimation(animation);

animation.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationEnd(Animation animation) {
if (images.length - 1 > imageIndex) {
animate(imageView, images, imageIndex + 1, forever); //Calls itself until it gets to the end of the array
} else {
if (forever) {
animate(imageView, images, 0, forever); //Calls itself to start the animation all
// over again in a loop if forever = true
}
}
}

public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}

public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
});
}

那段代码有效,问题是淡入和淡出之间没有任何东西可以让您看到这些图片(白屏)下的内容。我希望在淡出运行时开始淡入,这样您就不会看到白色背景,而总是看到图片。我尝试将 timeBetween 值更改为零(甚至负数),这应该会更改第二个动画的偏移量,这将导致动画同时开始但没有成功 - 它只会使图像消失得更快,但仍然不会达到所请求的效果(一起淡入和淡出)

谁能解释一下如何才能同时激活淡入和淡出

最佳答案

我认为最好的方法是拥有两个 ImageView,它们都位于同一个 FrameLayout 内,以便它们重叠(也可以使用 RelativeLayout 来完成)。

顶部ImageView获取图像1。

底部ImageView获取图像2。

将顶部 ImageView alpha 从 1 动画到 0。

随着顶部 ImageView 淡出,底部 ImageView 变得可见。

动画结束后,将底部图像切换到顶部并将 alpha 设置回 1。

在底部加载新图像

重复动画

关于java - 同时淡入和淡出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40049781/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com