Reputation: 141
Code below.
The ObjectAnimator object "slideUpTranslate" works just fine. It slides the ImageView upward.
slideDownTranslate, however, doesn't work at all unless a literal value is supplied in lieu of the integer variable "menuHeight". It doesn't throw an error of any kind, it just does nothing when you press the slide down button.
I've tried manually initializing menuHeight to an arbitrary value in the onCreate call. I've tried designating it as a float instead of an integer. I've tried using different variables for each of the ObjectAnimator objects. I've tried instantiating slideDownTranslate with exactly the same parameters as slideUpTranslate. No cigar. I really don't see a reason why slideDownTranslate isn't working.
Button slideUpButton;
Button slideDownButton;
ImageView menuView;
int menuHeight;
@Override
protected void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
slideUpButton = (Button)findViewById(R.id.slideUpButton);
slideDownButton = (Button)findViewById(R.id.slideDownButton);
menuView = (ImageView)findViewById(R.id.menuView);
menuView.setBackgroundResource(R.drawable.menucropped);
//This works
final ObjectAnimator slideUpTranslate =
ObjectAnimator.ofFloat(menuView,View.TRANSLATION_Y, menuHeight * -1);
slideUpTranslate.setInterpolator(new AccelerateDecelerateInterpolator());
//This does nothing
final ObjectAnimator slideDownTranslate =
ObjectAnimator.ofFloat(menuView,View.TRANSLATION_Y, menuHeight);
slideDownTranslate.setInterpolator(new AccelerateDecelerateInterpolator());
menuView.post(new Runnable() {
@Override
public void run() {
menuHeight = menuView.getHeight();
menuView.setTranslationY(menuHeight);
}
});
slideUpButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
slideUpTranslate.start();
Log.i("Test", "Slide up clicked... Height:" + Integer.toString(menuHeight));
}
});
slideDownButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
slideDownTranslate.start();
Log.i("Test", "Slide down clicked... Height:" + Integer.toString(menuHeight));
}
});
}
}
So in short, this works:
final ObjectAnimator slideDownTranslate =
ObjectAnimator.ofFloat(menuView,View.TRANSLATION_Y, 500);
But this doesn't:
final ObjectAnimator slideDownTranslate =
ObjectAnimator.ofFloat(menuView,View.TRANSLATION_Y, menuHeight);
To further complicate things, slideUpTranslate works with either a literal value or a variable. They're exactly the same, yet one works with a variable in the third parameter slot and one does not.
Going to wind up in the nut house if I can't figure this out soon! Save me!
Upvotes: 0
Views: 48
Reputation: 317372
You're assigning the value of menuView after it's first used to create the animations. Unless there is some hidden assignment of menuItem I can't see, you're passing a value of (menuHeight == 0) to both animators. Then, when you log the value of menuHeight later in your onclick handlers, you're seeing the reassigned value of menuHeight from the Runnable that executed later.
You might want to consider assigning your animators at the very last moment before they are started instead of initializing them in onCreate().
Upvotes: 1