Android Fade in Fade out Together - android

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.

Related

Use one animation object for multi views

I want set alpha animation to views but with one object without create many object like this fadeIn1 , fadeIn2 , fadeIn3 etc
View1 , View2 , View3
Animation fadeIn = new AlphaAnimation(0, 1);
fadeIn.setStartOffset(1000);
fadeIn.setDuration(1000);
view1.startAnimation(fadeIn); //I want show this after 1 second
fadeIn.setStartOffset(1500);
view2.startAnimation(fadeIn); //I want show this after 1.5 second
fadeIn.setStartOffset(2000);
view3.startAnimation(fadeIn); //I want show this after 2 second
But all views shows after 2 second together , Why ?
Use AnimatiorSet for this
Note: The below code was in kotlin.
First create an function which returns an animator by taking view
private fun getAnimation(view: View): Animator {
return ObjectAnimator.ofFloat(view, "alpha", 0f, 1f).apply {
duration = 1000
}
}
Then after user AnimatorSet.playSequentially to play one after other.
AnimatorSet().apply {
playSequentially(listOf(getAnimation(btn_1),getAnimation(btn_2),getAnimation(btn_3)))
interpolator = AccelerateInterpolator()
start()
}
Please free to ask any query in comment section, I'll try to answer as soon as possible :)
You need AnimationSet ex:
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);
view.startAnimation(animation);

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

Animation set for image view

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.

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!

Categories

Resources