I don't know what's happening, but most of my animations don't work. For example:
nothingToShow.animate().alpha(1f).setDuration(android.R.integer.config_shortAnimTime).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
nothingToShow.setAlpha(0f);
nothingToShow.setVisibility(View.VISIBLE);
super.onAnimationStart(animation);
}
});
deleteAll.animate().alpha(0f).setDuration(android.R.integer.config_shortAnimTime).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
deleteAll.setVisibility(View.GONE);
super.onAnimationEnd(animation);
}
});
searchImg.animate().alpha(0f).setDuration(android.R.integer.config_shortAnimTime).setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
searchImg.setVisibility(View.GONE);
super.onAnimationEnd(animation);
}
});
nothingToShow appears, but the other two Views don't dissapear. Most of the animations which aren't working are from alpha 1f to 0f, but not all of them. Some are working. That is very strange. Any idea?
.setDuration() takes a time in milliseconds, not a resource ID (docs)
getResources().getInteger(android.R.integer.config_shortAnimTime)
Related
My code looks like that:
objectAnimator animator = ObjectAnimator.ofFloat(view, "rotation", 0f, (float) truePosition);
animator.setDuration(totalTime);
animator.setInterpolator(new DecelerateInterpolator());
animator.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
einsatz = 0;
disableChips();
imageRoulette.setEnabled(false);
}
#Override
public void onAnimationEnd(Animator animator) {
imageRoulette.setEnabled(true);
imageRoulette.setImageDrawable(null);
imageRoulette.setBackgroundResource(R.drawable.roulette);
enableChips();
printNumberAfterRotation(randPosition);
myBets.clear();
einsatz = -1;
gesamtEinsatz = 0;
}
#Override
public void onAnimationCancel(Animator animator) { }
#Override
public void onAnimationRepeat(Animator animator) { }
});
animator.start();
By now I always have to tap on the view to rotate my image. Is there a way to restart it after 10 seconds?
Do you have any suggestions how to fix it?
#Override
public void onAnimationEnd(Animator animator) {
animation.setStartDelay(10000); // 10 SEC
animation.start();
}
Now I have to create an animation of a spinning fan. When user click POWER ON button, this fan begin to spin, then keep it's spinning speed at some level.When user click POWER OFF button, it slowing down then it stop.
I make some code as follows:
ImageView mFanImageview;
private ValueAnimator mFanValueAnimator;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_centripetal_particle);
ButterKnife.bind(this);
}
private void startFan() {
mFanValueAnimator = new ValueAnimator();
mFanValueAnimator.setDuration(1000);
mFanValueAnimator.setFloatValues(mFanImageview.getRotation(), mFanImageview.getRotation() + 360);
mFanValueAnimator.setInterpolator(new AccelerateInterpolator());
mFanValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
mFanImageview.setRotation((Float) animation.getAnimatedValue());
}
});
mFanValueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
mFanValueAnimator = new ValueAnimator();
mFanValueAnimator.setDuration(500);
mFanValueAnimator.setFloatValues(mFanImageview.getRotation(), mFanImageview.getRotation() + 360);
mFanValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
mFanValueAnimator.setRepeatMode(ValueAnimator.RESTART);
mFanValueAnimator.setInterpolator(new LinearInterpolator());
mFanValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
mFanImageview.setRotation((Float) animation.getAnimatedValue());
}
});
mFanValueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationCancel(Animator animation) {
stopFanValueAnimator().start();
}
});
mFanValueAnimator.start();
}
#Override
public void onAnimationCancel(Animator animation) {
stopFanValueAnimator().start();
}
});
mFanValueAnimator.start();
}
private ValueAnimator stopFanValueAnimator() {
ValueAnimator stopAnimator = new ValueAnimator();
stopAnimator.setDuration(1000);
stopAnimator.setFloatValues(mFanImageview.getRotation(), mFanImageview.getRotation() + 360);
stopAnimator.setInterpolator(new DecelerateInterpolator());
stopAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
mFanImageview.setRotation((Float) animation.getAnimatedValue());
}
});
return stopAnimator;
}
#OnClick(R.id.stop_button)
public void onStopButtonClicked() {
mFanValueAnimator.cancel();
}
#OnClick(R.id.start_button)
public void onStartButtonClicked() {
startFan();
}
The animation above seems ok, but I found that the fan spinning speed at the end of AccelerateInterpolator animator is hard to match the beginning of LinearInterpolator.I have to adjust duration of LinearInterpolator animator carefully.
How can I get the update rate at the end of AccelerateInterpolator animator then set the right duration of LinearInterpolator animator?
I think the best approach is set accelerate interpolator initially, and on repeat again set LinearInterpolator. Now in case of repeat put a flag for handling switch off. If the switch off demanded then change value of switch Off flag that will apply Decelerate interpolator and make sure that the animation will not run again.
mFanValueAnimator.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationRepeat(Animator animation) {
if (switchOff) {
animation.setInterpolator(new DecelerateInterpolator());
} else {
animation.setInterpolator(new LinearInterpolator());
}
}
});
I want to animate my FAB (Floating Action Button).
My goal is presented in this youtube video:
Video
Does anybody know how I can animate my FAB and how will look like the animation in the xml file?
Thanks.
public void hideView(final View view) {
view.animate()
.translationY(view.getHeight()+YOUR MARGIN)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
public void showView(final View view) {
view.animate()
.translationY(0)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
EDITED:
for i sec wait call it from :
handler.postDelayed(new Runnable(){
#Override
public void run() {
showView(...)
}
}, 1000);
you might have to use runOnUiThread(new Runnable(...)) inside run
Re Edit:
public void hideView(final View view) {
view.animate()
.translationY(-10)
.setDuration(30)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
view.animate()
.translationY(view.getHeight()+YOUR MARGIN + 10)
.setDuration(300)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
}
});
}
});
}
I'm trying to animate buttons with fade in animation using AnimatorSet
Button fades in > Click button > Remaining buttons fade out
So in order to do this, I want to set the onClickListner after the animation is completed, but that doesn't seem to work. Clicking a button in the middle of the animation triggers the onClick action:
setQuestion = new AnimatorSet();
setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
setQuestion.start();
This is the method that checks if the animation has finished.
private void checkAnimation() {
while (true) {
// Check if animation has ended
if (setQuestion.isRunning() == false) {
assignListners();
break;
}
}
}
You can set an AnimatorListener on fadeinAnimation5.
This will give you an onAnimationEnd callback.
fadeinAnimation5.addListener(new AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
// ...
}
#Override
public void onAnimationRepeat(Animator animation) {
// ...
}
#Override
public void onAnimationEnd(Animator animation) {
// ...
}
#Override
public void onAnimationCancel(Animator animation) {
// ...
}
});
Or, as suggested by slott use an AnimatorListenerAdapter
fadeinAnimation5.addListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
// ...
}
}
I was having a similar problem and here is how I solved it:
private void crossFadeAnimation(final View fadeInTarget, final View fadeOutTarget, long duration){
AnimatorSet mAnimationSet = new AnimatorSet();
ObjectAnimator fadeOut = ObjectAnimator.ofFloat(fadeOutTarget, View.ALPHA, 1f, 0f);
fadeOut.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
}
#Override
public void onAnimationEnd(Animator animation) {
fadeOutTarget.setVisibility(View.GONE);
}
#Override
public void onAnimationCancel(Animator animation) {
}
#Override
public void onAnimationRepeat(Animator animation) {
}
});
fadeOut.setInterpolator(new LinearInterpolator());
ObjectAnimator fadeIn = ObjectAnimator.ofFloat(fadeInTarget, View.ALPHA, 0f, 1f);
fadeIn.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
fadeInTarget.setVisibility(View.VISIBLE);
}
#Override
public void onAnimationEnd(Animator animation) {}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
});
fadeIn.setInterpolator(new LinearInterpolator());
mAnimationSet.setDuration(duration);
mAnimationSet.playTogether(fadeOut, fadeIn);
mAnimationSet.start();
}
You can actually set a listener to the AnimatorSet directly since AnimatorSet inherits from Animator. Here's some code:
import android.animation.Animator;
AnimatorSet setQuestion = new AnimatorSet();
setQuestion.playSequentially(fadeinAnimationQ,fadeinAnimation1,fadeinAnimation2,fadeinAnimation3,fadeinAnimation4,fadeinAnimation5);
setQuestion.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
// !! turn on your onClickListener here !!
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
setQuestion.start();
I have some spots, which are buttons, animating vertically from top to the bottom of the screen. I have implemented the below:
spot.animate().x(x2).y(y2).scaleX(SCALE_X).scaleY(SCALE_Y).setDuration(animationTime).setListener
(
new AnimatorListenerAdapter()
{
#Override
public void onAnimationStart(Animator animation)
{
animators.add(animation);
}
public void onAnimationEnd(Animator animation)
{
animators.remove(animation);
if (!gamePaused )
{
....
}
}
}
);
Question:
I discover that the buttons animating with accelerating speed at the start and decelerating speed upon end of animation.
How could the code be modified with a LinearInterpolator such that the animation is having uniform speed throughout its journey?
Thanks!!
Did you try to use setInterpolator(TimeInterpolator interpolator) method from ViewPropertyAnimator class? So your code would look like:
spot.animate().x(x2).y(y2).scaleX(SCALE_X).scaleY(SCALE_Y).setInterpolator(new LinearInterpolator()).setDuration(animationTime).setListener(
new AnimatorListenerAdapter()
{
#Override
public void onAnimationStart(Animator animation)
{
animators.add(animation);
}
public void onAnimationEnd(Animator animation)
{
animators.remove(animation);
if (!gamePaused )
{
....
}
}
}
);