1

我正在尝试相互交换两个文本视图(它们处于不同的线性布局中)。

我的要求与以下链接中提到的要求有点相似,

动画:将 TextView 移动到另一个容器中

我使用了“nineoldandroids”库的 ObjectAnimator,但是当它移动到其他布局时,textview 会消失。

有人可以帮我解决这个问题。提前致谢。

更新1:-

现在我使用“nineoldandroids”库的 PropertyValuesHolder 来执行翻译动画。使用 PropertyValuesHolder 后,textview 在完成动画后可见(即不消失)。但是动画后onclick功能不起作用。

我还放置了 toast 消息以在动画后检查 Textviews 的 X 尺寸。

然后我得到以下显示:- 动画之前:- 对于第一行和第一列的 textview(即 tv11):- 0.0 用于第二行和第二列的 textview(即 tv22):- 0.0

动画后:- 对于第一行和第一列的 textview(即 tv11):- 240.0 对于第二行和第二列的 textview(即,tv22):- -240.0

无论如何,将这些文本视图作为其他布局的子视图(到它们被移动的位置)。

请找到以下相同的代码:-

  • MainActivity.java:-

    findViewById(R.id.tv22).setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {
    
                PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", +240);
                PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", +380);
                ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv11), pvhX, pvhY).setDuration(2000).start();
    
    
                PropertyValuesHolder pvhX2 = PropertyValuesHolder.ofFloat("x", -240);
                PropertyValuesHolder pvhY2 = PropertyValuesHolder.ofFloat("y", -380);
                ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv22), pvhX2, pvhY2).setDuration(2000).start();
                Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv11)).getX()+"tv11", Toast.LENGTH_SHORT).show();
                Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv22)).getX()+"tv22", Toast.LENGTH_SHORT).show();
    
            }
        });
    
  • activity_main.xml:-

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false"
    android:orientation="vertical"
    tools:context=".MainActivity" >
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp" />
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:clipChildren="false"
    android:orientation="horizontal" >
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv11"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv13"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    </LinearLayout>
    
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp" />
    
    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:clipChildren="false"
    android:orientation="horizontal" >
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv21"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    
    <TextView
        android:id="@+id/tv22"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"
        android:background="#4374E0" />
    
    <View
        android:layout_width="1dp"
        android:layout_height="match_parent" />
    </LinearLayout>
    
    <View
    android:layout_width="match_parent"
    android:layout_height="1dp" />
    
    </LinearLayout>
    
  • 更新 2:-

即使执行了以下更改,onclick 也无法正常工作。有人可以帮助我。

        findViewById(R.id.tv22).setOnClickListener( new View.OnClickListener() {
            public void onClick(View v) {

                Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv11)).getX()+"tv11"+((TextView)findViewById(R.id.tv22)).getX()+"tv22", Toast.LENGTH_SHORT).show();                  

                PropertyValuesHolder pvhX = PropertyValuesHolder.ofFloat("x", +240);
                PropertyValuesHolder pvhY = PropertyValuesHolder.ofFloat("y", +380);
                final ObjectAnimator changeIn = ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv11), pvhX, pvhY).setDuration(2000);
                changeIn.addListener(new AnimatorListenerAdapter() {
                    public void onAnimationEnd(Animator anim) {                 
                        view.setScaleX(1f);
                        view.setScaleY(1f);*/                                                                                   
                        TextView testtv1= (TextView)findViewById(R.id.tv11);                            
                        TextView testtv2= (TextView)findViewById(R.id.tv22);
                        testtv1.clearAnimation();
                        testtv2.clearAnimation();
                        View v1= container1.getChildAt(1);
                        View v2= container2.getChildAt(3);
                        Log.i("childcount1 before",container1.getChildCount()+"");
                        container1.removeView(v1);
                        Log.i("childcount1 after",container1.getChildCount()+"");
                        Log.i("childcount2 before",container2.getChildCount()+"");
                        container2.removeView(v2);
                        Log.i("childcount2 after",container2.getChildCount()+"");
                        Log.i("check1","Removed Successfully");

                        LinearLayout.LayoutParams lp = new    LinearLayout.LayoutParams(0,0);
                        container1.addView(v2, 1,lp);                           
                        container2.addView(v1, 3,lp);

                        Toast.makeText(MainActivity.this, ((TextView)findViewById(R.id.tv11)).getX()+"tv11"+((TextView)findViewById(R.id.tv22)).getX()+"tv22", Toast.LENGTH_SHORT).show();

                    }
                });
                changeIn.start();

                PropertyValuesHolder pvhX2 = PropertyValuesHolder.ofFloat("x", -240);
                PropertyValuesHolder pvhY2 = PropertyValuesHolder.ofFloat("y", -380);
                ObjectAnimator.ofPropertyValuesHolder(findViewById(R.id.tv22), pvhX2, pvhY2).setDuration(2000).start();


            }
        });
4

0 回答 0