ViewFlipper outAnimation不起作用

时间:2018-05-09 10:40:46

标签: android viewflipper

我希望在文本视图上连续显示从左到右动画的不同文本,在屏幕中央等待3秒,然后在屏幕外部动画,并替换为从左到右动画的新文本

我在viewflipper中包含了一个textview。

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv__inc_pre_sing__screen_title"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="#fff"
        android:gravity="center"
        android:text="PRACTICE"
        android:textAllCaps="true"
        android:textColor="@color/colorAccent"
        android:textSize="16dp"
        android:textStyle="bold"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <android.support.constraint.ConstraintLayout
        android:id="@+id/rl__inc_pre_sing__tm_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:animateLayoutChanges="true"
        android:background="#efff"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tv__inc_pre_sing__screen_title">

        <ImageView
            android:id="@+id/tv__inc_pre_sing__quotation_mark"
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_marginTop="12dp"
            android:src="@drawable/ic_launcher_background"
            app:layout_constraintTop_toTopOf="parent" />

        <ViewFlipper
            android:id="@+id/view_flipper"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_marginEnd="8dp"
            android:animateLayoutChanges="true"
            android:layout_marginStart="24dp"
            android:layout_marginTop="16dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toEndOf="@id/tv__inc_pre_sing__quotation_mark"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/tv"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="Hiiiiii" />


        </ViewFlipper>

        <ImageView
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_marginBottom="8dp"
            android:layout_marginTop="8dp"
            android:src="@drawable/ic_launcher_background"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/view_flipper" />

    </android.support.constraint.ConstraintLayout>

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:text="Click Me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

    <View
        android:layout_width="0dp"
        android:layout_height="16dp"
        android:background="@drawable/ic_launcher_background"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/rl__inc_pre_sing__tm_container" />
</android.support.constraint.ConstraintLayout>

MainActivity Code

public class Main6Activity extends AppCompatActivity {

    private ViewFlipper mViewFlipper;
    private int count = 0;
    private TextView textView;
    private ConstraintLayout rootContainer;

     @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main6);
        mViewFlipper = findViewById(R.id.view_flipper);
        textView = findViewById(R.id.tv);
        rootContainer = findViewById(R.id.rl__inc_pre_sing__tm_container);

        mViewFlipper.setAutoStart(true);
        mViewFlipper.startFlipping();
        mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(rootContainer.getContext(), android.R.anim.slide_in_left));
        mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(rootContainer.getContext(), android.R.anim.slide_out_right));

        Resources resources = getApplicationContext().getResources();
        final String[] textString = resources.getStringArray(R.array.teacher_messages);
 new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    while (!isInterrupted()) {
                        Thread.sleep(3000);
                        runOnUiThread(() -> updateText(textString));
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }

     public void updateText(String[] strings) {
        if (count >= strings.length) {
            count = 0;
        }
        textView.setText(strings[count]);
         count++;
    }
    }

我认为随着textview长度的变化,outanimation不起作用,我想根据textview高度设置约束布局的动画。 不确定这是不是正确的理由。

视图鳍状肢的outAnimation不起作用。

1 个答案:

答案 0 :(得分:1)

这不是ViewFlipper的工作方式。 View鳍状肢只会为添加到其中的视图设置动画。

以下是一个例子:

 <ViewFlipper
        android:id="@+id/viewFlipper"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/loading"
        android:autoStart="true"
        android:flipInterval="5000"
        android:inAnimation="@android:anim/slide_in_left"
        android:outAnimation="@android:anim/slide_out_right">

        <TextView
            fontPath="fonts/benton_light.ttf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/text1"
            android:textColor="@color/white"
            android:textSize="18sp" />

        <TextView
            fontPath="fonts/benton_light.ttf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/text3"
            android:textColor="@color/white"
            android:textSize="18sp" />

        <TextView
            fontPath="fonts/benton_light.ttf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/tex2"
            android:textColor="@color/white"
            android:textSize="18sp" />
    </ViewFlipper>

Source

如果这不符合您的需求。你可以看看这个解决方案。 (Animating text changes in a TextView