📜  如何在Java中使用Java在动画中添加TextSwitcher和动画

📅  最后修改于: 2021-05-09 03:01:15             🧑  作者: Mango

TextSwitcher用于为屏幕上的文本设置动画。它是ViewSwitcher类的子类。它仅包含一个TextView类型的子级。为了在TextSwitcher上设置动画,我们必须添加动画标签,也可以以编程方式添加。这是TextSwitcher的一些用法:

  • 在日期选择器中更改数字
    • 计时器倒计时

      TextSwitcher使用两种类型的动画:

      • 在动画中
      • 外出动画

      方法:

      1. activity_main.xml文件中添加以下代码。
        activity_main.xml
        
        
          
            
          
            


        MainActivity.java
        package com.madhav.maheshwari.gfgTextSwitcher;
          
        import android.graphics.Color;
        import android.os.Bundle;
        import android.view.Gravity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextSwitcher;
        import android.widget.TextView;
        import android.widget.ViewSwitcher;
          
        import androidx.appcompat.app.AppCompatActivity;
          
        public class MainActivity extends AppCompatActivity {
            private TextSwitcher textSwitcher;
            private Button nextButton;
            private int index = 0;
            private String[] arr
                = { "GeeksForGeeks", "A",
                    "Computer", "Science",
                    "Portal", "For",
                    "Geeks" };
            private TextView textView;
          
            @Override
            protected void onCreate(
                Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
          
                textSwitcher = findViewById(R.id.textSwitcher);
                nextButton = findViewById(R.id.button);
                nextButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v)
                        {
          
                            // when the text switcher
                            // reaches at the end
                            // of our array. It will
                            // be reset to 0.
                            if (index == arr.length - 1) {
                                index = 0;
                                textSwitcher.setText(arr[index]);
                            }
                            else {
                                textSwitcher.setText(arr[++index]);
                            }
                        }
                    });
          
                // Here we have to create
                // a TextView for our TextSwitcher
                textSwitcher.setFactory(
                    new ViewSwitcher.ViewFactory() {
                        @Override
                        public View makeView()
                        {
                            textView
                                = new TextView(
                                    MainActivity.this);
                            textView.setTextColor(
                                Color.parseColor("#219806"));
          
                            textView.setTextSize(40);
                            textView.setGravity(
                                Gravity.CENTER_HORIZONTAL);
                            return textView;
                        }
                    });
          
                // This is used to set the text
                // when app starts which is
                // at index i.e 0.
                textSwitcher.setText(arr[index]);
            }
        }


      2. 现在,在MainActivity中添加以下代码。 Java文件。

        主要活动。Java

        package com.madhav.maheshwari.gfgTextSwitcher;
          
        import android.graphics.Color;
        import android.os.Bundle;
        import android.view.Gravity;
        import android.view.View;
        import android.widget.Button;
        import android.widget.TextSwitcher;
        import android.widget.TextView;
        import android.widget.ViewSwitcher;
          
        import androidx.appcompat.app.AppCompatActivity;
          
        public class MainActivity extends AppCompatActivity {
            private TextSwitcher textSwitcher;
            private Button nextButton;
            private int index = 0;
            private String[] arr
                = { "GeeksForGeeks", "A",
                    "Computer", "Science",
                    "Portal", "For",
                    "Geeks" };
            private TextView textView;
          
            @Override
            protected void onCreate(
                Bundle savedInstanceState)
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_main);
          
                textSwitcher = findViewById(R.id.textSwitcher);
                nextButton = findViewById(R.id.button);
                nextButton.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v)
                        {
          
                            // when the text switcher
                            // reaches at the end
                            // of our array. It will
                            // be reset to 0.
                            if (index == arr.length - 1) {
                                index = 0;
                                textSwitcher.setText(arr[index]);
                            }
                            else {
                                textSwitcher.setText(arr[++index]);
                            }
                        }
                    });
          
                // Here we have to create
                // a TextView for our TextSwitcher
                textSwitcher.setFactory(
                    new ViewSwitcher.ViewFactory() {
                        @Override
                        public View makeView()
                        {
                            textView
                                = new TextView(
                                    MainActivity.this);
                            textView.setTextColor(
                                Color.parseColor("#219806"));
          
                            textView.setTextSize(40);
                            textView.setGravity(
                                Gravity.CENTER_HORIZONTAL);
                            return textView;
                        }
                    });
          
                // This is used to set the text
                // when app starts which is
                // at index i.e 0.
                textSwitcher.setText(arr[index]);
            }
        }
        

      输出: