动画结束后,第一次View.getHeight变为“ 0”,但是我得到的是高度,为什么?

时间:2019-02-04 11:31:50

标签: android viewpropertyanimator

我有一个动画来查看活动中的上下滑动,我正在使用以下代码。第一次,动画结束后,我将rlView的高度设置为“ 0”

private void animateView() {
        Log.d(TAG, "animateView: height" + markLocationBinding.rlView.getHeight());
        if (markLocationBinding.ivHurray.getVisibility() == View.GONE) {
            markLocationBinding.ivHurray.setVisibility(View.VISIBLE);
            Log.d(TAG, "animateView: inside height" + markLocationBinding.rlView.getHeight());
            markLocationBinding.ivHurray.animate()
                    .translationY(markLocationBinding.rlView.getHeight())
                    .alpha(1.0f)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            Log.d(TAG, "animateView: inside height end" + markLocationBinding.rlView.getHeight());
                        }
                    });
        } else {
            markLocationBinding.ivHurray.animate()
                    .translationY(0)
                    .alpha(0f)
                    .setListener(new AnimatorListenerAdapter() {
                        @Override
                        public void onAnimationEnd(Animator animation) {
                            super.onAnimationEnd(animation);
                            markLocationBinding.ivHurray.setVisibility(View.GONE);

                        }
                    });
        }
    }

我的布局

 <RelativeLayout
            android:id="@+id/ivHurray"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="-450dp"
            android:background="#bb000000"
            android:clickable="true"
            android:orientation="vertical"
            android:visibility="gone">

            <RelativeLayout
                android:id="@+id/rlView"
                android:layout_width="match_parent"
                android:layout_height="450dp">

                <LinearLayout
                    android:id="@+id/llText"
                    android:layout_width="match_parent"
                    android:layout_height="130dp"
                    android:layout_below="@+id/ivMan"
                    android:layout_marginTop="-30dp"
                    android:background="#94aea1"
                    android:gravity="bottom"
                    android:orientation="vertical"
                    android:paddingBottom="15dp">

                    <TextView
                        android:id="@+id/tvHurray"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:padding="8dp"
                        android:text="@string/hurray"
                        android:textColor="@color/white"
                        android:textSize="22dp"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/tvHurraySub"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:padding="5dp"
                        android:text="@string/geo_marked_loc"
                        android:textColor="@color/white"
                        android:textSize="18dp" />
                </LinearLayout>
      <ImageView
                android:id="@+id/ivMan"
                android:layout_width="match_parent"
                android:layout_height="320dp"
                android:background="@drawable/hurray_image"
                android:scaleType="center" />

            </RelativeLayout>

//some other views           
        </RelativeLayout>

2 个答案:

答案 0 :(得分:3)

您需要等到视图布局完成后,才能获取其高度/宽度。您可以使用ViewTreeObserver

markLocationBinding.rlView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            //Need to remove the layout listener otherwise animateView() will be called every time view updates
            rlView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            animateView();
        }
    });

答案 1 :(得分:1)

您可以得到如下测量的高度和宽度:

view.measure(0,0);
int width=view.getMeasuredWidth(); 
int height=view.getMeasuredHeight();