简体   繁体   English

在动态视图中翻译动画效果不佳

[英]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. 在此项目中,我想在单击按钮时将TextView的副本从顶部TextView位置移至底部TextView位置。

I got a problem in using Translate Animation. 使用翻译动画时出现问题。 I want to move a view to exact position(Like in Zapya). 我想将视图移到确切位置(例如Zapya中的视图)。

In Zapya, when user select item and choose "send", the gridview item's clone move to user icon. 在Zapya中,当用户选择项目并选择“发送”时, gridview项目的克隆将移至用户图标。 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. 使用“平移动画”可与最初在xml中创建的视图一起使用,但不适用于动态创建的视图。

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 和xml文件

<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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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