I've been looking around and the main ways seem to be setting an animation listener on the object. However I have set an animation listener and the animation complete callback does not fire.
Do you know how I can get the callback when an view.animate().translationY() has finished its animation?
root.setLayoutAnimationListener
(
new Animation.AnimationListener()
{
#Override
public void onAnimationStart(Animation animation)
{
}
#Override
public void onAnimationEnd(Animation animation)
{
closeFragmentAnimationComplete(); //is not called
}
#Override
public void onAnimationRepeat(Animation animation)
{
}
}
);
root.animate().translationY(100);
Please try:
view.animate().translationY(100).setListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {}
#Override
public void onAnimationEnd(Animator animation) {
closeFragmentAnimationComplete();
}
#Override
public void onAnimationCancel(Animator animation) {}
#Override
public void onAnimationRepeat(Animator animation) {}
});
Related
This is the code:
//These are the animations of the Menu button getting bigger and smaller
final ScaleAnimation MenuScaleBigger = new ScaleAnimation(1f, 1.1f, 1f, 1.1f,1,0.5f,1,0.5f);
MenuScaleBigger.setDuration(400);
final ScaleAnimation MenuScaleSmaller = new ScaleAnimation(1.1f, 1f, 1.1f, 1f,1,0.5f,1,0.5f);
MenuScaleSmaller.setDuration(400);
MenuScaleBigger.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Menu.setScaleX(1.1f);
Menu.setScaleY(1.1f);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
MenuScaleSmaller.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
Menu.setScaleX(1f);
Menu.setScaleY(1f);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
//This is the click listener that starts the animation of getting bigger
Menu.setOnClickListener(new View.OnClickListener() {
int counter = 0;
#Override
public void onClick(View v) {
if(counter == 0){
Menu.startAnimation(MenuScaleBigger);
counter++;
}
else{
Menu.startAnimation(MenuScaleSmaller);
counter--;
}
}
});
I think that the bouncing happens because of the Menu.ScaleX(); and Menu.ScaleY(); , but I need some way of making the animation stay in the last frame.
What I want is that if I click the Menu Imageview it becomes bigger, and stays bigger, and if I press it again it becomes smaller going back to the original size.
Remove the AnimationListeners and add these two lines
MenuScaleBigger.setFillAfter(true);
MenuScaleSmaller.setFillAfter(true);
setFillAfter(true); makes sure that the View stays in the position after animation and doesn't come back to it's base position
i am currently making an android app and tried to implement an event listener on a new thread.
This is the implementation of the onCreate method of the main activity :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main );
new Thread(new Runnable() {
public void run(){
seek_bar() ;
}
}).start();
}
and the implementation of the seek_bar method :
public void seek_bar()
{
seek=(SeekBar)findViewById(R.id.seekBar1) ;
seek.setOnSeekBarChangeListener(
new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//do somework
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
//do some work
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
//do some work
}
}
) ;
}
It works but my question is what could possibly go wrong , and is it a good practice ?
I have been working on a little game and I need a progress bar animation, which begins when I touch a button for the first time and if I touch the button again before the animation ended the progressbar needs to reset.
In my code animation.start(); works well but the animation.cancel() does not seem to work.
French Version of this Question
My Activity Code containing the line that does not work:
public class MainActivity extends Activity {
Button b_bleu;
PrgressBar bar1;
#Override
public void onWindowFocusChanged(boolean hasFocus) {
b_bleu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ObjectAnimator animation = ObjectAnimator.ofInt(bar1, "progress", 100, 0);
animation.setDuration(5000);
animation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animator) {
}
#Override
public void onAnimationEnd(Animator animator) {
//application se termine
}
#Override
public void onAnimationCancel(Animator animator) {
}
#Override
public void onAnimationRepeat(Animator animator) {
}
});
animation.cancel();
animation.start();
}
});
}
I think the problem is that animation.cancel() has no effect since you call animation.start(); right after it.
Try something like this:
Declare a field:
private boolean mStarted = false;
Then set it in animation callbacks and check its value:
animation.addListener(new Animator.AnimatorListener() {
#Override
public void onAnimationStart(Animator animation) {
mHasStarted = true;
}
#Override
public void onAnimationEnd(Animator animation) {
mHasStarted = false; // reset the field value so that it can be started again onClick once it has ened
}
#Override
public void onAnimationCancel(Animator animation) {
mHasStarted = false;
}
});
if (mStarted) {
animation.cancel();
} else {
animation.start();
}
Hope this helps!
I am using Android Studio, and I want to open a class page with an intent.
My code currently looks like this
public class StartUpScreen extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_up_page);
final ImageView MyImageView =(ImageView) findViewById(R.id.imageView);
final Animation Animation = AnimationUtils.loadAnimation(getBaseContext(), R.anim.startup);
final Animation Animation2 = AnimationUtils.loadAnimation(getBaseContext(), R.anim.abc_fade_out);
MyImageView.startAnimation(Animation);
Animation.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
MyImageView.startAnimation(Animation2);
finish();
Intent a = new Intent(this,MainPage.class);
startActivity(a);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
}
All looks good, but my Intent has a red line from (this,MainPage.class); I have looked and tried changing it with no luck.
Can anyone please help?
I have seen a lot of answers but doesn't seems to find one. I am using new FAB in one of my fragments and want to remove it when that particular fragment goes to backstack, but I am not sure which method in fragment gets called when it is added to back stack and replaced by other fragment.
Following methods were called whenever the Fragment is replaced or added to backstack
1) onPause()
2) onStop()
3) onDestroyView()
call your FAB removing method in any one of the above methods in your Fragment.
http://developer.android.com/guide/components/fragments.html#Creating
Here is my suggestion -
First, add the animation code and backstack listener in your Activity:
public class MainActivity extends AppCompatActivity
implements FragmentManager.OnBackStackChangedListener {
private FloatingActionButton mFab;
private Animation mShowFab;
private Animation mHideFab;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportFragmentManager().addOnBackStackChangedListener(this);
mShowFab = AnimationUtils.makeInAnimation(this, false);
mShowFab.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
mFab.setVisibility(View.VISIBLE);
}
});
mHideFab = AnimationUtils.makeOutAnimation(this, true);
mHideFab.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation animation) {
mFab.setVisibility(View.INVISIBLE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
#Override
public void onAnimationStart(Animation animation) {
}
});
}
public void showFab(boolean show) {
boolean visible = mFab.isShown();
if (show) {
if (!visible)
mFab.startAnimation(mShowFab);
} else {
if (visible)
mFab.startAnimation(mHideFab);
}
}
And then - depending on backstack depth - show or hide the FAB:
#Override
public void onBackStackChanged() {
showFab(getSupportFragmentManager().getBackStackEntryCount() > 0);
}