android动画对象到ConstraintLayout的中心

时间:2019-01-31 14:14:02

标签: android

在我的布局之一中,我具有这种结构

pip 19.0.1 from /Users/me/.pyenv/versions/3.7.2/lib/python3.7/site-packages/pip (python 3.7)

我想动画将 <TextView android:id="@+id/textView19" style="@style/TextAppearance.AppCompat.Caption" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:gravity="center" android:textColor="@color/grey_500" app:layout_constraintBottom_toBottomOf="parent" /> <de.hdodenhof.circleimageview.CircleImageView android:id="@+id/instagram_avatar" android:layout_width="100dp" android:layout_height="100dp" android:layout_gravity="center_horizontal" android:layout_marginStart="8dp" android:layout_marginTop="20dp" android:layout_marginEnd="8dp" android:background="@drawable/circle_white_shape" android:elevation="5dp" android:src="@drawable/img_wizard_1" android:tint="@color/mdtp_white" app:civ_border_color="@color/grey_5" app:civ_border_width="3dp" app:civ_fill_color="@color/mdtp_white" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"/> </android.support.constraint.ConstraintLayout> CircleImageView移动到ConstraintLayout的中心,如何计算设备的中心并移动到该中心?

例如:

instagram_avatar

2 个答案:

答案 0 :(得分:0)

ViewTreeObserver vto = imageView.getViewTreeObserver();
        vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {

                int[] location = new int[2];
                imageView.getLocationOnScreen(location);

                int imgHeight = imageView.getHeight();
                int y = location[1];


                Point point = new Point();
                getWindowManager().getDefaultDisplay().getSize(point);


                imageView.animate()
                        .translationY((point.y) / 2f - y - imgHeight / 2f)
                        .setInterpolator(new AccelerateInterpolator())
                        .setDuration(5000)
                        .start();
            }
        });

答案 1 :(得分:0)

链接到说明,我从那里获取代码-https://robinhood.engineering/beautiful-animations-using-android-constraintlayout-eee5b72ecae3

        val autoTransition = AutoTransition()
        autoTransition.duration = 400

        val set = ConstraintSet()
        set.clone(view as ConstraintLayout)
        set.centerVertically(R.id.instagram_avatar, 0)
        set.centerHorizontally(R.id.instagram_avatar, 0)

        TransitionManager.beginDelayedTransition(view as ConstraintLayout, autoTransition)
        set.applyTo(view as ConstraintLayout)