Reputation: 57
I am basically trying to get a text to vibrate; I created a few animations and put them in an AnimationSet but can't get it to repeat. I've looked through a bunch of solutions here and haven't gotten any to work for me yet. Can anyone tell me what I'm doing wrong or suggest a better way? My code is as following:
package com.example.dealbreaker;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import java.util.Random;
public class LobbyOwner extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lobby_owner);
ShakeIt();
}
private void ShakeIt() {
final int animationTime = 1000;
final int fromXDelta = 0;
final int fromYDelta = 0;
AnimationSet textAnimSet = new AnimationSet(true);
for (int i = 0; i < 10; ++i) {
final float randomX = (float) Math.random();
final float randomY = (float) Math.random();
float toXDelta = (randomX < 0.5) ? randomX * 10: -randomX * 10;
float toYDelta = (randomY < 0.5) ? randomY * 10: -randomY * 10;
TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
TranslateAnimation antiAnimation = new TranslateAnimation(fromXDelta, -toXDelta, fromYDelta, -toYDelta);
animation.setStartOffset(i * animationTime);
textAnimSet.addAnimation(animation);
textAnimSet.addAnimation(antiAnimation);
}
Log.i("AnimationSet", textAnimSet.getAnimations().toString()); //shows 10 animations in the animationset
textAnimSet.setDuration(animationTime);
textAnimSet.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {
//do I need something here?
}
@Override
public void onAnimationEnd(Animation animation) {
//textAnimSet.start(); //doesn't work?!
//tv.startAnimation(textAnimSet); //doesn't work
//Log.i("Information", "We are here"); //generates 3 outputs???
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
TextView tv = (TextView) findViewById(R.id.excitedLobbyTxtView);
tv.clearAnimation();
tv.startAnimation(textAnimSet);
}
}
Upvotes: 0
Views: 69
Reputation: 2657
please try to reset your animation when its finished for i textAnimSet.reset();
complet code :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ShakeIt();
}
private void ShakeIt() {
TextView tv = (TextView) findViewById(R.id.excitedLobbyTxtView);
tv.clearAnimation();
final int animationTime = 1000;
final int fromXDelta = 0;
final int fromYDelta = 0;
AnimationSet textAnimSet = new AnimationSet(true);
for (int i = 0; i < 10; ++i) {
final float randomX = (float) Math.random();
final float randomY = (float) Math.random();
float toXDelta = (randomX < 0.5) ? randomX * 10: -randomX * 10;
float toYDelta = (randomY < 0.5) ? randomY * 10: -randomY * 10;
TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
TranslateAnimation antiAnimation = new TranslateAnimation(fromXDelta, -toXDelta, fromYDelta, -toYDelta);
animation.setStartOffset(i * animationTime);
textAnimSet.addAnimation(animation);
textAnimSet.addAnimation(antiAnimation);
}
Log.i("AnimationSet", textAnimSet.getAnimations().toString()); //shows 10 animations in the animationset
textAnimSet.setDuration(animationTime);
textAnimSet.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {
//do I need something here?
}
@Override
public void onAnimationEnd(Animation animation) {
textAnimSet.reset();
textAnimSet.startNow();
//textAnimSet.start(); //doesn't work?!
//tv.startAnimation(textAnimSet); //doesn't work
//Log.i("Information", "We are here"); //generates 3 outputs???
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
tv.startAnimation(textAnimSet);
}
Upvotes: 1
Reputation: 57
I figured out why I couldn't restart the animation, I had declared my view tv below my AnimationListener and therefore I couldn't access the variable... I am still confused on why the animation won't return the same direction even with the antiAnimation-variable though. The working code is as follows:
package com.example.dealbreaker;
import androidx.appcompat.app.AppCompatActivity;
import android.animation.AnimatorListenerAdapter;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.AnimationUtils;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;
import android.widget.TextView;
import java.util.Random;
public class LobbyOwner extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lobby_owner);
ShakeIt();
}
private void ShakeIt() {
final int animationTime = 20;
final int fromXDelta = 0;
final int fromYDelta = 0;
AnimationSet textAnimSet = new AnimationSet(true);
for (int i = 0; i < 10; ++i) {
final float randomX = (float) Math.random();
final float randomY = (float) Math.random();
float toXDelta = (randomX < 0.5) ? randomX * 10: -randomX * 10;
float toYDelta = (randomY < 0.5) ? randomY * 10: -randomY * 10;
TranslateAnimation animation = new TranslateAnimation(fromXDelta, toXDelta, fromYDelta, toYDelta);
TranslateAnimation antiAnimation = new TranslateAnimation(-toXDelta, fromXDelta, -toYDelta, fromYDelta);
animation.setStartOffset(i * animationTime);
textAnimSet.addAnimation(animation);
textAnimSet.addAnimation(antiAnimation);
}
textAnimSet.setDuration(animationTime);
TextView tv = (TextView) findViewById(R.id.excitedLobbyTxtView);
tv.clearAnimation();
textAnimSet.setAnimationListener(new Animation.AnimationListener(){
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
tv.startAnimation(textAnimSet);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
tv.startAnimation(textAnimSet);
}
}
Upvotes: 1