Java 淡入&;同时淡出
我正在使用这部分代码创建动画:Java 淡入&;同时淡出,java,android,android-animation,Java,Android,Android Animation,我正在使用这部分代码创建动画: private void animate(final ImageView imageView, final Drawable[] images, final int imageIndex, final boolean forever) { //imageView <-- The View which displays the images //images[] <--
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
}
});
}
private void animate(最终图像视图图像视图、最终可绘制[]图像、最终int-imageIndex、,
最终布尔值(永远){
//imageView我认为最好的方法是将两个imageView
放在同一个FrameLayout
内,以便它们重叠(也可以使用RelativeLayout
)
TopImageView
获取图像1
底部ImageView
获取图像2
将顶部ImageView
alpha从1设置为0
随着顶部ImageView
淡出,底部ImageView
变得可见
动画结束后,将底部图像切换到顶部,并将alpha设置回1
在底部加载新图像
重复动画我认为最好的方法是将两个图像视图
放在同一个帧布局
内,以便它们重叠(也可以使用相对视图
来完成)
TopImageView
获取图像1
底部ImageView
获取图像2
将顶部ImageView
alpha从1设置为0
随着顶部ImageView
淡出,底部ImageView
变得可见
动画结束后,将底部图像切换到顶部,并将alpha设置回1
在底部加载新图像
重复动画嗨,我在寻找解决方案,发现了这个问题
我在解决方案中使用了位图,但我相信您可以将其更改回去
使用2个ImageView时也是如此(未随函数传递)
这是我的解决方案:
private void animate(final Bitmap[] images, final int imageIndex, final boolean forever) {
final ImageView imageView0 = mImageSlideShow0;
final ImageView imageView1 = mImageSlideShow1;
imageView0.setVisibility(View.VISIBLE);
imageView1.setVisibility(View.VISIBLE);
AnimationSet animationIn = null;
AnimationSet animationOut = null;
//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 fadeDuration = 1000; // Configure time values here
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeDuration);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
fadeOut.setStartOffset( 2 * fadeDuration); //time that image is visible without any animations
fadeOut.setDuration(fadeDuration);
animationIn = new AnimationSet(false); // change to false
animationIn.addAnimation(fadeIn);
animationIn.setRepeatCount(1);
animationOut = new AnimationSet(false); // change to false
animationOut.addAnimation(fadeOut);
animationOut.setRepeatCount(1);
int animationInIndex = imageIndex;
int animationOutIndex = 0;
//if first image take last one in array
if(imageIndex != 0)
{
animationOutIndex = animationInIndex - 1;
}
else
{
animationOutIndex = images.length - 1;
}
//select the correct fade in / fade out view
if(mCurrentImageViewActive == 0)
{
//set correct image image view
imageView0.setImageBitmap(images[animationInIndex]);
imageView1.setImageBitmap(images[animationOutIndex]);
//set correct animation image view
imageView0.setAnimation(animationIn);
imageView1.setAnimation(animationOut);
//set next fade in view
mCurrentImageViewActive = 1;
}
else
{
imageView1.setImageBitmap(images[animationInIndex]);
imageView0.setImageBitmap(images[animationOutIndex]);
imageView1.setAnimation(animationIn);
imageView0.setAnimation(animationOut);
//set next fade in view
mCurrentImageViewActive = 0;
}
//if fade out is done call next action
animationOut.setAnimationListener(new Animation.AnimationListener()
{
public void onAnimationEnd(Animation animation)
{
if (images.length - 1 > imageIndex)
{
animate(images, imageIndex + 1,forever); //Calls itself until it gets to the end of the array
}
else
{
if (forever)
{
animate( 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
}
});
}
private void动画(最终位图[]图像、最终int-imageIndex、最终布尔值){
最终图像视图图像视图0=mImageSlideShow0;
最终图像视图图像视图1=mImageSlideShow1;
imageView0.setVisibility(View.VISIBLE);
imageView1.setVisibility(View.VISIBLE);
AnimationSet animationIn=null;
AnimationSet animationOut=null;
//images[]嗨,我在寻找解决方案,发现了这个问题
我在解决方案中使用了位图,但我相信您可以将其更改回去
使用2个ImageView时也是如此(未随函数传递)
这是我的解决方案:
private void animate(final Bitmap[] images, final int imageIndex, final boolean forever) {
final ImageView imageView0 = mImageSlideShow0;
final ImageView imageView1 = mImageSlideShow1;
imageView0.setVisibility(View.VISIBLE);
imageView1.setVisibility(View.VISIBLE);
AnimationSet animationIn = null;
AnimationSet animationOut = null;
//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 fadeDuration = 1000; // Configure time values here
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeDuration);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator()); // and this
fadeOut.setStartOffset( 2 * fadeDuration); //time that image is visible without any animations
fadeOut.setDuration(fadeDuration);
animationIn = new AnimationSet(false); // change to false
animationIn.addAnimation(fadeIn);
animationIn.setRepeatCount(1);
animationOut = new AnimationSet(false); // change to false
animationOut.addAnimation(fadeOut);
animationOut.setRepeatCount(1);
int animationInIndex = imageIndex;
int animationOutIndex = 0;
//if first image take last one in array
if(imageIndex != 0)
{
animationOutIndex = animationInIndex - 1;
}
else
{
animationOutIndex = images.length - 1;
}
//select the correct fade in / fade out view
if(mCurrentImageViewActive == 0)
{
//set correct image image view
imageView0.setImageBitmap(images[animationInIndex]);
imageView1.setImageBitmap(images[animationOutIndex]);
//set correct animation image view
imageView0.setAnimation(animationIn);
imageView1.setAnimation(animationOut);
//set next fade in view
mCurrentImageViewActive = 1;
}
else
{
imageView1.setImageBitmap(images[animationInIndex]);
imageView0.setImageBitmap(images[animationOutIndex]);
imageView1.setAnimation(animationIn);
imageView0.setAnimation(animationOut);
//set next fade in view
mCurrentImageViewActive = 0;
}
//if fade out is done call next action
animationOut.setAnimationListener(new Animation.AnimationListener()
{
public void onAnimationEnd(Animation animation)
{
if (images.length - 1 > imageIndex)
{
animate(images, imageIndex + 1,forever); //Calls itself until it gets to the end of the array
}
else
{
if (forever)
{
animate( 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
}
});
}
private void动画(最终位图[]图像、最终int-imageIndex、最终布尔值){
最终图像视图图像视图0=mImageSlideShow0;
最终图像视图图像视图1=mImageSlideShow1;
imageView0.setVisibility(View.VISIBLE);
imageView1.setVisibility(View.VISIBLE);
AnimationSet animationIn=null;
AnimationSet animationOut=null;
//images[]如果我有两个以上的ImageView,比如说10个呢?如果我有两个以上的ImageView,比如说10个呢?