Animation set for image view - android

I want multiple animations to be played on the same ImageView. I'm using an animation set but it never fades in. But it does rotate. Can somebody tell me what I'm missing ?
AnimationSet s = new AnimationSet(false);//false mean dont share interpolators
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f , Animation.RELATIVE_TO_SELF, .5f, Animation.RELATIVE_TO_SELF, .5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(800);
s.addAnimation(fadeIn);
s.addAnimation(anim);
iv.startAnimation(s);

You are missing:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator()); // add this
fadeIn.setDuration(fadeInDuration);
fadeIn.setFillEnabled(true); // to apply setFillBefore and setFillAfter
fadeIn.setFillBefore(true); // it means that at start of animation you set alpha="0"
fadeIn.setFillAfter(false);
iv.setAnimation(fadeIn);
iv.startAnimation(fadeIn);
Also, in your styles you must remove alpha="0"
AlphaAnimation is working on your image as it is. So if you set alpha="0" it works between this 0. If alpha="1" or without it (when style default is alpha="1"), it works between 0-1.

Related

Fade in and out an image constantly in Android app

I'm trying to make an image fade in and out constantly, but it just fade in and out one time.
How can I make it repeat constantly?
Here's the code:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(true);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
ImageView loading = (ImageView)findViewById(R.id.loading);
loading.startAnimation(animation);
With Animator, it's pretty easy :
Animator alphaAnimator = ObjectAnimator.ofFloat(loading, View.ALPHA, 0f, 1f);
alphaAnimator.setDuration(1000);
alphaAnimator.setRepeatMode(ValueAnimator.REVERSE);
alphaAnimator.setRepeatCount(ValueAnimator.INFINITE);
alphaAnimator.start();
You should repeat your animation:
animation.setRepeatCount(Animation.INFINITE);
Try to use the method setRepeatMode(int repeatMode) from Animation class.

How to change animation speed when is playing?

I have Animation and i need to change duration or speed in run.
How to do this?
Animation fadeIn = new AlphaAnimation(1, 0);
fadeIn.setInterpolator(new AccelerateInterpolator()); //add this
fadeIn.setDuration(800);
fadeIn.setFillEnabled(true);
fadeIn.setFillAfter(true);
bt1.startAnimation(fadeIn);
You can use Interpolators to have a variable speed during animation. Example can be found here.
I think you need two animations with different speeds. After the first has finished, the second gets played.
Animation anim1 = new AlphaAnimation (1, 0.5F);
anim1.setDuration (600); //The slow part.
//set other things for anim1
Animation anim2 = new AlphaAnimation (0.5F, 0);
anim2.setDuration (200); //The fast part.
//set other things for anim2

Android Fade in Fade out Together

I am trying to make blinking image button.
I was thinking about to use fade-in and fade-out , but I cant use it together at 1 Image Button. Following code blinks only once:
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setInterpolator(new DecelerateInterpolator());
fadeIn.setDuration(1000);
Animation fadeOut = new AlphaAnimation(1, 0);
fadeOut.setInterpolator(new AccelerateInterpolator());
fadeOut.setStartOffset(1000);
fadeOut.setDuration(1000);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(fadeIn);
animation.addAnimation(fadeOut);
this.setAnimation(animation);`
Just add this:
AnimationSet animation = new AnimationSet(false);
...
animation.setRepeatCount(Animation.INFINITE);
this.setAnimation(animation);
Obviously, adjust the count as needed.

Shading sub Layout in android

I am stuck in a condition that I can't keep the sub layout faded after using the fade animation
FrameLayout mapLayout = (FrameLayout)findViewById(R.id.mapLayout);
Animation fadeAnim = AnimationUtils.loadAnimation(this, android.R.anim.fade_out);
mapLayout.startAnimation(fadeAnim);
Please guide how can I achieve this in android. This animation and shaded effect occurs when I click the 'Pause' Button and to reverse the effect I will press the 'Resume' Button(Sorry I made the image with 'Pause' button in shaded view, in actual it is 'Resume')
Try
fadeAnim.fillAfter(true);
fadeAnim.execute();
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
hii makki you can set the below code at your onclick event
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(50);
set.addAnimation(animation);
animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f,Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, -1.0f,Animation.RELATIVE_TO_SELF, 0.0f
);
animation.setDuration(100);
set.addAnimation(animation);
LayoutAnimationController controller = new LayoutAnimationController(set, 0.5f);
l.setLayoutAnimation(controller);
then fade out animation
public static Animation runFadeOutAnimationOn(Activity ctx, View target) {
Animation animation = AnimationUtils.loadAnimation(ctx,android.R.anim.fade_out);
target.startAnimation(animation);
return animation;
}
Both above answers helped me in solving the issue, but both answers weren't the actual solution to my problem. The solution I made is
I defined two animations according to my need
Animation fadeOut = new AlphaAnimation(0f,0.5f);
Animation fadeIn = new AlphaAnimation(0.5f,1f);
fadeOut.setFillAfter(true);
fadeIn.setFillAfter(true);
When I clicked 'Pause' button I executed following line
mapLayout.startAnimation(fadeOut);
Then to resume from paused state, I executed following line
mapLayout.startAnimation(fadeIn);
Thanks Simon and Deepak!

set the animation to the activity without xml file

Hi I want to set the animation to an activity without xml file.Please give me some suggestions on this topic.Thanks in advance
AnimationSet myAnimation = new AnimationSet(true);
// Create a translate animation
/* TranslateAnimation animation=new TranslateAnimation(0,0,237,0);
animation.setDuration(250);
// Add each animation to the set
myAnimation.addAnimation(animation);*/
ScaleAnimation scale = new ScaleAnimation((float)0.5, (float)1, (float)0.5, (float)1);
scale.setFillAfter(true);
scale.setDuration(500);
<activity name>.startAnimation(scale);
private Animation createAnimation() {
final Animation animation = new AlphaAnimation(1, 0);
animation.setDuration(1000);
animation.setInterpolator(new AccelerateDecelerateInterpolator());
return animation;
}
Simple alpha animation, the way to create others is pretty much the same.
Use view.startAnimation(yourAnimation) to start animation, and yourAnimation.reset() before reusing the same animation again.
P.S. Maybe I misunderstood you, and you want an animation between activity changes?

Categories

Resources