簡體   English   中英

在動態視圖中翻譯動畫效果不佳

[英]Translate Animation is not working perfectly in dynamic view

在此項目中,我想在單擊按鈕時將TextView的副本從頂部TextView位置移至底部TextView位置。

使用翻譯動畫時出現問題。 我想將視圖移到確切位置(例如Zapya中的視圖)。

在Zapya中,當用戶選擇項目並選擇“發送”時, gridview項目的克隆將移至用戶圖標。 因此,我使用翻譯動畫來移動視圖。 我動態創建了一個視圖以顯示項目的克隆。

使用“平移動畫”可與最初在xml中創建的視圖一起使用,但不適用於動態創建的視圖。

代碼在這里

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



        }
    });

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

“視圖”僅顯示開始和結束時間。 但不要介於兩者之間。 我該如何解決。 請告知是否還有其他方法。 我會很感謝。

增加持續時間,可能是一個問題

anim.setDuration(4000);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM