Making a smooth fade out for imageview in android
Replace img.setVisibility(View.GONE) in your code with a call to fadeOutAndHideImage(img) which is defined like this:
private void fadeOutAndHideImage(final ImageView img)
{
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setDuration(1000);
fadeOut.setAnimationListener(new AnimationListener()
{
public void onAnimationEnd(Animation animation)
{
img.setVisibility(View.GONE);
}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationStart(Animation animation) {}
});
img.startAnimation(fadeOut);
}
It will apply the fade out animation first, then will hide the image view.
Android fade in and fade out with ImageView
To implement this the way you have started, you'll need to add an AnimationListener so that you can detect the beginning and ending of an animation. When onAnimationEnd() for the fade out is called, you can set the visibility of your ImageView object to View.INVISIBLE, switch the images and start your fade in animation - you'll need another AnimationListener here too. When you receive onAnimationEnd() for your fade in animation, set the ImageView to be View.VISIBLE and that should give you the effect you're looking for.
I've implemented a similar effect before, but I used a ViewSwitcher with 2 ImageViews rather than a single ImageView. You can set the "in" and "out" animations for the ViewSwitcher with your fade in and fade out so it can manage the AnimationListener implementation. Then all you need to do is alternate between the 2 ImageViews.
Edit:
To be a bit more useful, here is a quick example of how to use the ViewSwitcher. I have included the full source at https://github.com/aldryd/imageswitcher.
activity_main.xml
<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>
MainActivity.java
// Let the ViewSwitcher do the animation listening for you
((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();
}
}
});
Android - AlphaAnimation - FadeOut ImageView than FadeIn other ImageView
use glide.
u can animate change with it and do a lots of work with images
implementation 'com.github.bumptech.glide:glide:4.11.0'
DrawableCrossFadeFactory factory =
new DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build();
Glide.with(java.util.Objects.requireNonNull(getActivity()))
.load(R.drawable.logo)
.transition(DrawableTransitionOptions.withCrossFade(factory))
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(imageView)
use this code
val factory: DrawableCrossFadeFactory =
DrawableCrossFadeFactory.Builder().setCrossFadeEnabled(true).build()
if(error){
Glide.with(this).load(R.drawable.error)
.transition(DrawableTransitionOptions.withCrossFade(factory))
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(imageView)
}else{
Glide.with(this).load(R.drawable.logo)
.transition(DrawableTransitionOptions.withCrossFade(factory))
.skipMemoryCache(true)
.diskCacheStrategy(DiskCacheStrategy.RESOURCE)
.into(imageView)
}
How to do Fade in and Fade out in Image view by using Transitions on Android programmatically?
create two
Animation
Animation fadeOutAnimation = new AlphaAnimation(1.0f, 0.0f);
Animation fadeInAnimation = new AlphaAnimation(0.0f, 1.0f);set duration
fadeOutAnimation.setDuration(1000);
fadeInAnimation.setDuration(1000);set listener - when the fadeout animation finish replace the image
fadeOutAnimation.setAnimationListener(new AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationEnd(Animation animation) {
//here switch the images !
//and the begin the second animation FadeIn
}
});ImageView.startAnimation(fadeOutAnimation);
Zoom out & fade in of an imageView at the same time
Add the following to your xml zoomout:
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true">
<alpha
android:duration="1000"
android:fromAlpha="1.0"
android:startOffset="1"
android:toAlpha="0.0"/>
<scale
android:duration="1000"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale=".5"
android:toYScale=".5"/>
</set>
And remove the java code for the fade in animation
Animation fadeIn = new AlphaAnimation(1, 0);
fadeIn.setInterpolator(new AccelerateInterpolator());
fadeIn.setStartOffset(500);
fadeIn.setDuration(1000);
imageView.setAnimation(fadeIn);
Here is a reference http://thegeekyland.blogspot.com/2015/12/android-animations-explained.html
Related Topics
How to Use Getsystemservice in a Non-Activity Class (Locationmanager)
Device Not Detected in Eclipse When Connected with Usb Cable
Android: Programmatically Detect If Device Has Hardware Menu Button
Calling Setcontentview() Multiple Times
Ripple Effect on Android Lollipop Cardview
Move Markers in Google Map V2 Android
Android: How to Remove Margin/Padding in Preference Screen
Android: Determine Security Type of Wifi Networks in Range (Without Connecting to Them)
Multi Selection Spinner in Android Without Alertdialog
Android Fade in and Fade Out with Imageview
Android: How to Get a Radiogroup with Togglebuttons
Android: How to Select Texts from Webview
Android SQLite Issue - Table ... Has No Column Named
Download the Android Sdk Components for Offline Install
Android: Mediaplayer Setvolume Function