Blink animation on TextView with text changing simultaneously - android

I have an array of Strings and want to display one String at a time with blink animation (after a blink another String in textView). I did the animation part with the code below:
On OnCreate:
TextView subTitle=findViewById(R.id.subTitle);
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(2500);
anim.setStartOffset(0);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
subTitle.startAnimation(anim);
and changing the textview text with this:
Handler h = new Handler();
int delay = 2500;
Runnable runnable;
#Override
protected void onResume() {
h.postDelayed(runnable = new Runnable() {
public void run() {
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
h.postDelayed(runnable, delay);
}
}, delay);
super.onResume();
}
But the text of textView does not change simultaneously with blink. Is there a single way I can do this?

You can do like below...
anim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//if set animation count to once
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
anim.start();
}
#Override
public void onAnimationRepeat(Animation animation) {
// Or if set repeat count to infinite
selected = subTtls[new Random().nextInt(subTtls.length)];
subTitle.setText(selected);
}
});
Modify as you want...

Related

Continue the program after the end of the animation android studio

I made an animation (ObjectAnimator)for a text view it work correctly but i have a question.
I wanna know is there any method that i can use immediately after animation duration finished ?
(I mean a method like onFinish when we use CountDownTimer)
I want the rest of methods and codes I have in app run when animation finished.Is there a solution for that?
ObjectAnimator deltaXAnimation = ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
ObjectAnimator deltaYAnimation = ObjectAnimator.ofFloat(winMassage,"scaleY",1f,1.5f);
deltaYAnimation.setDuration(300);
deltaYAnimation.start();
HI there are proper callback methods in which you get Animation Start and end Listeners
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
deltaXAnimation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
Below code might be understand well, creating a listener for ObjectAnimator
ObjectAnimator deltaXAnimation =
ObjectAnimator.ofFloat(winMassage,"scaleX",1f,1.5f);
deltaXAnimation.setDuration(300);
deltaXAnimation.start();
anim.setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation a) {
//
}
public void onAnimationRepeat(Animation a) {
}
public void onAnimationEnd(Animation a) {
}
});
We can go also to your ObjectAnimator
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//stop animation after 5 seconds
deltaXAnimation.cancel();
}
}, 5000); //5000 equals to 5seconds
}
Hope this might help you

How to start a function when animation has ended?

I have two functions that animate two different TextViews but they both seem to start at the same time but I am trying to have the animation of the first function finish first and then start the second function.
public Boolean functionFinished = false;
public void runFunction(){
firstFunction();
if(functionFinished = true){
secondFunction();
}
}
public void firstFunction(){
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
initialCount.startAnimation(out);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
if(initialCount.getText().equals("3")){
initialCount.setText("2");
initialCount.startAnimation(out);
} else if(initialCount.getText().equals("2")){
initialCount.setText("1");
initialCount.startAnimation(out);
} else if (initialCount.getText().equals("1")){
initialCount.setText("START!");
}
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
functionFinished = true;
}
The second function simply changes its own textView every second counting up from 0.
What did I do wrong / how do I correct this so that the second function runs after the first function has finished? (ie. sets functionFinished to true only when the TextView from the firstFunction is "START!")
public void runFunction() {
firstFunction();
}
public void firstFunction() {
initialCount = (TextView) findViewById(R.id.textView_Fade);
initialCount.setText("3");
final Animation out = new AlphaAnimation(1.0f, 0.0f);
out.setDuration(1000);
out.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
...
secondFunction();
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
initialCount.startAnimation(out);
}
Just call second function from onAnimationEnd. Also keep in mind that you should attach listener before start animation.
If your minimum sdk version is 14:-
textView1.animate()
.alpha(0f)
.setDuration(400)
.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
// animate second textview here....
}
});
Because you assign functionFinished = true at the end of the firstFunction().
And you must misunderstand that initialCount.startAnimation(out) will block the code and firstFunction() completes only after animation ends .
initialCount.startAnimation(out) will not block any code ,and functionFinished = true is run immediately. so secondFunction() will run immediately after firstFunction() finishes instead of animation finishes.

TextView keeps popping up

I have an activity where an TextView show up, and after that it fades out. However, everytime it has fade out, it pops back in. I tried
wtext.setText("");
But then my text just disappeares without fading out(after fading in). Does anyone knows how to fix this? Thanks! Kind regards.
This is my code:
public class SplashScreen extends Activity {
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
final TextView wtext = (TextView) (findViewById(R.id.wtext));
Animation animation = AnimationUtils.loadAnimation(this, R.anim.abc_fade_in);
wtext.startAnimation(animation);
Runnable task = new Runnable() {
#Override
public void run() {
Animation animation1 = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.abc_fade_out);
wtext.startAnimation(animation1);
wtext.setText("");
}
};
handler.postDelayed(task, 5000);
}
If you want something to happen after an animation is complete (in this case, hide a textview), you'll have to use an AnimationListener
Animation.AnimationListener myListener = new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
wtext.setVisibility(View.INVISIBLE);
}
};
animation1.setAnimationListener(myListener);
You could use a ViewPropertyAnimator, which will change the view's actual properties rather than just its appearance, so that it will not jump back to the original state after the animation is complete.
wtext.animate().alpha(0);
You could try setting fillAfter flag to true on the element: it will apply the final transformation to it after the animation is finished.

Animation doesn't get called inside postDelayed run()

I have a linearlayout that I want it to be animated after 3 seconds delay using Handler.
After 3 seconds have passed, it doesn't even execute the animation, nor did it enter the AnimationListener's methods.
Here is how I do it:
loginBox.setVisibility(View.GONE);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Animation animTranslate = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.translate);
animTranslate.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
loginBox.setVisibility(View.VISIBLE);
Animation animFade = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade);
loginBox.startAnimation(animFade);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
btnContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
finish();
}
});
}
}, 3000);
The run() method works fine when I click btnContinue.
How can I make it work?
you forgot to call
loginBox.startAnimation(animTranslate)
and probably you want loginBox.setVisibility(View.VISIBLE); before starting the TranslateAnimation

Delay while restart of animation

I'm using value animator to animate a image view. I want to add a delay in Animation listener, i.e in "onAnimationRepeat()". How to accomplish this?
Maybe try something like this:
public class Test {
private int repeatNumber == 0;
public void startAnimation() {
Animation animation = AnimationUtils.loadAnimation(this, R.anim.fade_in);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {Log.i("start", "start");}
#Override
public void onAnimationEnd(Animation animation) {Log.i("end", "end");}
#Override
public void onAnimationRepeat(Animation animation) {
Test.this.repeatNumber++;
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Test.this.startAnimation();
}, 30000 );
}
textView.startAnimation(animation);
}
}

Categories

Resources