I am trying to perform 2 text animations in a row with my TextView under Android
first:
TextView target = (TextView)findViewById(R.id.textanimation);
target.setText(text1);
target.startAnimation(animationSet1);
animationSet1 is a set of alpha and translate animations:
Animation alpha = new AlphaAnimation(0.0f, 1.0f);
Animation a = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0, Animation.RELATIVE_TO_SELF, 0,
Animation.RELATIVE_TO_SELF, -1.0f,
Animation.RELATIVE_TO_SELF, 0);
alpha.setDuration( 1000 );
a.setDuration( 1000 );
When onAnimationEnd happens in animationSet1 I immediately start another animation
TextView target = (TextView)findViewById(R.id.textanimation);
target.setText(text2);
target.startAnimation(animationSet1);
However in the 2nd animation I can see on a split of second a fully opaque text, however it should start with 0 alpha. How to avoid this situation? Will appresiate any answer.
P.S. Even if I have 2 separate textviews with the prepared text in both - same bug is seen.
If I put some delay between the animations both of them become smooth.
Sergey
I'm not sure how you are triggering the start of the 2nd animation following the termination of the 1st animation. I'm guessing you're checking to see if the animation on a view is null. This isn't the best or correct way to do it. Instead you should you an AnimationListener
Animation animation1 = new Animation();//Replace with your animation
animation1.setDuration(DURATION_1);
animation1.setAnimationListener(new AnimationListener() {
public void onAnimationStart(Animation arg0) {}
public void onAnimationRepeat(Animation arg0) {}
public void onAnimationEnd(Animation arg0) {
Animation animation2 = new Animation();
animation2.setDuration(DURATION_2);
view2.startAnimation(animation2);
}
});
view1.startAnimation(animation1);
Related
I wonder how to animate current Activity, not transition between activities. For example, I want to shift the activity slightly to the left and then move it back, so that it looks like a little shake effect. Any ideas? Thanks
Finally I have found a solution:
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, -0.05f,
Animation.RELATIVE_TO_PARENT, 0.0f,
Animation.RELATIVE_TO_PARENT, 0.0f);
animation.setDuration(100);
animation.setInterpolator(new AccelerateInterpolator());
animation.setRepeatCount(1);
animation.setRepeatMode(Animation.REVERSE);
View viewActivity = findViewById(android.R.id.content);
if (viewActivity != null)
viewActivity.startAnimation(animation);
you need to create animation instance like below
Animation startAnimation = AnimationUtils.loadAnimation(context, R.anim.shaking_animation);
then u need to start that animation with your parent layout
parent_layout.startAnimation(startAnimation);
you can get shake animation here shaking / wobble view animation in android
finally you can clear animation like below
parent_layout.clearAnimation();
The basic idea is to get access to the root layout of the activity and then do some animations on it, e.g. if you use DataBindung it is just like this (but not the best idea for the kind of animation you want):
binding.getRoot().animate().rotation(-5).setDuration(500).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
binding.getRoot().animate().rotation(5).setDuration(1000).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
binding.getRoot().animate().rotation(0).setDuration(500);
}
});
}
});
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.
I'm trying to make a custom navigation drawer in android.
It should compromise of a spinner view for selecting city from the list, then city temperature and then name, email and contact no.
I'm able to make city option and edit text list for name, email etc. Now I can't insert temp details in it.
need help
Closing animation of your slider
public static void playcloseanimationofleftdrawer() {
TranslateAnimation translateAnimation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, -1.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
translateAnimation.setInterpolator(new DecelerateInterpolator(2.0f));
AlphaAnimation alphaAnimation = new AlphaAnimation(1.0f, 0.0f);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(translateAnimation);
animation.addAnimation(alphaAnimation);
animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
// Do nothing
}
#Override
public void onAnimationRepeat(Animation animation) {
// Do nothing
}
#Override
public void onAnimationEnd(Animation animation) {
}
});
animation.setDuration(500);
leftdrawer.startAnimation(animation);
}
Opening animation of your slider
public static void playopenanimationleftdrawer() {
TranslateAnimation translateAnimation = new TranslateAnimation(
TranslateAnimation.RELATIVE_TO_SELF, -4.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f);
translateAnimation.setInterpolator(new DecelerateInterpolator(2.0f));
AlphaAnimation alphaAnimation = new AlphaAnimation(0.0f, 1.0f);
AnimationSet animation = new AnimationSet(false);
animation.addAnimation(translateAnimation);
animation.addAnimation(alphaAnimation);
animation.setDuration(1000);
animation.setFillAfter(true);
leftdrawer.startAnimation(animation);
}
Just think how you can achieve that!Anyway giving you how to do it.First make relative layout inside your layout,call that relative layout lets say left drawer.After which give that new created layout a color,basically you might want to give white color,ok next step is to give the height of relative layout
match_parent or fill_parent
and the width of your layout should be fixed size...or if you want your drawer to be from start of screen to end of screen you can still use above written code.So after all this all you have to do is to put a button above your created layout(most useful button is ToggleButton as it have 2 states to be pressed(on/off)) and that button will make your created layout to go either VISIBLE or INVISIBLE,so here you have custom sliding drawer where you can put any widget you like and put some translate animation onto it to make it fell real drawer.Good Luck
I want to apply a translate animation to the view from bottom right to center THEN scale animation from where it finished translation. Once this is done, I want the view to come back to its original size and position. Any idea how to do that? Here is the code I am using for animation
TranslateAnimation translateAnim = new TranslateAnimation(0, midScreenX, 0, midScreenY);
translateAnim.setDuration(2000);
ScaleAnimation scaleAnim = new ScaleAnimation(1, 4, 1, 4, Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF,1f);
scaleAnim.setDuration(2000);
use an AnimationListener to chain the animations and use setFillAfter(true)
TranslateAnimation translateAnim = new TranslateAnimation(0, midScreenX, 0, midScreenY);
translateAnim.setDuration(2000);
// user setFillAfter to make sure that we scale from the last position it was animated to.
translateAnim.setFillAfter(true);
// make scaleAnim final so we can use it in the AnimationListener.onAnimationEnd method
final ScaleAnimation scaleAnim = new ScaleAnimation(1, 4, 1, 4, Animation.RELATIVE_TO_SELF,1f, Animation.RELATIVE_TO_SELF,1f);
scaleAnim.setDuration(2000);
scaleAnim.setFillAfter(true);
translateAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
// now that the first animation is done, start the second
someViewObj.startAnimation(scaleAnim);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
// start first animation
someViewObj.startAnimation(translateAnim);
Android Docs : http://developer.android.com/reference/android/view/animation/Animation.AnimationListener.html
http://developer.android.com/reference/android/view/animation/Animation.html#setFillAfter(boolean)
I applyed a TranslateAnimation to an EditText with the FillAfter=true in order to keep its position at the end of the animation.
The animation works properly but the problem is that I can't enter to the edittext anymore.
I think it's due to the fact that the animation affects only the rendering without modifying the actual view coordinates.
Is there any possibility to maintain the final coordinates with the edittext normally working?
Thanks,
Daniele
Unfotunately the animation only renders the raw pixels of the animated element, but not its "android-intern" position. The best solution (that i am able to come up with) is to use an AnimationListener and set the position of the animated element correctly after the animation has finished. Here is my code to slideDown a searchWrapper:
public void toggleSearchWrapper() {
AnimationSet set = new AnimationSet(true);
// slideDown Animation
Animation animation = new TranslateAnimation(
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.0f, Animation.RELATIVE_TO_SELF, 1.0f
);
animation.setDuration(300);
animation.setFillEnabled(false);
animation.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(final Animation anim) {
};
#Override
public void onAnimationRepeat(final Animation anim) {
};
#Override
public void onAnimationEnd(final Animation anim) {
// clear animation to prevent flicker
searchWrapper.clearAnimation();
// set new "real" position of wrapper
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, R.id.branchFinderIncludeHeader);
searchWrapper.setLayoutParams(lp);
}
});
set.addAnimation(animation);
// set and start animation
searchWrapper.startAnimation(animation);
}