动画文本使用TextSwitcher在Android中文本、动画、Android、TextSwitcher

2023-09-05 06:23:13 作者:别说谁变了你拦得住时间吗

我是新来的Andr​​oid和学习与TextSwitcher工作。 我想知道如何在文本文档使用TextSwitcher动画。

我有一个TextSwitcher和一个按钮的布局。 当我点击按钮,TextSwitcher应切换文本。

我读到这这里....

Create Android的TextSwitcher动态生成的TextView

但我无法得到它的工作。

如何进行动画处理的文本,这样,当我点击按钮TextSwitcher切换文本。

解决方案

TextSwitcher可用于动画在屏幕上的文本。 查看该博客查看详细 使用TextSwitcher在Android中 和所有的内容从博客采取的使用TextSwitcher在Android中

我们需要设置IN和OUT动画。 1:在动画:与文字都在屏幕上。 2:出动画:与文本从屏幕熄灭

全部code适当的意见

 公共类MainActivity延伸活动{
    私人TextSwitcher mSwitcher;
    按钮btnNext;

    //字符串数组来显示TextSwitcher
    字符串textToShow [] = {主标题,您的信息,新技术,新章程,第一财经日报,什么是新的};
    INT messageCount = textToShow.length;
    //保持文本的当前指数
    INT CURRENTINDEX = -1;



    @覆盖
    保护无效的onCreate(包savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        的setContentView(R.layout.example1_layout);

        //获取引用
        btnNext =(按钮)findViewById(R.id.buttonNext);
        mSwitcher =(TextSwitcher)findViewById(R.id.textSwitcher);

        //设置将创建TextView的对象TextSwitcher的的ViewFactory当记者问
        mSwitcher.setFactory(新的ViewFactory(){

            公共查看makeView(){
                // TODO自动生成方法存根
                //创建新的TextView,设置像clolr,大小等属性
                TextView中会将myText =新的TextView(MainActivity.this);
                myText.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
                myText.setTextSize(36);
                myText.setTextColor(Color.BLUE);
                会将myText回报;
            }
        });

        //声明进出动画并进行初始化
        动画= AnimationUtils.loadAnimation(这一点,android.R.anim.slide_in_left);
        动画OUT = AnimationUtils.loadAnimation(这一点,android.R.anim.slide_out_right);

        //设置textSwitcher的动画类型
        mSwitcher.setInAnimation(在);
        mSwitcher.setOutAnimation(出);

        // ClickListener的NEXT按钮
        //当点击按钮TextSwitcher将文本之间切换
        //当前文本将走出去,下一个文本将要与指定的动画
        btnNext.setOnClickListener(新View.OnClickListener(){

            公共无效的onClick(视图v){
                // TODO自动生成方法存根
                CURRENTINDEX ++;
                //如果指数达到最大值重置
                如果(CURRENTINDEX == messageCount)
                    CURRENTINDEX = 0;
                mSwitcher.setText(textToShow [CURRENTINDEX]);
            }
        });

    }
}
 

这8大特性都不了解 别说你会用Android O系统

I am new to Android and learning to work with TextSwitcher. I want to know how a text can be Animated using TextSwitcher.

I have a layout with a TextSwitcher and a button. When I click on Button, TextSwitcher should switch the Text.

I read about this here ....

Create Android TextSwitcher with dynamically generated Textview

But I could not get it work.

How to animate the Text, such that when I click on Button TextSwitcher switches the Text.

解决方案

TextSwitcher can be used to Animate Text on Screen. See the blog for detail Using TextSwitcher In Android and all the contents are taken from the blog Using TextSwitcher In Android

We need to set In and OUT animation . 1: In Animation: with which Text come in the Screen. 2: Out Animation: with which Text goes out from the Screen.

Full Code with proper comments

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 clolr, size 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]);
            }
        });

    }
}