2016年3月6日 星期日

How to animate a TextView using TextSwitcher

Reference:
http://www.learn-android-easily.com/2013/06/android-textswitcher.html
http://www.cnblogs.com/over140/archive/2010/10/22/1857991.html
http://never-say-never.iteye.com/blog/976886


Code snippet:

public class MainActivity implements ViewSwitcher.ViewFactory{
    private TextSwitcher mTextSwitcher;
    private Button mButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        ........
        mTextSwitcher = (TextSwitcher) findViewById(R.id.text_switcher);
        mTextSwitcher.setFactory(this);

       // 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);

        mButton = (Button) findViewById(R.id.button);
        mButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // TODO Auto-generated method stub
                mSwitcher.setText(myTextString);
            }
        });
    }

    // 重寫 ViewSwitcher.ViewFactory makeView()方法,返回一個 ViewTextSwitcher 交換時 使用  
    @Override 
    public View makeView() {  
        TextView textView = new TextView(this);  
        textView.setTextSize(36);
        textView.setColor(Color.White);
        textView.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        return textView;  
    }
}

沒有留言: