Подтвердить что ты не робот

Анимация текста с помощью TextSwitcher в Android

Я новичок в Android и участвую в работе с TextSwitcher. Я хочу знать, как текст можно анимировать с помощью TextSwitcher.

У меня есть макет с TextSwitcher и кнопкой. Когда я нажимаю кнопку "Button", TextSwitcher должен переключать текст.

Я читал об этом здесь....

Создать Android TextSwitcher с динамически созданным Textview

Но я не мог заставить его работать.

Как оживить текст, чтобы при щелчке по кнопке TextSwitcher переключался текст.

4b9b3361

Ответ 1

TextSwitcher можно использовать для анимации текста на экране. Подробнее см. блог Использование TextSwitcher в Android , и все содержимое взято из блога Использование TextSwitcher в Android

Нам нужно установить анимацию In и OUT.

  • В анимации: с помощью чего текст входит в экран.
  • Out Animation: с текстом выходит из экрана.

Полный код с соответствующими комментариями

public class MainActivity extends Activity {
    private TextSwitcher mSwitcher;
    Button btnNext;

    // Array of String to Show In TextSwitcher
    String textToShow[]={"Main HeadLine","Your Message","New In Technology","New Articles","Business News","What IS New"};
    int messageCount=textToShow.length;
    // to keep current Index of text
    int currentIndex=-1;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.example1_layout);

        // get The references
        btnNext=(Button)findViewById(R.id.buttonNext);
        mSwitcher = (TextSwitcher) findViewById(R.id.textSwitcher);

        // Set the ViewFactory of the TextSwitcher that will create TextView object when asked
        mSwitcher.setFactory(new ViewFactory() {

            public View makeView() {
                // TODO Auto-generated method stub
                // create new textView and set the properties like colour, size, gravity etc
                TextView myText = new TextView(MainActivity.this);
                myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                myText.setTextSize(36);
                myText.setTextColor(Color.BLUE);
                return myText;
            }
        });

        // Declare the in and out animations and initialize them
        Animation in = AnimationUtils.loadAnimation(this,android.R.anim.slide_in_left);
        Animation out = AnimationUtils.loadAnimation(this,android.R.anim.slide_out_right);

        // set the animation type of textSwitcher
        mSwitcher.setInAnimation(in);
        mSwitcher.setOutAnimation(out);

        // ClickListener for NEXT button
        // When clicked on Button TextSwitcher will switch between texts
        // The current Text will go OUT and next text will come in with specified animation
        btnNext.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                currentIndex++;
                // If index reaches maximum reset it
                if(currentIndex==messageCount)
                    currentIndex=0;
                mSwitcher.setText(textToShow[currentIndex]);
            }
        });

    }
}