[英]Stop animation from taking my view out of screen android
這是我的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>
這是我的動畫代碼來反彈我的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);
我的問題是動畫將我的imageview帶到了我的屏幕下方。 我想讓它擊中我的屏幕底部並彈跳而不是低於它。
您將Y
位置設置為getDisplayHeight()
。 這意味着,你告訴框架:
將此視圖的頂部
Y
位置設置為屏幕底部的動畫。
而框架正是你所說的。
你想要做的是動畫getDisplayHeight() - bounceBallImage.getHeight()
。 但是一旦你這樣做,你就會發現,它會給你相同的結果。 那是因為getHeight()
返回0,那是因為你的視圖沒有被繪制,因此它沒有高度。
請在此處查看如何在測量后立即了解視圖的高度。 您將使用getMeasuredHeight()
而不是getHeight()
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.