Cristian Babarusi
Cristian Babarusi

Reputation: 1505

How to move ImageView to another ImageView Android Animation

Anyone know how i can move an imageview to another imageview ?

enter image description here

im thinking at this kind of method, or maybe is another one that i dont know... no problem, im glad to learn it

<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:interpolator="@android:anim/linear_interpolator">

<translate
    android:duration="800"
    android:fromXDelta="0%p"
    android:toXDelta="75%p" />

i know that fromXDelta (is the starting position) and the toXDelta is the possition where should arrive.. but? how i can know what is my starting position and arrive position looking at my picture example?

Added details:

There are 4 different layouts in here, but only 3 are with weight value. The bar from top where are buttons is a layout but not have weight like the rest.

So laying cards from up are in the layout1 My desired position to arrive are in the layout2 Playing cards from down are in the layout3

Also i use onClick methods in xml not onClickListeners. Thanks

Upvotes: 2

Views: 4340

Answers (3)

Jaydeep Kasture
Jaydeep Kasture

Reputation: 21

private fun moveImageToSpots() {

                ObjectAnimator.ofFloat(cardToMove, "translationX", cardToMove.x,targetCard.y.toFloat()).apply {
                    duration = 500
                    start()
                }
                ObjectAnimator.ofFloat(cardToMove, "translationY", cardToMove.y, targetCard.y.toFloat()).apply {
                    duration = 500
                    start()
                }

}

Upvotes: 0

Cristian Babarusi
Cristian Babarusi

Reputation: 1505

Finnaly i have succeded with the help of user Francois L.

Was a lot of work to do, i don't say no, because i needed to redesign all my layouts, but this is no problem cause i learned something new and i appreciate all the help i can receive.

If anyone want to move a view (any type of view, imageview, buttonview, textview etc ) to another view it is necessary that both views to be in the same layout, that is the most important part.

And the code to achieve this is:

//here is the part of initialization
    ImageView poz1Inamic = (ImageView) findViewById(inamic_pozitia1);
    ImageView poz1InamicSpateCarte = (ImageView) findViewById(inamic_pozitia1spatecarte);

    //other code
    poz1InamicSpateCarte.setVisibility(View.INVISIBLE);

    //here is the initialization of the arrive position
    ImageView cartePusaInamic = (ImageView) findViewById(R.id.cartePusaInamic);

    //the code for moving from start position to arrive position
    poz1Inamic.animate()
            .x(cartePusaInamic.getX())
            .y(cartePusaInamic.getY())
            .setDuration(333)
            .start();

Upvotes: 0

Fran&#231;ois Legrand
Fran&#231;ois Legrand

Reputation: 1223

You can do this programmatically like this :

@Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final View target = findViewById(R.id.target);
    final View viewToMove = findViewById(R.id.viewToMove);

    viewToMove.setOnClickListener(new View.OnClickListener() {
        @Override public void onClick(View v) {
            translate(viewToMove, target);
        }
    });
}

private void translate(View viewToMove, View target) {
    viewToMove.animate()
            .x(target.getX())
            .y(target.getY())
            .setDuration(1000)
            .start();
}

Here the XML :

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
android:id="@+id/activity_main"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/target"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:layout_gravity="center"
    android:background="@color/colorAccent"/>

<ImageView
    android:id="@+id/viewToMove"
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:background="@color/colorPrimary"
    android:src="@mipmap/ic_launcher"/>
</FrameLayout>

Upvotes: 3

Related Questions