简体   繁体   中英

Translate Animation is not working perfectly in dynamic view

In this project, I want to move TextView's clone from top TextView place to bottom TextView place when button is click.

I got a problem in using Translate Animation. I want to move a view to exact position(Like in Zapya).

In Zapya, when user select item and choose "send", the gridview item's clone move to user icon. So, I use Translate Animation to move view. And I created a view dynamically to show item's clone.

Using Translate Animation works with view that is initially created in xml but not with view that is dynamically created.

The code is here

    top = (TextView) findViewById(R.id.textviewtop);
    bottom = (TextView) findViewById(R.id.textviewbottom);
    button = (Button) findViewById(R.id.button);


    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            int []from = new int[2];
            int []to = new int[2];

              from[0] = (int) top.getX();
            from[1] = (int) top.getY();
             to[0] = (int) bottom.getX();
            to[1] = (int) bottom.getY();
            ViewGroup vg = (ViewGroup) top.getParent();

            view = new TextView(getApplicationContext());
            view.setWidth(top.getWidth());
            view.setHeight(top.getHeight());
            view.setX(from[0]);
            view.setY(from[1]);
            view.setText(top.getText());
            view.setVisibility(View.VISIBLE);
            view.setTextColor(Color.BLACK);
            view.setId(0x23454657);
            vg.addView(view);


            TranslateAnimation anim = new TranslateAnimation( 0, 0 , from[1], to[1] );
            anim.setDuration(1000);
            anim.setFillAfter( true );
            view.startAnimation(anim);



        }
    });

and xml file

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/rl"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/textviewtop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentTop="true"
        android:text="@string/hello_world" />


    <Button 
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="Move"/>

    <TextView
        android:id="@+id/textviewbottom"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        android:text="@string/hello_world" />
</RelativeLayout>

"view" show only starting and ending time. But not in between. How can I solve that. Please tell if there's any other way. I will thanks a ton.

增加持续时间,可能是一个问题

anim.setDuration(4000);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM