Use one animation object for multi views - android

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);

Related

Repeating FadeOut on TextView

I have a TextView that acts as a warning label when a user tries to do something they shouldn't. I want it to blink a few times before staying there at the end.
I have blinking once working fine, but it will not repeat the animation. Is there something wrong with my animation?(because i have read the other questions about android animation and reportedly they work)
private void blink(int count){
if(count>0) {
AnimationSet anime = new AnimationSet(true);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(600);
Animation ani = new AlphaAnimation(1.0f, 0.0f);
ani.setDuration(600);
ani.setStartOffset(2000);
anime.addAnimation(anim);
anime.addAnimation(ani);
//anime.setStartOffset(0);
//anime.setStartTime(0);
//anime.setRepeatCount(Animation.INFINITE);
anime.setRepeatCount(count);
anime.setRepeatMode(Animation.RESTART);
anime.setFillAfter(true);
tWarningText.startAnimation(anime);
}
}
clearAnimation is called when some buttons are pressed, because tWarningText is, well, a warning text, I want it to stay there until they do something about it.
Instead of clearing animation after definite time, you can limit repeat count. This should also work fine.
public void blink(View view, int count) {
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50);
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(count);
view.startAnimation(anim);
}
Usage:
blink(textView, 10);
referred from sany's answer.

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

How to apply animations to entire Android Listview

I am trying to have a Listview fade out/in under certain circumstances. I'm running into issues when running the animation, the Listview just becomes invisible and no animation is performed.
I've tried running the fade_out animation:
Animation out = AnimationUtils.loadAnimation(mainActivity, R.anim.fade_out);
out.setInterpolator(new LinearInterpolator());
myListView.startAnimation(out);
As well as an AlphaAnimation
AlphaAnimation animation1 = new AlphaAnimation(1f, .5f);
animation1.setDuration(2000);
animation1.setFillAfter(false);
myListView.startAnimation(animation1)
and finally simply setting the alpha
myListView.setAlpha(.5f)
The best I can tell is that alpha transparencies are not supported with a list view and only values of 0 and 1 are available as alpha. I can't find any documentation confirming or denying either way.
Is there some kind of known issue with ListViews? Am I doing something wrong? Or is this some kind of limited functionality on ListViews?
Just try it's.If you want to do visible :
public void viseb_show(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(0.0f, 1.0f);
animation.setDuration(500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.VISIBLE);
}
or if non visible
public void viseb_gone(){
AnimationSet set = new AnimationSet(true);
Animation animation = new AlphaAnimation(1.0f, 0.0f);
animation.setDuration(3500);
set.addAnimation(animation);
anim.startAnimation(animation);
anim.setVisibility(View.GONE);
}
anim it's you object who mast be visible.
I figured out my issue. emigrantdd sent me in almost the right direction, how ever there were a few problems I needed to overcome.
First I had to alter emigrantdd's code to:
public void viseb_gone()
{
AnimationSet set = new AnimationSet(false);
Animation fadeout= new AlphaAnimation(1.0f, 0.0f);
fadeOut.setInterpolator(new LinearInterpolator());
fadeOut.setDuration(3500);
set.addAnimation(animation);
myListView.startAnimation(fadeOut);
myListView.setVisibility(View.GONE);
}
As there was some problem with the interpolator.
The other issue I had was that the ListView I was testing on was still in a ScrollView as a part of an old design, once it was taken out it worked as was intended with the changes above.

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