androidandroid-studioanimationandroid-imageview

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


Solution

  • 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>