Android TextView object are visible before ObjectAnimator starts - android

I am trying to execute a simple animation using ObjectAnimator. Here is the source:
TextView[] introTextViews = new TextView[] {(TextView) findViewById(R.id.intro_textView1_1),
(TextView) findViewById(R.id.intro_textView1_2),
(TextView) findViewById(R.id.intro_textView2_1),
(TextView) findViewById(R.id.intro_textView2_2),
(TextView) findViewById(R.id.intro_textView3_1),
(TextView) findViewById(R.id.intro_textView3_2),
(TextView) findViewById(R.id.intro_textView4_1),
(TextView) findViewById(R.id.intro_textView4_2)};
AnimatorSet animSet = new AnimatorSet();
ObjectAnimator[] animators = new ObjectAnimator[introTextViews.length];
for(int i=0; i<introTextViews.length; i++){
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
animSet.play(animators[0]).with(animators[1]); // Anim1
animSet.play(animators[2]).with(animators[3]).after(animators[0]); //Anim2
animSet.play(animators[4]).with(animators[5]).after(animators[2]); //Anim3
animSet.play(animators[6]).with(animators[7]).after(animators[4]); //Anim4
animSet.setDuration(6000);
animSet.setInterpolator(new AccelerateDecelerateInterpolator());
animSet.start();
The problem is that TextView objects are in the screen before animation been started. While Anim1 is running the TextViewobjects that belong to the Anim2, Anim3, Anim4 are on the screen. When Anim2 time has come TextView objects dissappear and start the animation, while the Anim3 and Anim4 are still visible waiting for their time to come!
I want the TextView objects invisible until they will come in the screen after animation ends.

Finally I did this:
for(int i=0; i<introTextViews.length; i++){
//Add this line just to fix the animation initial X point
introTextViews[i].setX(getResources().getDisplayMetrics().widthPixels+10);
animators[i] = ObjectAnimator.ofFloat(introTextViews[i],
TextView.TRANSLATION_X, getResources().getDisplayMetrics().widthPixels,
0);
}
I know It is a little "hacky" and I am still waiting for any official answer!

Your solution is good,and there is another solution you can refer:
in your for loop,set all TextViews to invisible by setVisibility(View.Gone),then set AnimatorListener for each Animator,and implement its onAnimationStart,where you set the corresponding TextView visible.

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

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.

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- setDuration not Working? ObjectAnimator

I'm trying to move an ImageView down using ObjectAnimator. The setDuration() method doesn't seem to impact the duration of the animation. I can't set this animation in the XML as I want to play this dynamically.
ObjectAnimator an= ObjectAnimator.ofFloat(img, View.TRANSLATION_Y, 0, 100);
an.setDuration(5000);
an.start();
Sorta off topic here but say I want to play more than one of those ObjectAnimation after each other, Is there a way to do that? I've looked at the Animator set but I wasn't sure if it would move the actual object like an Object Animator, instead of just appearing as so as in the TranslateAnimation.
Thanks in advance!
Maybe you are testing on Samsung device? In this case, under developer options there is the option Animator duration scale that is set to off by default. If you switch to the normal value you can see the animations.
Hope this help.
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = inflater.inflate(R.layout.loading_popup, null,false);
popup = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT, true);
popup.setContentView(popupView);
popup.setOutsideTouchable(true);
ImageView loadingPlane=(ImageView)popupView.findViewById(R.id.loading_plane);
RotateAnimation anim = new RotateAnimation(0.0f, 360.0f,Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
// Step 2: Set the Animation properties
anim.setInterpolator(new LinearInterpolator());
//anim.setRepeatCount(Animation.INFINITE);
anim.setRepeatCount(-1);
anim.setDuration(1500);
loadingPlane.startAnimation(anim);
findViewById(R.id.flyinstyle).post(new Runnable() {
public void run() {
popup.showAtLocation(findViewById(R.id.flyinstyle), Gravity.CENTER, 0, 0);
}
});

How can I have text blink every second?

In my application I have displayed the time in the form of "hh:mm a". Now I want to blink the : for every second, how?
You can use this:
TextView myText = (TextView) findViewById(R.id.myText );
Animation anim = new AlphaAnimation(0.0f, 1.0f);
anim.setDuration(50); //You can manage the time of the blink with this parameter
anim.setStartOffset(20);
anim.setRepeatMode(Animation.REVERSE);
anim.setRepeatCount(Animation.INFINITE);
myTextview.startAnimation(anim);
put : in a view inside viewFlipper , set blinking effect in showNext() and showPrevious() .set autoStart= true and indeterminant=true ;

Categories

Resources