Creating animation on ImageView while changing image resource
For a single imageview you can use this helper function:
public static void ImageViewAnimatedChange(Context c, final ImageView v, final Bitmap new_image) {
final Animation anim_out = AnimationUtils.loadAnimation(c, android.R.anim.fade_out);
final Animation anim_in = AnimationUtils.loadAnimation(c, android.R.anim.fade_in);
anim_out.setAnimationListener(new AnimationListener()
{
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
@Override public void onAnimationEnd(Animation animation)
{
v.setImageBitmap(new_image);
anim_in.setAnimationListener(new AnimationListener() {
@Override public void onAnimationStart(Animation animation) {}
@Override public void onAnimationRepeat(Animation animation) {}
@Override public void onAnimationEnd(Animation animation) {}
});
v.startAnimation(anim_in);
}
});
v.startAnimation(anim_out);
}
changing images with fade animation
you can use ObjectAnimator , it has methods to setDuration and start delays , which you can calculate according to your need.
ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(imageView, "alpha",0, 1);
objectAnimator.setDuration(DEFAULT_ANIMATION_DURATION);
objectAnimator.setStartDelay(milliseconds);
objectAnimator.start();
you can create three different object animators to set accordinglt . for more refer this link
how to Set Android animation on setting new Image Resource
You have to import:
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
private Animation animFade;
animFade = AnimationUtils.loadAnimation(this, R.anim.fadeout);
You can have the onClickListener for the button and add the following code:
animFade.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {
// when fadeout animation ends, fade in your second image
}
});
yourfirstimage.startAnimation(animFade);
Create an XML file with the animation in the res/anim
(fadeout.xml for example):
<?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="3000" android:repeatCount="0"/>
</set>
Hope this code gives you an understanding how animation works. Let me know if you need more clarification.
Note: Always do Animation outside of the UI Thread.
Happy coding.
Imageview: image change animation
You can use frame animation , it is simple to use example like this.
spin_animation.xml file in res/drawable/ folder:
<animation-list android:id="@+id/selected" android:oneshot="false">
<item android:drawable="@drawable/wheel0" android:duration="50" />
<item android:drawable="@drawable/wheel1" android:duration="50" />
<item android:drawable="@drawable/wheel2" android:duration="50" />
<item android:drawable="@drawable/wheel3" android:duration="50" />
<item android:drawable="@drawable/wheel4" android:duration="50" />
<item android:drawable="@drawable/wheel5" android:duration="50" />
</animation-list>
Here is the code to load and play this animation.
// Load the ImageView that will host the animation and
// set its background to our AnimationDrawable XML resource.
ImageView img = (ImageView)findViewById(R.id.spinning_wheel_image);
img.setBackgroundResource(R.drawable.spin_animation);
// Get the background, which has been compiled to an AnimationDrawable object.
AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
// Start the animation (looped playback by default).
frameAnimation.start();
Android ImageView switch resources with animation
Finally used a ViewFlipper to switch between the two "images".
<ViewFlipper
android:id="@+id/view_flipper"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/background_level"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/background_level_1" />
<ImageView
android:id="@+id/background_level_flip1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@drawable/background_level_2" />
</ViewFlipper>
How to make animation of image change for ImageView from Java code
You can programmatically create a TransitionDrawable
using the class' constructor. You don't need to acquire it from XML. This gives you the flexibility of dynamically assigning the Drawables it transitions between.
// drawable1 and drawable2 can be dynamically assigned in your Java code
Drawables[] drawables = new Drawables[] {
drawable1, drawable2
};
mTransition = new TransitionDrawable(drawables);
Change ImageView image during animation
I think you should try doing this way:
i.startAnimation(anim);
if (anim.hasEnded())
{
i.setImageResource(R.drawable.logoc);
i.startAnimation(animo);
}
Edit:
Ok, I was wrong: This works:
public class MyAnimationListener implements AnimationListener {
@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub
i.setImageResource(R.drawable.bg);
i.startAnimation(animo);
}
@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
}
@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
}
}
MyAnimationListener mListener = new MyAnimationListener();
anim.setAnimationListener(mListener);
i.startAnimation(anim);
Related Topics
Add Margin Between a Radiobutton and Its Label in Android
Imageview Adjustviewbounds Not Working
Access Ordered Images and Video in Same Cursor
Stop Scrollview from Auto-Scrolling to an Edittext
How to Know If Android Talkback Is Active
Error "Package Android.Support.V7.App Does Not Exist"
Recyclerview Smoothscroll to Position in The Center. Android
How to Get The Application's Icon from The Package Name
How to Make Part of The Text Bold in Android at Runtime
Db File in Assets Folder. Will It Be Updated
Android: Runonuithread Vs Asynctask