【问题标题】:How to align the imageview on bottom of another imageview after moving to new location in android?移动到android中的新位置后,如何在另一个imageview的底部对齐imageview?
【发布时间】:2021-02-22 18:59:45
【问题描述】:

我在布局中声明了两个 imageViews(imageView1 和 ImageView2)。 imageView1 分配给底部,imageView2 对齐为 imageView1 的 ALIGN_BOTTOM。在代码中,执行一些操作后,imageView1 正在移动到屏幕中心。现在,imageView2 应该与 imageView1 的 ALIGN_BOTTOM 对齐。这个怎么做?我正在使用下面的布局。

        <RelativeLayout
            android:id="@+id/rlayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageView
                android:id="@+id/imageView1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:adjustViewBounds="true"
                android:scaleType="centerCrop"/>

            <ImageView
                android:id="@+id/imageView2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_centerInParent="true"
                android:layout_alignBottom="@+id/imageView1"
                android:src="@drawable/android"
                android:visibility="visible" />
         </RelativeLayout

【问题讨论】:

  • xml只能设置init位置。你想总是移动两个图像吗?为什么不直接移动布局呢?
  • 我想动态地重新对齐视图位置。imageView1 正在移动到中心,并且从底部到中心。之后我需要将 imageView2 align_bottom 重新对齐到 imageView1

标签: android android-animation android-layoutparams


【解决方案1】:

假设您知道要制作动画的坐标,您可以编写一个使用 ViewPropertyAnimator 的方法,例如:

private void customAnimation(View view, float toX, float toY, int duration, int startDelay) {
    view.animate()
        .translationX(toX)
        .translationY(toY)
        .setDuration(duration)
        .setStartDelay(startDelay)
        .setInterpolator(new AccelerateDecelerateInterpolator());
}

(插值器是可选的,只是让动画看起来更逼真)

然后将其命名为:

customAnimation(imageView1, 100, 100, 2000, 0);
customAnimation(imageView2, 150, 150, 2000, 2000);

您必须在哪里获取并填写适合您的持续时间的坐标和值,然后自己开始延迟,但这应该可以帮助您开始。

如果更适合您,还有方法 translationXBy() 和 translationYBy()。

【讨论】:

    猜你喜欢
    • 2018-06-28
    • 1970-01-01
    • 2014-06-21
    • 1970-01-01
    • 1970-01-01
    • 2015-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多