0

I have declared two imageViews(imageView1 and ImageView2) in layout. imageView1 assigned to bottom and imageView2 aligned as ALIGN_BOTTOM of imageView1. In code, after performing some action, imageView1 is moving to center of the screen. Now, imageView2 should align to ALIGN_BOTTOM of imageView1. How to do this? i am using below layout.

        <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
rcr
  • 23
  • 3
  • Xml can only set the init postion. Do you want to always move both two images? Why not move the rlayout directly? – Matrix Feb 23 '21 at 06:30
  • i want realign the view position dynamically..imageView1 is moving to center with naimation from bottom to center. after that i need to realign imageView2 align_bottom to imageView1 – Chait Feb 23 '21 at 15:22

1 Answers1

0

Assuming you know the coordinates you want to animate to, you could write a method that uses ViewPropertyAnimator something like :

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());
}

(The Interpolator is optional, just makes the animation look more realistic)

And then call it something like :

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

where you have to get and fill in the proper coordinates and values for your duration and start delay yourself, but this should get you started.

There are also methods translationXBy() and translationYBy() if they suit you better.

A Honey Bustard
  • 3,433
  • 2
  • 22
  • 38