1

This is my xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <Button
        android:id="@+id/bounceBallButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="Bounce Ball" />

    <ImageView
        android:id="@+id/bounceBallImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/bounceBallButton"
        android:src="@drawable/ball" />

</RelativeLayout>

and this is my animation code to bounce my imageview:

Button bounceBallButton = (Button) findViewById(R.id.bounceBallButton);
final ImageView bounceBallImage = (ImageView) findViewById(R.id.bounceBallImage);
bounceBallImage.clearAnimation();
TranslateAnimation transAnim = new TranslateAnimation(0, 0, 0, getDisplayHeight());
transAnim.setStartOffset(500);
transAnim.setDuration(3000);
transAnim.setFillAfter(true);
transAnim.setInterpolator(new BounceInterpolator());                
bounceBallImage.startAnimation(transAnim);

My problem is that the animation takes my imageview below my screen. I want it to hit at the bottom of my screen and bounce and not go below it.

Pozzo Apps
  • 1,859
  • 2
  • 22
  • 32
vipin
  • 11
  • 3

1 Answers1

0

You animate Y position to getDisplayHeight(). Which means, that you tell the framework:

animate this view's top Y position to the screen's bottom.

And framework does exactly what you say.

What you want to do is to animate to getDisplayHeight() - bounceBallImage.getHeight(). But as soon as you do that, you'll find out, that it gives you the same result. That's because getHeight() returns 0, and that's because your view's hasn't been drawn, hence it has not height.

See here how to know the height of the view as soon as it is measured. You'd be using getMeasuredHeight() instead of getHeight().

Community
  • 1
  • 1
azizbekian
  • 60,783
  • 13
  • 169
  • 249
  • bounceBallImage.getHeight() and bounceBallImage.getViewTreeObserver().removeOnGlobalLayoutListener(this); height = bounceBallImage.getMeasuredHeight(); Both returns the same value. and decreasing these from getDisplayHeight() is not resolving the issue. – vipin Mar 29 '17 at 13:40