使用淡入淡出动画更改图像
changing images with fade animation
我需要创建要显示 3 张图像的动画。每个图像都应完全淡入和淡出(缓慢闪烁),然后应显示下一张图像。我通过创建 3 个动画来完成它。每个动画在 onAnimationEnd 侦听器中启动下一个动画:
private void startAnimation() {
final Animation aniIn = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn1 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn2 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
aniIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom0.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom0.setVisibility(View.INVISIBLE);
ivRandom1.startAnimation(aniIn1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom1.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom1.setVisibility(View.INVISIBLE);
ivRandom2.startAnimation(aniIn2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom2.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
showFinal();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
ivRandom0.startAnimation(aniIn);
}
对于那个动画,我有这个 xml 它将 alhpa 从 0 设置为 1 再回到 0:
fade_in_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="300"
android:fromAlpha="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toAlpha="1"
android:repeatCount="1"
android:repeatMode="reverse"
/>
</set>
这是我的布局:
<RelativeLayout
android:id="@+id/images_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_image_random_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator0"
android:visibility="visible" />
<ImageView
android:id="@+id/iv_image_random_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator1"
android:visibility="invisible" />
<ImageView
android:id="@+id/iv_image_random_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator2"
android:visibility="invisible" />
</RelativeLayout>
虽然这很有效,但我不喜欢嵌套的回调。它还会导致内存泄漏,因为它在回调中持有对图像视图的引用。
有没有更好的方法来制作这种动画?或者至少避免内存泄漏?我正在尝试 imageswitcher,但它会导致我不想要的图像交叉淡入淡出。
谢谢!
你可以使用ObjectAnimator,它有setDuration和start delays的方法,你可以根据需要计算。
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha",0, 1);
objectAnimator.setDuration(DEFAULT_ANIMATION_DURATION);
objectAnimator.setStartDelay(milliseconds);
objectAnimator.start();
您可以创建三个不同的对象动画师来设置相应的。更多 refer this link
您可以使用视图切换器来实现此行为。请看下面的代码:
布局
<ViewSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inAnimation="@anim/fade_in"
android:outAnimation="@anim/fade_out" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/sunset" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/clouds" />
</ViewSwitcher>
代码
((ViewSwitcher) findViewById(R.id.switcher)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewSwitcher switcher = (ViewSwitcher) v;
if (switcher.getDisplayedChild() == 0) {
switcher.showNext();
} else {
switcher.showPrevious();
}
}
});
请参考这个link
我需要创建要显示 3 张图像的动画。每个图像都应完全淡入和淡出(缓慢闪烁),然后应显示下一张图像。我通过创建 3 个动画来完成它。每个动画在 onAnimationEnd 侦听器中启动下一个动画:
private void startAnimation() {
final Animation aniIn = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn1 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
final Animation aniIn2 = AnimationUtils.loadAnimation(this,
R.anim.fade_in_out);
aniIn.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom0.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom0.setVisibility(View.INVISIBLE);
ivRandom1.startAnimation(aniIn1);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn1.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom1.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
ivRandom1.setVisibility(View.INVISIBLE);
ivRandom2.startAnimation(aniIn2);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
aniIn2.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
ivRandom2.setVisibility(View.VISIBLE);
}
@Override
public void onAnimationEnd(Animation animation) {
showFinal();
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
ivRandom0.startAnimation(aniIn);
}
对于那个动画,我有这个 xml 它将 alhpa 从 0 设置为 1 再回到 0:
fade_in_out.xml:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<alpha
android:duration="300"
android:fromAlpha="0"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:toAlpha="1"
android:repeatCount="1"
android:repeatMode="reverse"
/>
</set>
这是我的布局:
<RelativeLayout
android:id="@+id/images_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="@+id/iv_image_random_0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator0"
android:visibility="visible" />
<ImageView
android:id="@+id/iv_image_random_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator1"
android:visibility="invisible" />
<ImageView
android:id="@+id/iv_image_random_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/img_generator2"
android:visibility="invisible" />
</RelativeLayout>
虽然这很有效,但我不喜欢嵌套的回调。它还会导致内存泄漏,因为它在回调中持有对图像视图的引用。
有没有更好的方法来制作这种动画?或者至少避免内存泄漏?我正在尝试 imageswitcher,但它会导致我不想要的图像交叉淡入淡出。
谢谢!
你可以使用ObjectAnimator,它有setDuration和start delays的方法,你可以根据需要计算。
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha",0, 1);
objectAnimator.setDuration(DEFAULT_ANIMATION_DURATION);
objectAnimator.setStartDelay(milliseconds);
objectAnimator.start();
您可以创建三个不同的对象动画师来设置相应的。更多 refer this link
您可以使用视图切换器来实现此行为。请看下面的代码:
布局
<ViewSwitcher
android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:inAnimation="@anim/fade_in"
android:outAnimation="@anim/fade_out" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/sunset" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:src="@drawable/clouds" />
</ViewSwitcher>
代码
((ViewSwitcher) findViewById(R.id.switcher)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
ViewSwitcher switcher = (ViewSwitcher) v;
if (switcher.getDisplayedChild() == 0) {
switcher.showNext();
} else {
switcher.showPrevious();
}
}
});
请参考这个link