Sliding animation having multiple views synchronization issue

左心房为你撑大大i 提交于 2019-12-05 08:27:52

Sorry for misunderstanding your question.

Anyway, your issue is not about time exactly. Both animations have the same duration but the problem is the sizes of the animated regions are different. Logically, moving a longer distance in the same duration will look slower than moving a smaller distance.

For example, to solve your problem, I used the following code in the OnClickListener:

public void onClick(View v) {
    int width =textView2.getWidth();
    if (counter == 0) {
        textView2.setVisibility(View.VISIBLE);
        ObjectAnimator.ofFloat(textView1, "translationX", 0, -1*width).setDuration(duration1).start();
        ObjectAnimator.ofFloat(textView2, "translationX", 1*width, 0).setDuration(duration1).start();
        counter++;

    }
    else {
        ObjectAnimator.ofFloat(textView1, "translationX", -1*width, 0).setDuration(duration1).start();
        ObjectAnimator.ofFloat(textView2, "translationX", 0, 1*width).setDuration(duration1).start();
        counter--;
    }
}

Try this

<!-- YOUR CONTAINER -->
<LinearLayout
    android:id="@+id/target"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/target"
    android:layout_marginTop="50dp"
    android:orientation="horizontal" >

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_alignParentLeft="true"
        android:background="#f00"
        android:gravity="center"
        android:text="RED"
        android:textColor="#000" />

    <Button
        android:id="@+id/TextView02"
        android:layout_width="50dp"
        android:layout_height="match_parent"
        android:layout_alignBottom="@+id/TextView01"
        android:layout_alignParentRight="true"
        android:layout_alignTop="@+id/TextView01"
        android:background="#0F0"
        android:text="TXT"
        android:visibility="invisible" />
</LinearLayout>

Then use a ViewPropertyAnimator like this (if you can use it)

//Translate your view -50 to the left. 
yourLinearLayout.animate().translationX(-50).setDuration(1000);

With the ObjectAnimator I achieved the same results by using this code

    Button textView02 = (Button) findViewById(R.id.textView02);
    LinearLayout target = (LinearLayout) findViewById(R.id.target);
    int width = textView02.getWidth();

    ObjectAnimator.ofFloat(target, "translationX", 0, -width).setDuration(1000).start();

Hope this helps you

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!