Pablo Villar
Pablo Villar

Reputation: 167

How to make an animation in relation to another element?

I am with my first application in which has two TextView (a and b). I have an animation that attempt to drop the "TextView b" at the same level of "TextView a". In my phone works perfectly, but in other cell falls in other positions. Anyone know the correct way?

enter image description here

This is the animation that I'm using:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/bounce_interpolator"
    android:fillAfter="true">

    <translate
        android:fromYDelta="0%![enter image description here][1]p"
        android:toYDelta="265"
        android:duration="1000"
        android:fillEnabled="false"
        />
</set>

Upvotes: 0

Views: 148

Answers (2)

Chandrakanth
Chandrakanth

Reputation: 3831

I don't know how to do it using xml animation. But you can achieve it programmatically in the following way

void startAnimation(){
    TextView textView1=(TextView) findViewById(R.id.textView1);
    TextView textView2=(TextView) findViewById(R.id.textView2);

    Animation animation=new TranslateAnimation(0, 0, 0, textView1.getBottom()-textView2.getBottom());
    animation.setDuration(1000);
    animation.setFillAfter(true);
    animation.setInterpolator(new BounceInterpolator());
    textView2.startAnimation(animation);
}

Upvotes: 3

JASON G PETERSON
JASON G PETERSON

Reputation: 2223

Make some sort of layout where you get the bottom of the TextViews at the vertical center of screen:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:orientation="horizontal" >
</LinearLayout>

Then, after inflating the layout, use the ViewTreeObserver pattern described here to get the height of one of the TextViews (they're identical in height).

Finally, use this value to set a YDelta that will work for every device. Given that you will set it dynamically, it will likely be easier to programmatically create the Animation using the code pattern here.

Upvotes: 0

Related Questions